C ++ Primer (fifth edition) Study Notes _ 8_standard template library _ map ing container, _ 8_map

Source: Internet
Author: User
Tags string to number

C ++ Primer (fifth edition) Study Notes _ 8_standard template library _ map ing container, _ 8_map

C ++ Primer (fifth edition) Study Notes _ 8_standard template library _ map ing container

The element data of the map ing container is composed of a key value and a ing data. The key value and the ing data have a one-to-one ing relationship.
The data structure of the map ing container is also implemented using the red/black tree.

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 an element, key values are in the red/black tree from small to large. 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 <":" <(* iter ). second <endl; return 0 ;}

Running result:
Bomi: 96
Jack: 98.5
Kate: 97.5


2. Delete elements and repeated inserted values: erase () and clear () Methods
(1) Like the set container, map maps the erase () function of the container to delete all elements on a key value. You can also use the clear () method to clear the map ing container.
(2) For values inserted repeatedly, only the last inserted value is used.
# Include <iostream> # include <stdio. h >#include <vector >#include <map> # include <string> using namespace std; int main () {map <int, char> str; // insert an element, key values are in the red/black tree from small to large. str [25] = 'M'; str [28] = 'K'; str [10] = 'X '; str [30] = 'a'; str [25] = 'I'; // repeat the inserted value, using the last inserted value str [10] = 'y '; // repeat the inserted value, using the last inserted value cout <"original data:" <endl; for (map <int, char >:: iterator iter = str. begin (); iter! = Str. end (); iter ++) cout <(* iter ). first <":" <(* iter ). second <endl; // Delete the int n = str element whose key value is 0. erase (0); cout <"Number of deleted elements:" <n <endl; // Delete element n = str whose key value is 25. erase (25); cout <"Number of deleted elements:" <n <endl; cout <"erase () data:" <endl; for (map <int, char >:: iterator iter = str. begin (); iter! = Str. end (); iter ++) cout <(* iter ). first <":" <(* iter ). second <endl; str. clear (); cout <"clear () data:" <endl; for (map <int, char >:: iterator iter = str. begin (); iter! = Str. end (); iter ++) cout <(* iter ). first <":" <(* iter ). second <endl; return 0 ;}

Running result:
Raw data:
10: y
25: I
28: k
30:
Number of deleted elements: 0
Number of deleted elements: 1
Data after erase:
10: y
28: k
30:
Clear () data:


3. Reverse element traversal: reverse_iterator
The rbegin () and rend methods are required to indicate the start position and end position of reverse traversal.
# Include <iostream> # include <stdio. h >#include <vector >#include <map> # include <string> using namespace std; int main () {map <int, char> str; // insert an element, key values are in the red/black tree from small to large. 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 ;}

Running result:
30:
28: k
25: m
10: y


4. Search for elements: find ()
Like set, use the find () method to search for a key value and find the iterator where the return key value is located. Otherwise, the end () iterator position is returned. Because of the red/black tree structure, the search speed is extremely fast.
# Include <iostream> # include <stdio. h >#include <vector >#include <map> # include <string> using namespace std; int main () {map <int, char> str; // insert an element, key values are in the red/black tree from small to large. str [25] = 'M'; str [28] = 'K'; str [10] = 'X '; str [30] = 'a'; map <int, char >:: iterator iter; iter = str. find (28); if (iter! = Str. end () cout <(* iter ). first <":" <(* iter ). second <endl; else cout <"not found it" <endl; return 0 ;}

Running result:
28: k


5. Custom comparison functions
By default, elements are inserted in the ascending order of key values. Because the internal data structure is a red/black tree, the comparison function is consistent with the set function. There are two writing methods,
(1) If the element is not a struct, You can compile a comparison function. The following describes how to insert elements into the map in the ascending order of key values:
# 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 the element. Place the key value from large to small into the 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 ). first <":" <(* iter ). second <endl; return 0 ;}

Running result:
Running result:
30:
28: k
25: m
10: x


(2) If the element is a struct, you can directly write the comparison function in the struct.
# Include <iostream> # include <stdio. h >#include <vector >#include <map> # include <string> using namespace std; struct Info {string name; float score; // you must write an overloaded function, reload the "<" operator and customize the sorting rule bool operator <(Info a) const {// sort by score from large to small. If you want to sort data from small to large, use the ">" sign. return. score <score ;}}; int main () {// insert an element. The key value ranges from large to small and is placed in the red/black tree. map <Info, int> str; Info info; info. name = "Jack"; info. score = 60; str [info] = 25; info. name = "Bomi"; info. score = 80; str [info] = 10; info. name = "Peti"; info. score = 66.5; str [info] = 30; 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 ;}
Running result:
10: Bomi 80
30: Peti 66.5
25: Jack 60


6. map writing for character ing numbers
It is time-consuming to separate the numbers and use the remainder mathematical operation. Using numbers as strings and map ing makes it easy to separate numbers.
# Include <iostream> # include <stdio. h> # include <vector> # include <map> # include <string> using namespace std; int main () {// insert an element, put the key value from big to small into the 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 preceding value assignment statement can be simplified with the loop code for (int j = 0; j <10; j ++) m ['0' + j] = j; */string a; a = "6234"; int sum = 0; for (int I = 0; I <. length (); I ++) sum + = str [a [I]; cout <"sum of each number:" <sum <endl; sum = str [a [0]; for (int I = 1; I <. length (); I ++) {sum * = 10; sum + = str [a [I];} cout <"string converted to a number: "<sum <endl ;}

Running result:
Sum of numbers: 15
String to number: 6234


7. map Writing of digit ing characters
# Include <iostream> # include <stdio. h> # include <vector> # include <map> # include <string> using namespace std; int main () {// insert an element, put the key value from large to small into the 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 preceding value assignment statement can be simplified with the loop code for (int j = 0; j <10; j ++) m [j] = '0' + j; */int n = 7; string single; cout < "Convert the unit number to a string:" <single + str [n] <endl; int num = 6234; string s; for (int I = num; I! = 0; I/= 10) {s. insert (s. begin (), str [I % 10]);} cout <"Multi-digit conversion to string:" <s <endl; return 0 ;}

Running result:
Convert unit number to string: 7
Multi-digit conversion to string: 6234

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.