[C ++ STL] Things About C ++ STL-map container

Source: Internet
Author: User
Tags map data structure

Map container

1) concept: map is a container used to store data and retrieve data from a dataset. Its data composition includes two items: one is its data value and the other is the keyword used for sorting. The keyword is unique and used to sort data automatically. The data value of each element is irrelevant to the keyword and can be changed directly.

[Key] The internal structure adopts rb_tree (Red/black tree ). Search complexity:O (log2n)


The multimap and map features are the same. The only difference is that repeated key values are allowed !!!

2) Use

Header file to be loaded: # include <map>
Using namespace STD;
Template prototype:
Template <
Class key ,// Data Type of the keyword
Class type ,// Data Type of the data value
Class traits = less <key> ,// Compare the two meta-elements to determine whether they are in the map The relative position in the container. It is optional and its default value is less <key>
Class Allocator = Allocator <pair <const key, type> // Represents a storage management device. It is optional and its default value is Allocator <pair <const key, type>
>

3) map container features:
(1) It is an associated container and its size can be changed. It can improve the Data Reading Capability Based on keywords.
(2) provides a two-way positioner to read and write data.
(3)Sorted by keywords and a comparison Function.
(4)All keywords are unique..
(5) a template that provides a general and independent data type.

4) for more information about map, see resources.

Download STL map resources

5) combined with the map method, a comprehensive test is provided.Code:

# Include <cstdlib> # include <map> # include <iostream> using namespace STD; Map <int, char> CTR; int print_one_item (Map <int, char> :: const_iterator CP) // print a map element {cout <"(" <CP-> first <"," <CP-> second <") "; return 0;} void test_0000_range () // test pai_range () function {// pair the first iterator returns the first element greater than or equal to the given keyword. // The second iterator returns the first element greater than the given keyword. Pair <Map <int, char >:: const_iterator, Map <int, char >:: const_iterator> P; P = Ctr. Range _range (2); If (P. First! = Ctr. end () {cout <"the first element which key> = 2 is:"; // cout <"(" <p. first-> first <"," <p. first-> second <")"; print_one_item (P. first); // call the subaccount Program To print a cout <Endl;} If (P. Second! = Ctr. end () {cout <"the first element which key> 2 is:"; cout <"(" <p. second-> first <"," <p. second-> second <")"; cout <Endl ;}} void creat_map () {Ctr. insert (pair <int, char> (1, 'A'); Ctr. insert (pair <int, char> (2, 'B'); Ctr. insert (pair <int, char> (3, 'B'); Ctr. insert (pair <int, char> (4, 'C'); Ctr. insert (pair <int, char> (5, 'D'); Ctr. insert (pair <int, char> (1, 'C');} void erase_map () // delete an element {Map <I NT, char >:: iterator CP = Ctr. find (2); Ctr. erase (CP); // Delete the element whose key value is equal to 2} void clear_map () {Ctr. clear (); // clear the map container (delete all elements) if (CTR. empty () // when the map container is empty, cout <"the container is empty" <Endl; else cout <"the container is not empty" <Endl ;} int print () // print a map container {Map <int, char >:: const_iterator CP; For (Cp = Ctr. begin (); CP! = Ctr. end (); CP ++) // print the value of print_one_item (CP) for CP from the beginning to the end of C; // call the subroutine to print an element return 0 ;} void print_first_element () {Map <int, char >:: iterator CP; // iterator CP = Ctr. begin (); // locate the start point of the CTR, cout <"the first element is:" <CP-> second <Endl; // print out the first element} void key_compare_map () // key_comp get a copy of the Compare object to sort the elements in the map container. {Map <int, int> C; Map <int, Int, less <int> >:: key_compare kc = C. key_comp (); If (Kc (1, 2) cout <"KC (1, 2) is true" <Endl; else cout <"KC (1, 2) is false "<Endl; If (Kc (2, 1) cout <" KC (2, 1) is true "<Endl; else cout <" KC (2, 1) is false "<Endl;} void lower_bound_map () {Map <int, char >:: iterator CP; /* returns a locator whose value points to the first keyword is greater than or equal to a given value element, or returns the ending locator pointing to the map volume */CP = Ctr. lower_bound (2); // returns the first Locator of an element greater than or equal to 2. If (CP! = Ctr. end () {cout <"the first element which key> = 2 is:"; print_one_item (CP); // call a subroutine to print a cout <Endl ;}} void compare_map () {Map <int, char> ctr1, ctr2; int I; for (I = 0; I <3; I ++) {ctr1.insert (pair <int, char> (I, 'A' + I); ctr2.insert (pair <int, char> (I, 'A' + I);} If (ctr1! = Ctr2) // when ctr1 and CT2 are different, cout <"they are not equal" <Endl; else // when ctr1 and ctr2 are cout <"they are equal" <Endl;} void comp_map () // The size comparison of the two map containers is based on the size comparison of the first distinct element. {// Two map containers are equal. if and only when their number of elements is equal and their values at the same position are equal, Map <int, char> ctr1, ctr2; int I; for (I = 0; I <3; I ++) // The values of ctr1 and ctr2 are {ctr1.insert (pair <int, char> (I, I )); ctr2.insert (pair <int, char> (I, I + 1);} If (ctr1 <ctr2) cout <"ctr1 <ctr2" <Endl; else cout <"ctr1> = ctr2" <Endl;} void reverse_map () // print the reverse map rbegin () rend () and reverse_iterator at the same time using {Map <int, char >:: reverse_iterator RCP; For (RCP = Ctr. rbegin (); RCP! = Ctr. rend (); RCP ++) cout <"(" <RCP-> first <"," <RCP-> second <")";} void swap_map () {Map <int, int> ctr1, ctr2; Map <int, int>: const_iterator CP; int I; for (I = 0; I <3; I ++) // assign values to ctr1 and ctr2 first {ctr1.insert (pair <int, int> (I, I); ctr2.insert (pair <int, int> (I, I + 10);} cout <"before exchange with ctr2 the ctr1 is:"; for (Cp = ctr1.begin (); CP! = Ctr1.end (); CP ++) // print the cout value corresponding to CP from the beginning to the end of CP <"(" <CP-> first <", "<CP-> second <") "; cout <Endl; cout <" after exchange with ctr2 the ctr1 is: "; ctr1.swap (ctr2 ); // exchange ctr1 content with ctr2 for (Cp = ctr1.begin (); CP! = Ctr1.end (); CP ++) // print the cout value corresponding to CP from the beginning to the end of CP <"(" <CP-> first <", "<CP-> second <") "; cout <Endl ;}int main () {creat_map (); int I; cout <" 1, test begin () "<Endl; cout <" 2, test count () to calculate the number of keywords "<Endl; cout <" 3, test test_1__range () "<Endl; cout <" 4, test erase () "<Endl; cout <" 5, test key_compare_map () "<Endl; cout <"6, test lower_bound_map ()" <Endl; cout <"7, test map size and max_size (maximum possible length)" <Endl; cout <"8, test symbol [] Use the <Endl; cout <"9 to test the symbol! = Function "<Endl; cout <" 10, test symbol <function "<Endl; cout <" 11, print reverse map "<Endl; cout <"12, swap two map values" <Endl; while (1) {CIN> I; Switch (I) {Case 1: print_first_element (); break; case 2: Int J; j = Ctr. count (1); // obtain the number of elements whose keyword is 1 (since the map container keyword is unique, it can only take 0 or 1) cout <"the number of key 1 is:" <j <Endl; break; Case 3: test_1__range (); break; Case 4: erase_map (); break; case 5: key_compare_map (); break; Case 6: lower_bound_map (); break; Case 7: cout <"the size of CTR is:" <Ctr. size () <Endl; cout <"The max_size of CTR is:" <Ctr. max_size () <Endl; break; case 8: cout <"Before change map is:" <Endl; print (); CTR [1] = 'W '; // change the value corresponding to the keyword 1 to w ctr [7]; // Add a cout item with the keyword 7 value 0 <"\ nafter change map is: "<Endl; print (); break; Case 9: compare_map (); break; case 10: comp_map (); break; Case 11: reverse_map (); break; case 12: swap_map (); break ;}} Map <int, char >:: iterator end; // iterator end = Ctr. end (); // locate the position behind the last element in the map, so nothing is printed. // end --; // locate the last element D except the repeated keyword C cout <"the last element is:" <End-> second <Endl; clear_map (); return 0 ;}

Let's look at the application of a map data structure (count the number of occurrences of each string)

 
# Include <iostream> # include <map> using namespace STD; int main () {Map <string, int> m; Map <string, int>: iterator J; string T [5] = {"ABC", "DD", "ABC", "DD", "DD"}; For (INT I = 0; I <5; ++ I) m [T [I] ++; // The default value is the second value rather than the key for (j = m. begin (); J! = M. end (); ++ J) cout <"<" <j-> first <"," <j-> second <">" <Endl; return 0 ;}

Related Article

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.