C + + Primer (Fifth edition) learning Note _8_ Standard Template Library _map mapping container
The element data of a map mapping container consists of a key value and a mapping data, which has a relationship of one by one mappings between the key value and the mapping data.
The data structure of the map mapping container is also implemented using red and black trees.
1. Map creation, element insertion, and traversal access
#include <iostream> #include <stdio.h> #include <vector> #include <map> #include <string> Using namespace Std;int Main () { map<string, float> str; Insert elements, key values from small to large into the red black tree str["Jack" = 98.5; str["Bomi"] = 96.0; str["Kate" = 97.5; For (map<string, float>::iterator iter = Str.begin (); ITER! = Str.end (); iter++) cout << (*iter). First &L t;< ":" << (*iter). Second << Endl; return 0;}
Operation Result:
bomi:96
jack:98.5
kate:97.5
2. Delete element and repeat Insert value: Erase () and clear () method
(1) As with the set container, the map mapping container's erase () delete element function, you can delete all elements on a key value. You can also use the clear () method to empty the map mapping container.
(2) for repeated insertions, only the last inserted value is taken
#include <iostream> #include <stdio.h> #include <vector> #include <map> #include <string> Using namespace Std;int Main () {map<int, char> str; Insert elements, key values from small to large into the red black tree str[25] = ' m '; STR[28] = ' k '; STR[10] = ' x '; STR[30] = ' a '; STR[25] = ' I '; The value of the repeated insertion, using the last inserted value str[10] = ' Y '; Repeat the inserted value with the last inserted value cout << "raw data:" << Endl; For (map<int, char>::iterator iter = Str.begin (); ITER! = Str.end (); iter++) cout << (*iter). First <& Lt ":" << (*iter). Second << Endl; Delete the element with a key value of 0 int n = str.erase (0); cout << "delete element:" << n << endl;//Delete element with key value 25 n = str.erase; cou T << "Number of deleted elements:" << n << Endl; cout << "Erase () after data:" << Endl; For (map<int, char>::iterator iter = Str.begin (); ITER! = Str.end (); iter++) cout << (*iter). First <& Lt ":" << (*iter). Second << Endl; Str.clear (); cout << "Clear () after data:" ≪< Endl; For (map<int, char>::iterator iter = Str.begin (); ITER! = Str.end (); iter++) cout << (*iter). First <& Lt ":" << (*iter). Second << Endl; return 0;}
Operation Result:
Raw data:
10:y
25:i
28:k
30:a
Number of deleted elements: 0
Number of deleted elements: 1
Erase () after data:
10:y
28:k
30:a
Clear () after data:
3, Element reverse traversal: reverse_iterator
The Rbegin () method and the Rend method are required to indicate the starting and ending positions of the reverse traversal.
#include <iostream> #include <stdio.h> #include <vector> #include <map> #include <string> Using namespace Std;int Main () { map<int, char> str; Insert elements, key values from small to large into the red black tree str[25] = ' m '; STR[28] = ' k '; STR[10] = ' x '; STR[30] = ' a '; For (map<int, char>::reverse_iterator iter = Str.rbegin (); ITER! = Str.rend (); iter++) cout << (*iter). First << ":" << (*iter). Second << Endl; return 0;}
Operation Result:
30:a
28:k
25:m
10:y
4. Searching for elements: Find ()
As with set, the Find () method is used to search for a key value and the location of the iterator where the return key value is found. Otherwise, the end () iterator position is returned. Because of the red and black tree structure, the search speed is very fast.
#include <iostream> #include <stdio.h> #include <vector> #include <map> #include <string> Using namespace Std;int Main () { map<int, char> str; Insert elements, key values from small to large into the red black tree str[25] = ' m '; STR[28] = ' k '; STR[10] = ' x '; STR[30] = ' a '; Map<int, Char>::iterator iter; ITER = Str.find (+); if (iter = Str.end ()) cout << (*iter). First << ":" << (*iter). Second << Endl; else cout << "not found it" << Endl; return 0;}
Operation Result:
28:k
5. Custom comparison function
By default, elements are inserted in the order of the key values from small to large. Because internal data structures are red-black trees, writing comparison functions is consistent with set. There are two ways to write,
(1) If the element is not a struct, you can write a comparison function. The following key values are inserted into the map by the small order of the Boulevard:
#include <iostream> #include <stdio.h> #include <vector> #include <map> #include <string> Using namespace std;struct mycomp{ bool Operator (const int& A, const int& b) { return a > B; }};int Main () { //Insert element, key value from large to small into red black tree map<int, char, mycomp> str; STR[25] = ' m '; STR[28] = ' k '; STR[10] = ' x '; STR[30] = ' a '; For (Map<int, char, mycomp>::iterator iter = Str.begin (); ITER! = Str.end (); iter++) cout << (*iter). Firs T << ":" << (*iter). Second << Endl; return 0;}
Operation Result:
Operation Result:
30:a
28:k
25:m
10:x
(2) If the element is a struct, then it is possible to write the comparison function directly inside the structure body.
#include <iostream> #include <stdio.h> #include <vector> #include <map> #include <string> Using namespace std;struct info{ string name; float score; You must write overloaded functions, overload the "<" operator, and customize the collation bool operator < (Info a) const { //by score from large to small. If you want to arrange from small to large, use ">", return A.score < score; }}; int main () { //Insert element, key value from large to small into red black tree map<info, int> str; Info info; Info.name = "Jack"; Info.score =; Str[info] =; Info.name = "Bomi"; Info.score = n; Str[info] = ten; Info.name = "Peti"; Info.score = 66.5; Str[info] =; For (map<info, int>::iterator iter = Str.begin (); ITER! = Str.end (); iter++) { cout << (*iter). Second << ":"; cout << ((*iter). First). Name << "" << ((*iter). First). Score << Endl; } return 0;}
Operation Result:
10:bomi 80
30:peti 66.5
25:jack 60
6. Map notation for character mapping numbers
It is time-consuming to separate the numbers and take the mathematical operation of the remainder. The digital separation is easily realized by using the mapping function of map as a string.
#include <iostream> #include <stdio.h> #include <vector> #include <map> #include <string> Using namespace Std;int Main () { //Insert element, key value from large to small into red black tree Map<char, int> str; str[' 0 '] = 0; str[' 1 '] = 1; str[' 2 '] = 2; str[' 3 '] = 3; str[' 4 '] = 4; str[' 5 '] = 5; str[' 6 '] = 6; str[' 7 '] = 7; str[' 8 '] = 8; str[' 9 '] = 9; /*//the above assignment statement can be simplified with circular code for (int j = 0; J <; J + +) m[' 0 ' + j] = J; */ string A; A = "6234"; int sum = 0; for (int i = 0; i < a.length (); i++) sum + = Str[a[i]]; cout << "Sum of each number:" << sums << Endl; sum = str[a[0]]; for (int i = 1; i < a.length (); i++) { sum *=; Sum + = Str[a[i]]; } cout << "string conversion to numbers:" << sum << Endl;}
Operation Result:
The and of each number are: 15
String converted to Number: 6234
7. Map notation for digital mapping characters
#include <iostream> #include <stdio.h> #include <vector> #include <map> #include <string> Using namespace Std;int Main () { //Insert element, key value from large to small into red black tree map<int, char> str; Str[0] = ' 0 '; STR[1] = ' 1 '; STR[2] = ' 2 '; STR[3] = ' 3 '; STR[4] = ' 4 '; STR[5] = ' 5 '; STR[6] = ' 6 '; STR[7] = ' 7 '; STR[8] = ' 8 '; STR[9] = ' 9 '; /*//The above assignment statement can be simplified with circular code for (int j = 0; J <; J + +) m[j] = ' 0 ' + j; */ int n = 7; string single; cout << "unit number converted to string:" << single + str[n] << Endl; int num = 6234; string S; for (int i = num; I! = 0; I/=) { S.insert (S.begin (), str[i%]); } cout << "multi-digit conversion to string:" << s << endl; return 0;}
Operation Result:
Unit number converted to string: 7
Convert multiple digits to strings: 6234
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
C + + Primer (Fifth edition) learning Note _8_ Standard Template Library _map mapping container