Detecting whether the container is empty map:
. 1 #include <the iostream> 2 #include <Map> . 3 #include < String > . 4 the using namespace STD; . 5 int main () . 6 { . 7 // detect whether the container is empty . 8 Map < String , String > mapText; . 9 COUT < <mapText.empty () << endl; // mapText.empty () == 0 empty mapText.empty () = 0 non-empty! 10 . 11 mapText [ " small A " ] = " A " ; // assignment 12 mapText [ " Small B " ] = " B " ; // Assignment 13 is mapText [ " Small C " ] = " C " ; // Assignment 14 COUT << mapText.empty () << endl; // mapText.empty ( ) == 0 empty mapText.empty () = 0 non-empty! 15 16 System ( " PAUSE " ); . 17 return 0 ; 18 is }
Repetition assignment, the value is replaced by:
1 #include <iostream> 2 #include<map> 3 #include<string> 4 using namespace std; 5 int main() 6 { 7 map<string, string>mapText; 8 mapText["小A"] = "A"; //赋值 9 mapText["小A"] = "B"; //重复赋值 10 cout << mapText["小A"] << endl; 11 12 system("pause"); 13 return 0; 14 }
Evaluating key exists, if there is no re-assignment:
. 1 #include <the iostream> 2 #include <Map> . 3 #include < String > . 4 the using namespace STD; . 5 int main () . 6 { . 7 Map < String , String > mapText; . 8 mapText [ " Small A " ] = " A " ; // assignment 9 // first detection key exists, if there is no assignment 10 IF (mapText.count ( " small A " ) == 0 ) // COUNT == 0 absent present count == 1 . 11 { 12 is mapText [ " Small A " ] = " B " ; // repeat assignment 13 is } 14 COUT << mapText [ " Small A " ] << endl; 15 16 System ( " PAUSE " ); . 17 return 0 ; 18 is }
map loop through:
map.begin () pointing to the first element of the map
map.end () points to the last element of the map after the address
1 #include <iostream> 2 #include<map> 3 #include<string> 4 using namespace std; 5 int main() 6 { 7 map<string, string>mapText; 8 mapText["小A"] = "A"; //赋值 9 mapText["小B"] = "B"; //赋值 10 mapText["小C1"] = "C"; //赋值 11 mapText["小C2"] = "C"; //赋值 12 for (map<string, string>::iterator itor = mapText.begin(); itor != mapText.end(); ++itor) 13 { 14 cout << "key = " << itor->first << ", value = " << itor->second << endl; 15 16 } 17 system("pause"); 18 return 0; 19 }
map through the "key" to delete the key:
1 #include <iostream> 2 #include<map> 3 #include<string> 4 using namespace std; 5 int main() 6 { 7 map<string, string>mapText; 8 mapText["小A"] = "A"; //赋值 9 mapText["小B"] = "B"; //赋值 10 mapText["小C1"] = "C"; //赋值 11 mapText["小C2"] = "C"; //赋值 12 cout << "删除前:" << endl; 13 for (map<string, string>::iterator itor = mapText.begin(); itor != mapText.end(); ++itor) 14 { 15 cout << "key = " << itor->first << ", value = " << itor->second << endl; 16 17 } 18 //删除小B 19 mapText.erase("小B"); 20 cout << "删除后:" << endl; 21 for (map<string, string>::iterator itor = mapText.begin(); itor != mapText.end(); ++itor) 22 { 23 cout << "key = " << itor->first << ", value = "Itor- <<> SECOND << endl; 24 25 } 26 is mapText.erase ( " Small B " ); // small B is absent, erase is not given 27 System ( " PAUSE " ); 28 return 0 ; 29 }
map through the "value" delete key-value pairs, the first to write a wrong usage to note here:
1 #include <iostream> 2 #include<map> 3 #include<string> 4 using namespace std; 5 int main() 6 { 7 map<string, string>mapText; 8 mapText["小A"] = "A"; //赋值 9 mapText["小B"] = "B"; //赋值 10 mapText["小C1"] = "C"; //赋值 11 mapText["小C2"] = "C"; //赋值 12 cout << "删除前:" << endl; 13 for (map<string, string>::iterator itor = mapText.begin(); itor != mapText.end(); ++itor) 14 { 15 cout << "key = " << itor->first << " , Value = " << itor-> SECOND << endl; 16 . 17 } 18 is // delete the C element is 19 @ WRONG: 20 is for (Map < String , String > :: = mapText.begin Iterator itor ();! itor mapText.end = (); ++ itor) 21 is { 22 is IF ((itor-> SECOND) == " C " ) 23 is { 24 mapText.erase (itor); 25 } 26 is } 27 28 COUT << "Delete After:" << endl; 29 for (map<string, string>::iterator itor = mapText.begin(); itor != mapText.end(); ++itor) 30 { 31 cout << "key = " << itor->first << ", value = " << itor->second << endl; 32 33 } 34 system("pause"); 35 return 0; 36 }
Error reason: itor pointer after the failure of the element is deleted, back to the for statement with mapText.end () the comparison error.
map through the "value" delete key-value pairs, the correct usage:
1 #include <iostream> 2 #include<map> 3 #include<string> 4 using namespace std; 5 int main() 6 { 7 map<string, string>mapText; 8 mapText["小A"] = "A"; //赋值 9 mapText["小B"] = "B"; //赋值 10 mapText["小C1"] = "C"; //赋值 11 mapText["小C2"] = "C"; //赋值 12 cout << "删除前:" << endl; 13 for (map<string, string>::iterator itor = mapText.begin(); itor != mapText.end(); ++itor) 14 { 15 cout << "key = " << itor->first << " , Value = " << itor-> SECOND << endl; 16 . 17 } 18 is // remove elements of the value of C 19 @ correct usage: 20 is for (Map < String , String > :: = mapText.begin Iterator itor (); itor mapText.end = ();! / * ++ itor * / ) 21 is { 22 is IF ((itor-> SECOND) == " C " ) 23 is { 24 itor = mapText.erase (itor); 25 } 26 is the else 27 { 28 ++itor; 29 } 30 } 31 32 33 cout << "删除后:" << endl; 34 for (map<string, string>::iterator itor = mapText.begin(); itor != mapText.end(); ++itor) 35 { 36 cout << "key = " << itor->first << ", value = " << itor->second << endl; 37 38 } 39 system("pause"); 40 return 0; 41 }
Delete map of the first element
mapText.erase(mapText.begin());