使用std映射进行比较的运行时错误

时间:2018-12-07 08:14:03

标签: c++ c++11 stdmap

首先,这里有一个类似的问题:Unusual std::map runtime error

但是因为那里没有真正的解决方案,所以我想再问一遍,因为我真的很固执和无知。

我的代码如下:

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;
    }
};

现在,我有一个std :: vector anotherObjVec; 我需要将向量中的项目相互比较。为此,我使用==运算符。

现在每次在随机实例上,即使输入数据相同,也似乎存在运行时错误。该错误指向“ 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
    }

我被困住了,不知道下一步该怎么做。我什至尝试像这样启动构造函数:

MyObj() : m_fooMap(std::map<std::string, std::string>()){}

使用C ++ 11,Visual Studio 2012(v110)

4 个答案:

答案 0 :(得分:1)

您正在比较来自不同地图的迭代器:

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;

答案 1 :(得分:1)

您的单词

  

即使输入数据相同,也似乎存在运行时   错误。

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

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 is missing here
}

所以它是未定义的行为

from):

  

返回值返回函数的末尾(main除外)   没有return语句是未定义的行为。

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

结果是未正确实例化地图。这发生在我身上,因为我正在使用std :: shared_ptr并将其存储在std :: vector中。然后遍历它,共享的ptrs之一就是nullptr。不知道为什么会这样,因为它是一个共享的ptr,并且它在向量中仍然应该保持引用计数不变,但是我只是更改了向量的迭代技术,现在就可以使用了。

相关问题