Add map data
1 // add data in six ways 2 mp.insert ({ 0 , " Map ZERO " }); // Use} { . 3 mp.insert (pair < int , String > ( 1 , " Map One " )); / / use pair . 4 mp.insert (the make_pair ( 2 , " Map TWO " )); // use the make_pair . 5 mp.insert (the make_pair < int , String > ( . 3 , " Map Three " )); . 6 mp.insert (Map < int , String > :: the value_type ( . 4 , " Map Four " )); // Use the value_type . 7 MP [ . 5 ] = " Map Five " ; // with the key, the value of direct assignment
Test code:
1 #include<bits/stdc++.h> 2 using namespace std; 3 void show(map<int,string>& mp){ 4 map<int,string>::iterator iter=mp.begin(); 5 while(iter!=mp.end()){ 6 cout<<iter->first<<" "<<iter->second<<endl; 7 iter++; 8 } 9 } 10 int main() 11 { 12 //Configured to Map 13 is Map < int , String > MP; // Construct a Map 14 15 // add data in six ways 16 mp.insert ({ 0 , " Map ZERO " }); // Use} { . 17 mp.insert (pair < int , String > ( . 1 , " Map One " )); // use pair 18 is mp.insert (the make_pair ( 2 , " Map TWO " )); // use the make_pair . 19 mp.insert (the make_pair < int , String > ( . 3 , " Map Three " )); 20 is mp.insert (Map < int , String > :: the value_type ( . 4 , " Map Four " )); // Use the value_type 21 is MP [ . 5 ] = " Map Five " ; // directly with the key, value assignment 22 is 23 is 24 25 Show (MP); // output Map 26 is return 0 ; 27 }
operation result:
1 0 map zero 2 1 map one 3 2 map two 4 3 map three 5 4 map four 6 5 map five