【发布时间】:2018-12-07 08:14:03
【问题描述】:
首先,这里有一个类似的问题: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)
【问题讨论】:
-
确实没有答案,除了应该是评论的答案。我这样标记了这个答案,你可能也想这样做 deathNode 。祝你好运!
-
@gsamaras 我仍然对那条评论感到困惑。我的地图是结构 MyObj 的成员变量。我不确定问题到底出在哪里。我应该如何实例化地图?请您提供更多说明吗?
-
我的意思是您链接的问题中发布的答案确实不是答案(应该是评论)。但是,我没有时间研究您的问题,抱歉(其他人肯定会这样做,这就是我说祝你好运的原因!)。
-
@rafix07 这是完整的代码。然而,“else if”是一个错字。感谢您指出这一点,我已解决。