Complicated map initialization
1 // configuration bit map like complex key and value is any value 2 map < String , Vector < String >> MP; . 3 map < int , map < String , Vector < String >>> MPS;
Test code:
1 #include<bits/stdc++.h> 2 using namespace std; 3 void show(map<string,vector<string> >& mp) 4 { 5 map<string,vector<string> >::iterator iter=mp.begin(); 6 while(iter!=mp.end()) 7 { 8 cout<<iter->first<<" :"; 9 for(int i=0;i<(int)iter->second.size();i++){ 10 cout<<iter->second[i]<<" ,"; 11 } 12 cout<<endl; 13 iter++; 14 } 15 } 16 int main() 17 { 18 19 //构造 map 复杂一点的 20 map<string,vector<string > > mp; 21 22 map<int,map<string,vector<string> > > mps; 23 mps[. 1 ] [ " ab & " ] .push_back ( " ab & ab & .... " ); 24 COUT << MPS [ . 1 ] [ " ab & " ] [ 0 ] << endl; 25 26 is // INSERT method and array method ( direct assignment) Comparative 27 28 @ 1: use direct assignment even key already exists, using array will be updated directly corresponding to the key value. 29 MP [ " ABC " ] .push_back ( " String ZERO " ); 30 MP [ " ABC " ] .push_back ( "); 31 is 32 33 is @ 2: Using the method can not cover the insert Insert, if the key already exists, the insert fails 34 is IF (mp.find ( " ABCD " ) mp.end == ()) { // absent 35 Vector < String > VS; 36 vs.push_back ( " String One " ); 37 [ vs.push_back ( " String One One " ); 38 is mp.insert (the make_pair ( " ABCD " , VS)); // 39 } 40 the else { //存在,就放进去 41 mp["abcd"].push_back("string two"); 42 mp["abcd"].push_back("string two two"); 43 } 44 show(mp); 45 return 0; 46 } 47 ab ab.... 48 abc :string zero ,string zero zero , 49 abcd :string one ,string one one ,
operation result:
1 ab ab.... 2 abc :string zero ,string zero zero , 3 abcd :string one ,string one one ,