C++ 使用std映射进行比较时出错

C++ 使用std映射进行比较时出错,c++,c++11,stdmap,C++,C++11,Stdmap,首先,这里有一个类似的问题: 但既然没有真正的解决办法,我想再问一次,因为我真的被卡住了,不知所措 我的代码如下: struct MyObj{ //constructor MyObj(){} std::map<std::string, std::string> m_fooMap; bool operator==(const MyObj& other) { if (m_fooMap.size() != other.m_fooMap.size()) re

首先,这里有一个类似的问题:

但既然没有真正的解决办法,我想再问一次,因为我真的被卡住了,不知所措

我的代码如下:

struct MyObj{
//constructor
MyObj(){}
std::map<std::string, std::string> m_fooMap;

bool operator==(const MyObj& other)
{
    if (m_fooMap.size() != other.m_fooMap.size())
        return false;

    std::map<std::string, std::string>::const_iterator i, j;
    i = m_fooMap.cbegin();
    j = other.m_fooMap.cbegin();
    for (; i != m_fooMap.cend(), j != other.m_fooMap.cend(); ++i, ++j)
    {
        if(i->first.empty() || j->first.empty())
            continue;

        if (i->first != j->first)
            return false;

        if (i->second != j->second)
            return false;
    }

  return true;
}

bool operator!=(const MyObj& other)
{
    return !operator==(other);
}
};

struct AnotherObj{
std::map<std::string, MyObj> m_collectionOfObjs; //always guaranteed to contain atleast one entry

bool operator==(const AnotherObj &other) const
    {
        for (auto& objIt : m_collectionOfObjs)
        {
            auto findSeriesIt = other.m_collectionOfObjs.find(objIt.first);

            if (findSeriesIt == other.m_collectionOfObjs.end())
                return false;

            //else found, see if the internal content is the same?
            else
            {
                if (objIt.second != findSeriesIt->second)
                    return false;
            }
        }

        //else
        return true;
    }
};
struct MyObj{
//建造师
MyObj(){}
std::map m_fooMap;
布尔运算符==(常量MyObj和其他)
{
如果(m_fooMap.size()!=other.m_fooMap.size())
返回false;
std::map::const_迭代器i,j;
i=m_fooMap.cbegin();
j=other.m_fooMap.cbegin();
对于(;i!=m_fooMap.cend(),j!=other.m_fooMap.cend();++i,++j)
{
如果(i->first.empty()|j->first.empty())
继续;
如果(i->first!=j->first)
返回false;
如果(i->秒!=j->秒)
返回false;
}
返回true;
}
布尔运算符!=(常量MyObj和其他)
{
返回!运算符==(其他);
}
};
结构另一个对象{
std::map m_collectionofbjs;//始终保证至少包含一个条目
布尔运算符==(常数另一个对象和其他)常数
{
用于(自动和objIt:m_CollectionObjs)
{
auto findSeriesIt=other.m_collectionOfObjs.find(objIt.first);
if(findSeriesIt==other.m_collectionOfObjs.end())
返回false;
//否则,请查看内部内容是否相同?
其他的
{
if(objIt.second!=findSeriesIt->second)
返回false;
}
}
//否则
返回true;
}
};
现在,我有一个std::vector和另一个objvec; 我需要比较这个向量中的项目,彼此之间。为此,我使用==运算符

现在,在每次随机实例中,即使输入数据相同,也似乎存在运行时错误。错误指向“xtree”文件中的以下代码

_Nodeptr _Lbound(const key_type& _Keyval) const
    {   // find leftmost node not less than _Keyval
    _Nodeptr _Pnode = _Root(); //<------------ THIS line is where it points to
    _Nodeptr _Wherenode = this->_Myhead;    // end() if search fails

    while (!this->_Isnil(_Pnode))
        if (_DEBUG_LT_PRED(this->_Getcomp(), this->_Key(_Pnode), _Keyval))
            _Pnode = this->_Right(_Pnode);  // descend right subtree
        else
            {   // _Pnode not less than _Keyval, remember it
            _Wherenode = _Pnode;
            _Pnode = this->_Left(_Pnode);   // descend left subtree
            }

    return (_Wherenode);    // return best remembered candidate
    }
\u Nodeptr\u Lbound(const key\u type&\u Keyval)const
{//查找不小于_Keyval的最左侧节点
_Nodeptr _Pnode=_Root();//\u Myhead;//end()如果搜索失败
而(!this->_Isnil(_Pnode))
if(_DEBUG_LT_PRED(this->_Getcomp(),this->_Key(_Pnode),_Keyval))
_Pnode=this->_Right(_Pnode);//右下子树
其他的
{//_Pnode不小于_Keyval,记住它
_其中node=\u Pnode;
_Pnode=this->_Left(_Pnode);//下降左子树
}
return(_Wherenode);//返回记忆最深刻的候选者
}
我被卡住了,不知道下一步该怎么办。我甚至试着像这样启动构造函数:

MyObj() : m_fooMap(std::map<std::string, std::string>()){}
MyObj():m_fooMap(std::map()){
使用C++11,Visual Studio 2012(v110)

i!=m_fooMap.cend(),j!=other.m_fooMap.cend()
使用逗号运算符,丢弃第一个操作数。它不会同时检查这两个条件,因此当
i
等于end迭代器时,可能会在以后取消引用。您应该改用
运算符:

(m_fooMap.cend() != i) and (other.m_fooMap.cend() != j);
i!=m_fooMap.cend(),j!=other.m_fooMap.cend()
使用逗号运算符,丢弃第一个操作数。它不会同时检查这两个条件,因此当
i
等于end迭代器时,可能会在以后取消引用。您应该改用
运算符:

(m_fooMap.cend() != i) and (other.m_fooMap.cend() != j);

您正在比较来自不同映射的迭代器:

auto findSeriesIt = other.m_collectionOfObjs.find(objIt.first);
if (findSeriesIt == m_collectionOfObjs.end())
    return false;
findSeriesIt
来自
other.m_collectionOfObjs
映射,但您正在将其与
m_collectionOfObjs
的末尾进行比较。应该是:

auto findSeriesIt = other.m_collectionOfObjs.find(objIt.first);
if (findSeriesIt == other.m_collectionOfObjs.end())
    return false;

您正在比较来自不同映射的迭代器:

auto findSeriesIt = other.m_collectionOfObjs.find(objIt.first);
if (findSeriesIt == m_collectionOfObjs.end())
    return false;
findSeriesIt
来自
other.m_collectionOfObjs
映射,但您正在将其与
m_collectionOfObjs
的末尾进行比较。应该是:

auto findSeriesIt = other.m_collectionOfObjs.find(objIt.first);
if (findSeriesIt == other.m_collectionOfObjs.end())
    return false;
你的话

尽管输入数据相同,但似乎存在一个运行时 错误

因此,
operator==
应该在块的末尾返回true,但是您的函数不返回任何值(如果您的映射为空,那么您的函数将到达没有返回语句的块的末尾):

bool运算符==(常量MyObj和其他)
{
如果(m_fooMap.size()!=other.m_fooMap.size())
返回false;
std::map::const_迭代器i,j;
i=m_fooMap.cbegin();
j=other.m_fooMap.cbegin();
对于(;i!=m_fooMap.cend(),j!=other.m_fooMap.cend();++i,++j)
{
如果(i->first.empty()|j->first.empty())
继续;
如果(i->first!=j->first)
返回false;
如果(i->秒!=j->秒)
返回false;
}
//这里缺少退货
}
因此,这是未定义的行为

():

从值返回函数的末尾流出(主函数除外) 没有return语句是未定义的行为

你的话

尽管输入数据相同,但似乎存在一个运行时 错误

因此,
operator==
应该在块的末尾返回true,但是您的函数不返回任何值(如果您的映射为空,那么您的函数将到达没有返回语句的块的末尾):

bool运算符==(常量MyObj和其他)
{
如果(m_fooMap.size()!=other.m_fooMap.size())
返回false;
std::map::const_迭代器i,j;
i=m_fooMap.cbegin();
j=other.m_fooMap.cbegin();
对于(;i!=m_fooMap.cend(),j!=other.m_fooMap.cend();++i,++j)
{
如果(i->first.empty()|j->first.empty())
继续;
如果(i->first!=j->first)
返回false;
如果(i->秒!=j->秒)
返回false;
}
//这里缺少退货
}
因此,这是未定义的行为

():

从值返回函数的末尾流出(主函数除外) 没有return语句是未定义的行为


结果表明映射没有正确实例化。这发生在我身上,因为我使用std::shared_ptr并将其存储在std::vector中。然后对其进行迭代,其中一个共享PTR是一个空PTR。不知道为什么会这样,因为