您的位置:首页 > 其它

map/set iterator not incrementable

2014-01-13 22:24 387 查看

1、报错

“map/set iterator not incrementable”,程序运行至一般报错

for (ActionBindings::const_iterator cit = _bindings.begin();
cit != _bindings.end(); cit++)
{
if (actionType == (*cit).second._type)
{
cit = _bindings.erase(cit);
}
}
在cit++这行报错。

2、内码解析

当执行完erase之后,cit已经指向空,无法进行++操作。

在C++11中,std::map/vector的erase方法返回iterator,能够指向被删除的下一个。(The other versions return an iterator to the element that follows the last element removed (or
map::end, if the last element was removed).)

3、解决方案

执行完erase,保存cit用于指向下个对象

ActionBindings::const_iterator cit = _bindings.begin();

while (cit != _bindings.end())
{
if (actionType == (*cit).second._type)
{
cit = _bindings.erase(cit);
}
else
{
cit++;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: