map / set迭代器不可增量的错误在C + + [英] map/set iterators not incrementable error in C++
问题描述
当我执行下面的代码,我得到map / set迭代器不可增量的错误。
typedef std :: multimap< ; int,int> MapType;
assgnt :: MapType my_map;
assgnt :: MapType :: iterator it;
for(it = my_map.begin(); it!= my_map.end();)
{
my_map = obj1.addGoodNeighbours(it-> first,it-> second ,my_map);
++ it;
}
请帮助
我不知道 assgnt :: MapType
是什么,但是分配另一个 assgnt :: MapType
<$ p> $
到 my_map
$ p> my_map = obj1.addGoodNeighbours(it-> first,it-> second,my_map);
您应至少重新分配迭代器:
for(it = my_map.begin(); it!= my_map.end(); ++ it;){
my_map = obj1 .addGoodNeighbours(it-> first,it-> second,my_map);
it = my_map.begin();
}
但我相信代码是不正确的。你基本上破坏了你迭代的结构,而迭代它。
EDIT :我们知道MapType现在是什么。所有以上都是正确的。您不能在迭代它们时重新分配地图。
When I execute the following code, I am getting map/set iterators not incrementable error.
typedef std::multimap<int, int> MapType;
assgnt::MapType my_map;
assgnt::MapType::iterator it;
for(it = my_map.begin(); it != my_map.end(); )
{
my_map = obj1.addGoodNeighbours(it->first, it->second, my_map);
++it;
}
Please help
解决方案 I don't know what a assgnt::MapType
is, but assigning another assgnt::MapType
to my_map
inside the for loop, can't be a good thing:
my_map = obj1.addGoodNeighbours(it->first, it->second, my_map);
You should at least reassign the iterator:
for(it = my_map.begin(); it != my_map.end(); ++it;) {
my_map = obj1.addGoodNeighbours(it->first, it->second, my_map);
it = my_map.begin();
}
But i believe that code is far from correct. You are basically destroying the structure that you're iterating while iterating it.
EDIT: Well we do know what a MapType is now. All of the above is still correct. You can't just re-assign your maps while iterating them.
这篇关于map / set迭代器不可增量的错误在C + +的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!