map 插入數據的方式局別
阿新 • • 發佈:2017-06-17
tor original light end 插入 space int ace r++
#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;
這種方式就可以覆蓋鍵對應的值
map 插入數據的方式局別