#include<map> #include<iostream> usingnamespace std; int main() { map <int, int> m1, m2, m3; map <int,int>::iterator m1_Iter; m1.insert( pair <int, int>(1, 10 ) ); m1.insert ( pair <int,int> ( 2, 20 ) ); m1.insert ( pair <int,int> ( 3, 30 ) ); m2.insert ( pair <int,int> ( 10, 100 ) ); m2.insert ( pair <int,int> ( 20, 200 ) ); m3.insert ( pair <int,int> ( 30, 300 ) ); cout << "The original map m1is:"; for ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ ) cout << " "<<m1_Iter->second; cout << "."<< endl;return 0;}
map
m1.insert( pair <string, int>("str", 10 ) );
m1.insert( pair <string, int>("str1", 11 ) );
m1.insert( pair <string, int>("str", 12 ) );
不会插入键相同的值,也就是不会覆盖
m1["sa"]=2;
m1["sa1"]=10;
这种方式就可以覆盖键对应的值
时间: 2024-12-27 20:48:30