Z
zs0723
I need to changed the value of a key in std::map, i have written the
following code :
#include <map>
#include <iostream>
#include <string>
using namespace std;
int main()
{
typedef map<string,string> TABLE;
typedef TABLE::iterator ITERATOR;
TABLE table;
table.insert(TABLE::value_type("zs","victor"));
if(table.insert(TABLE::value_typezs","xiao v")).second) // this
will failed , i can't overwirte the exist key's value
cout<<"insert success\n";
else
cout<<"insert failied\n";
// but use like array 's [] , it's ok
// table["zs"]="victor";
// table["zs"]="xiao v";
for(ITERATOR itr=table.begin();itr != table.end() ; ++itr)
{
cout<<itr->first <<":"<<itr->second;
cout<<endl;
}
cout<<std::endl;
}
My question is :
Why insert method can't overwrite the value of the existing key.
Thanks
following code :
#include <map>
#include <iostream>
#include <string>
using namespace std;
int main()
{
typedef map<string,string> TABLE;
typedef TABLE::iterator ITERATOR;
TABLE table;
table.insert(TABLE::value_type("zs","victor"));
if(table.insert(TABLE::value_typezs","xiao v")).second) // this
will failed , i can't overwirte the exist key's value
cout<<"insert success\n";
else
cout<<"insert failied\n";
// but use like array 's [] , it's ok
// table["zs"]="victor";
// table["zs"]="xiao v";
for(ITERATOR itr=table.begin();itr != table.end() ; ++itr)
{
cout<<itr->first <<":"<<itr->second;
cout<<endl;
}
cout<<std::endl;
}
My question is :
Why insert method can't overwrite the value of the existing key.
Thanks