Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/155.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 映射/设置迭代器在C++;_C++_Map_Iterator - Fatal编程技术网

C++ 映射/设置迭代器在C++;

C++ 映射/设置迭代器在C++;,c++,map,iterator,C++,Map,Iterator,当我执行以下代码时,我得到的是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, m

当我执行以下代码时,我得到的是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;
}
typedef std::multimap MapType;
assgnt::MapType my_map;
assgnt::MapType::iterator;
for(it=my_-map.begin();it!=my_-map.end();)
{
my_map=obj1.addGoodNeights(it->first,it->second,my_map);
++它;
}

请帮助

我不知道什么是
assgnt::MapType
,但是在for循环中为
my_map
分配另一个
assgnt::MapType
,这不是一件好事:

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();
}
但我相信这段代码远非正确。你基本上是在破坏你正在迭代的结构


编辑:我们现在确实知道什么是地图类型。以上所有内容仍然正确。您不能在迭代地图时重新分配地图。

您在迭代地图时正在修改地图。
std
容器不支持这一点,因此这是程序中的一个错误。目前,您正在完全覆盖对象,但不支持更详细的内容,甚至不支持添加元素

您之所以会出现此错误,可能是因为您处于调试模式,如果这会导致生产崩溃,我也不会感到惊讶

要实现相同的效果,可以使用临时贴图:

assgnt::MapType my_map;
assgnt::MapType tmp;

for (auto it = my_map.begin(); it != my_map.end(); ++it) {
     // you need to change addGoodNeighboors to add elements in place instead of creating a new map
     obj1.addGoodNeighbours(tmp, it->first, it->second, my_map);  
}

my_map.insert(begin(tmp), end(tmp));

为什么不直接将
++it
放入for构造中?我也试过了。。。得到相同的错误。。。在谷歌上搜索的时候,我在某个地方发现了++这是从for循环中取出来的,它对他们有效。。。但这对我不起作用。我们需要知道这是什么
MapType
。显然不是一个简单的
std::map
?为了将来的参考,请准确说明错误是什么。从您简洁的问题描述中,听起来您好像遇到了编译器错误。但是从您稍后的评论中,听起来您得到了一个相当具体的运行时诊断错误,这对大家都很有帮助;assgnt是类的名称,其中“typedef std::multimap MapType;”是declaredAlright。我们也不知道obj1.addGoodNeights做什么。您在for循环中忘记了一个额外的
+
<代码>+i