首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

map怎么修改键值

2012-03-30 
map如何修改键值map int,int testmap int,int ::iteratoriter test.begin()while(iter ! test.en

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++;   }} 

热点排行