Detailed description of map usage in STL in C ++, stlmap
Map is an associated container of STL, which provides one-to-one (the first can be called a keyword, each keyword can only appear once in map, and the second can be called the value of this keyword) because of this feature, it is possible to provide a quick channel for programming when we process one-to-one data. Here, we will talk about the organization of map internal data. Within map, we will build a red/black tree (a non-strictly balanced binary tree), which has the ability to automatically sort data, therefore, all the data in the map is ordered, and we will see the benefits of ordering later. The following is an example of one-to-one data ing. For example, in a class, each student's student ID has a one-to-one ing relationship with his name. This model may be easily described using map. Obviously, the student ID is described using int, the name is described by a string (this article does not use char * to describe the string, but uses string in STL). The following map description code is provided: Map <int, string> mapStudent; 1. map constructor map provides a total of six constructor functions, which involve the memory distributor and are omitted from the table. Below we will come into contact with some map constructor methods, here, we usually use the following method to construct a map: Map <int, string> mapStudent; 2. after the map container is constructed, We can insert data into the container. There are three ways to insert data: the first one is to insert pair data using the insert function. The following is an example (although the code below is handwritten, it should be compiled in VC and GCC, you can run the following command to check the effect. In VC, add this statement to block the 4786 warning # pragma warning (disable: 4786 )) # include <map> # include <string> # include <iostream> Using namespace std; Int main () {Map <int, string> mapStudent; mapStudent. insert (pair <int, string> (1, "student_one"); mapStudent. insert (pair <int, string> (2, "student_two"); mapStudent. insert (pair <int, string> (3, "s Tudent_three "); map <int, string >:: iterator iter; for (iter = mapStudent. begin (); iter! = MapStudent. end (); iter ++) {Cout <iter-> first <"" <iter-> second <end ;}} type 2: use the insert function to insert value_type data. The following example shows # include <map> # include <string> # include <iostream> Using namespace std; Int main () {Map <int, string> mapStudent; mapStudent. insert (map <int, string >:: value_type (1, "student_one"); mapStudent. insert (map <int, string >:: value_type (2, "student_two"); mapStudent. insert (map <int, string >:: value_type (3, "student_three"); map <int, string >:: iterator iter; for (iter = mapStudent. begin (); iter! = MapStudent. end (); iter ++) {Cout <iter-> first <"" <iter-> second <end: insert data Using arrays. The following example shows # include <map> # include <string> # include <iostream> Using namespace std; Int main () {Map <int, string> mapStudent; mapStudent [1] = "student_one"; mapStudent [2] = "student_two"; mapStudent [3] = "student_three"; map <int, string> :: iterator iter; for (iter = mapStudent. begin (); iter! = MapStudent. end (); iter ++) {Cout <iter-> first <"<iter-> second <end, although data can be inserted, there are differences between them. Of course, the first and second methods are the same in terms of performance, and the insert function is used to insert data, the concept of uniqueness of a set is involved in data insertion. When a map contains this keyword, the insert operation cannot insert data, but the array method is different, it can overwrite the value corresponding to the previous keyword and use a program to describe mapStudent. insert (map <int, string >:: value_type (1, "student_one"); mapStudent. insert (map <int, string >:: value_type (1, "student_two"); after these two statements are executed, the value of the 1 keyword in map is "student_one ", article 2 The insert statement does not take effect, so this involves how we know whether the insert statement is successfully inserted. We can use pair to determine whether the insert statement is successful. The program is as follows: Pair <map <int, string >:: iterator, bool> Insert_Pair; Insert_Pair = mapStudent. insert (map <int, string >:: value_type (1, "student_one"); we use the second pair variable to determine whether the insert is successful, its first variable returns a map iterator. If the insert is successful, Insert_Pair.second should be true; otherwise, it is false. The following code is provided to demonstrate whether the insertion is successful. # include <map> # include <string> # include <iostream> Using namespace std; Int main () {Map <int, string> mapStudent; Pair <map <int, string >:: iterator, bool> Insert_Pair; Insert_Pair = mapStudent. insert (pair <int, string> (1, "student_one"); If (Insert_Pair.second = true) {Cout <"Insert Successfully" <endl ;} else {Cout <"Insert Failure" <endl;} Insert_Pair = mapStudent. insert (pair <int, String> (1, "student_two"); If (Insert_Pair.second = true) {Cout <"Insert Successfully" <endl ;} else {Cout <"Insert Failure" <endl;} map <int, string >:: iterator iter; for (iter = mapStudent. begin (); iter! = MapStudent. end (); iter ++) {Cout <iter-> first <"" <iter-> second <end ;}} you can use the following program, check the effect of inserting an array into data overwrite # include <map> # include <string> # include <iostream> Using namespace std; Int main () {Map <int, string> mapStudent; mapStudent [1] = "student_one"; mapStudent [1] = "student_two"; mapStudent [2] = "student_three"; map <int, string> :: iterator iter; for (iter = mapStudent. begin (); iter! = MapStudent. end (); iter ++) {Cout <iter-> first <"<iter-> second <end ;}} 3. the map size inserts data into the map. How do we know how much data has already been inserted? The size function can be used as follows: Int nSize = mapStudent. size (); 4. there are also three methods for data traversal. The first method is to traverse the map: the application's forward iterator, which is everywhere in the above example. The second method is to skip the list: the application's reverse iterator, the following is an example to illustrate the effect. Run the Program # include <map> # include <string> # include <iostream> Using namespace std; Int main () {Map <int, string> mapStudent; mapStudent. insert (pair <int, string> (1, "st Udent_one "); mapStudent. insert (pair <int, string> (2, "student_two"); mapStudent. insert (pair <int, string> (3, "student_three"); map <int, string >:: reverse_iterator iter; for (iter = mapStudent. rbegin (); iter! = MapStudent. rend (); iter ++) {Cout <iter-> first <"" <iter-> second <end ;}} method 3: array method, program Description: # include <map> # include <string> # include <iostream> Using namespace std; Int main () {Map <int, string> mapStudent; mapStudent. insert (pair <int, string> (1, "student_one"); mapStudent. insert (pair <int, string> (2, "student_two"); mapStudent. insert (pair <int, string> (3, "student_three"); int nSize = mapStudent. size ()/ /The error here should be for (int nIndex = 1; nIndex <= nSize; nIndex ++) // by rainfish for (int nIndex = 0; nIndex <nSize; nIndex ++) {Cout <mapStudent [nIndex] <end ;}} 5. data Search (including determining whether this keyword exists in map) Here we will understand the advantages of map in ensuring order during data insertion. There are many methods to determine whether a data (keyword) appears in map. Although the title here is a data search, there will be a lot of basic map usage here. Three data search methods are provided here. The first method is to use the count function to determine whether a keyword exists. The disadvantage is that the data location cannot be located. Due to the characteristics of map, one-to-one ing relationship is provided, it is determined that the return value of the count function has only two values, either 0 or 1. The result is, of course, 1 is returned. The second type: Use the find function to locate the data location, it returns an iterator. When the data appears, it returns the iterator where the data is located. If there is no data to be searched in the map, the iterator it returns is equal to the iterator returned by the end function, program description # include <map> # include <string> # include <iostream> Using namespace std; Int main () {Map <int, string> mapStudent; mapStudent. insert (pair <int, string> (1, "student_one"); mapStudent. insert (pair <int, str Ing> (2, "student_two"); mapStudent. insert (pair <int, string> (3, "student_three"); map <int, string >:: iterator iter; iter = mapStudent. find (1); if (iter! = MapStudent. end () {Cout <"Find, the value is" <iter-> second <endl ;} else {Cout <"Do not Find" <endl ;}} method 3: This method is used to determine whether data is present. It seems stupid. However, I plan to explain the Lower_bound function usage here. This function is used to return the lower bound of the keyword to be searched (an iterator) Upper_bound function usage, this function is used to return the upper bound (an iterator) of the keyword to be searched. For example, if 1, 2, 3, and 4 have been inserted in map, if lower_bound (2) is returned, 2 is returned, in upper-bound (2), the 32.16_range function returns a pair. The first variable in pair is the iterator returned by Lower_bound, and the second iterator in pair is the iterator returned by Upper_bound, if the two iterators are the same, it means that Keyword, program description # include <map> # include <string> # include <iostream> Using namespace std; Int main () {Map <int, string> mapStudent; mapStudent [1] = "student_one"; mapStudent [3] = "student_three"; mapStudent [5] = "student_five"; map <int, string >:: iterator iter; iter = mapStudent. lower_bound (2); {// The iterator Cout <iter-> second <endl;} iter = mapStudent. lower_bound (3); {// return the iterator Cout <iter-> second <endl;} it for the lower bound 3. Er = mapStudent. upper_bound (2); {// The returned iterator Cout <iter-> second <endl;} iter = mapStudent. upper_bound (3); {// The returned iterator Cout <iter-> second <endl;} Pair <map <int, string >:: iterator, map <int, string >:: iterator> mapPair; mapPair = mapStudent. pai_range (2); if (mapPair. first = mapPair. second) {cout <"Do not Find" <endl;} Else {Cout <"Find" <endl;} mapPair = mapStudent. pai_range (3); if (mapPair. first = MapPair. second) {cout <"Do not Find" <endl;} Else {Cout <"Find" <endl ;}} 6. the clear () function can be used to clear the data in the map to determine whether there is data in the map. The empty () function can be used, if it returns true, it indicates that map7. the data is null. Here the erase function is used to delete the data. It has three overloaded functions, the following describes their usage in detail in the example # include <map> # include <string> # include <iostream> Using namespace std; Int main () {Map <int, string> mapStudent; mapStudent. insert (pair <int, string> (1, "student_one"); mapStudent. insert (pair <int, string> (2, "stu Dent_two "); mapStudent. insert (pair <int, string> (3, "student_three"); // If You Want To demonstrate the output effect, select one of the following, you can see better results. // if you want to delete 1, use the iterator to delete map <int, string >:: iterator iter; iter = mapStudent. find (1); mapStudent. erase (iter); // if you want to delete 1, delete Int n = mapStudent with the keyword. erase (1); // If deleted, 1 is returned; otherwise, 0 is returned. // use the iterator to delete parts. // The Code clears the whole map. mapStudent. earse (mapStudent. begin (), mapStudent. end (); // delete parts. It is also a STL feature. The delete interval is a set that is opened after the front and closed. // print and input the traversal code. Outbound} 8. other functions are used here, such as swap, key_comp, value_comp, and get_allocator. I feel that these functions are not used in programming. If you are interested, you can study them yourself. sorting here is a relatively advanced usage. Sorting is a problem. By default, STL uses smaller numbers for sorting. The above Code does not have any problems in sorting, because the above keyword is int type, it supports less than number calculation. In some special cases, for example, if the keyword is a struct, a problem occurs when sorting is involved because it is not less than number operation, insert and other functions cannot be used during compilation. The following two methods are provided to solve this problem. The first one is less than the number overload. The program example # include <map> # include <string> Using namespace std; typedef struct tagStudentInfo {Int nID; String strName;} StudentInfo, * PStudentIn Fo; // student information Int main () {int nSize; // map the score map with Student Information <StudentInfo, int> mapStudent; map <StudentInfo, int >:: iterator iter; studentInfo studentInfo; studentInfo. nID = 1; studentInfo. strName = "student_one"; mapStudent. insert (pair <StudentInfo, int> (studentInfo, 90); studentInfo. nID = 2; studentInfo. strName = "student_two"; mapStudent. insert (pair <StudentInfo, int> (studentInfo, 80); for (iter = mapStudent. be Gin (); iter! = MapStudent. end (); iter ++) cout <iter-> first. nID <endl <iter-> first. strName <endl <iter-> second <endl;} The above program cannot be compiled. If you need to reload the code smaller than the number, it will be OK, as shown below: Typedef struct tagStudentInfo {Int nID; string strName; Bool operator <(tagStudentInfo const & _ A) const {// This function specifies the sorting policy, which is sorted by nID. If nID is equal, sort by strName If (nID <_. nID) return true; If (nID = _. nID) return strName. compare (_. strName) <0; Return false ;}} StudentInfo, * PS TudentInfo; // The second type of student information: the application of the imitation function. At this time, the struct does not directly overload the code. The program description # include <map> # include <string> Using namespace std; typedef struct tagStudentInfo {Int nID; String strName;} StudentInfo, * PStudentInfo; // student information Classs sort {Public: Bool operator () (StudentInfo const & _, studentInfo const & _ B) const {If (_. nID <_ B. nID) return true; If (_. nID = _ B. nID) return _. strName. compare (_ B. strName) <0; Return false ;}}; Int main () {// Map the score Map with Student Information <StudentInfo, int, sort> mapStudent; StudentInfo studentInfo; studentInfo. nID = 1; studentInfo. strName = "student_one"; mapStudent. insert (pair <StudentInfo, int> (studentInfo, 90); studentInfo. nID = 2; studentInfo. strName = "student_two"; mapStudent. insert (pair <StudentInfo, int> (studentInfo, 80);} 10. in addition, because STL is a unified whole, many map usage is combined with other things in STL. For example, in sorting, less signs are used by default, that is, less <>, if you want There are many things involved in small sorting, which cannot be described here. It should also be noted that because of its internal order in map, it is ensured by the red and black trees, the time complexity of many function execution is log2N. If map functions can be used, STL Algorithm can also implement this function. We recommend that you use the built-in Function map, which is more efficient. Next, let's talk about the features of map in space. Otherwise, it is estimated that you will sometimes be depressed when using it. Because each map data corresponds to a node on the red/black tree, when this node does not save your data, it occupies 16 bytes, a parent node pointer, left and right child pointer, and an enumeration value (marked as red and black, this is equivalent to the balance factor in the binary tree.) I think you should know that these areas are very memory-intensive ......
Reprinted please declare: http://www.cnblogs.com/fnlingnzb-learner/p/5833051.html