读书人

map怎么修改键值

发布时间: 2012-03-30 17:32:09 作者: rapoo

map如何修改键值
map <int,int> test;
map <int,int> ::iterator iter = test.begin();
while(iter != test.end())
{
iter->first = ?
}

不用删除,再插入的方法!

[解决办法]
必须先删除再添加
[解决办法]
先找到该元素,把他复制到一个临时的pair,然后删除找到元素,修改pair的键值,再插入。
[解决办法]

C/C++ code
#include<iostream>#include<iterator>#include<map>using namespace std;int main(){   map <int,int> test;   test[0]=0;   test[1]=1;   test[2]=2;   map<int,int>::iterator iter = test.begin();   while(iter != test.end())   {        (int&)(iter->first) = iter->first+10;//gcc能过,vc木有试,但最好不要修改key        iter++;   }   iter = test.begin();   while(iter != test.end())   {        cout<<iter->first<<endl;        iter++;   }} 

读书人网 >C++

热点推荐