重新提出问题 - 问题改写 - 我有一个要求,我需要用新的键和值替换一对。考虑一下 -
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
std::multimap<unsigned int, std::string> mymap;
mymap.insert(std::multimap<unsigned int, std::string>::value_type(0, "A1"));
mymap.insert(std::multimap<unsigned int, std::string>::value_type(0, "A2"));
mymap.insert(std::multimap<unsigned int, std::string>::value_type(2, "C1"));
mymap.insert(std::multimap<unsigned int, std::string>::value_type(2, "C2"));
mymap.insert(std::multimap<unsigned int, std::string>::value_type(1, "B1"));
mymap.insert(std::multimap<unsigned int, std::string>::value_type(1, "B2"));
mymap.insert(std::multimap<unsigned int, std::string>::value_type(1, "B3"));
std::pair<std::multimap<unsigned int, std::string>::iterator, std::multimap<unsigned int, std::string>::iterator> pr = mymap.equal_range(1);
std::multimap<unsigned int, std::string>::iterator it;
for (it=pr.first; it!=pr.second; ++it)
{
unsigned int key = it->first;
key = key+10;
std::string val = it->second;
val = "X" + val;
mymap.erase(it);
mymap.insert(std::multimap<unsigned int, std::string>::value_type(key, val));
}
for ( it=mymap.begin() ; it != mymap.end(); it++ )
{
cout << (*it).first << " => " << (*it).second << endl;
}
return 0;
}
由于迭代器无效,程序在visual studio 2008上崩溃。
我希望它是:
0 => A1
0 => A2
2 => C1
2 => C2
11 => XB1
11 => XB2
11 => XB3
我的想法是想用新条目替换地图中的现有条目。
我做错了什么?非常感谢任何帮助。
答案 0 :(得分:5)
诀窍是首先推进迭代器,然后擦除迭代器的副本。
std::multimap<unsigned int, std::string>::iterator it = pr.first;
while (it != pr.second)
{
unsigned int key = it->first;
key = key+10;
std::string val = it->second;
val = "X" + val;
std::multimap<unsigned int, std::string>::iterator itCopy = it;
++it;
mymap.erase(itCopy);
mymap.insert(std::multimap<unsigned int, std::string>::value_type(key, val));
}
在C ++ 11中,您可以这样做:
std::multimap<unsigned int, std::string>::iterator it = pr.first;
while (it != pr.second)
{
unsigned int key = it->first;
key = key+10;
std::string val = it->second;
val = "X" + val;
it = mymap.erase(it);
mymap.insert(std::multimap<unsigned int, std::string>::value_type(key, val));
}
顺便说一下,因为这段代码每次都在增加密钥,所以它会一遍又一遍地处理每个元素。
答案 1 :(得分:2)
如果删除迭代器所在的条目,则可能会丢失到多图的其余部分的所有链接。
(新)解决方案:
插入新条目后,只需执行“擦除”:
std::multimap<unsigned int, std::string>::iterator it;
for (it=pr.first; it!=pr.second; ++it)
{
unsigned int key = it->first;
key = key+10;
std::string val = it->second;
val = "X" + val;
mymap.insert(std::multimap<unsigned int, std::string>::value_type(key, val));
}
mymap.erase(pr.first, pr.second);