Map Mapping Container

Source: Internet
Author: User

The element data of a map mapping container is a key value and a mapping data, the relationship between the key value and the mapping data has a one by one mapping.
Map mapping container data structure is implemented using red-black tree, the element inserted into the key is not allowed to repeat, the comparison function is only the key value of the element to compare, the elements of the data can be retrieved by key values.
Using the map container requires that the header file contain the statement "#include <map>".

1.map creation, element insertion, and traversal access:

Creates a map object, and the type of the key value and the mapping data is defined by itself. When you do not specify a comparison function, the insertion position of the element is inserted in the red and black tree according to the value from small to large, which is the same as the set we talked about earlier.

1#include <iostream>2#include <cstring>3#include <map>4 using namespacestd;5 6 intMain () {7map<string,float> m;//defines a map object that currently has no elements8m["Jack"]=98.5;//insert element, key value from small to large put9m["Bomi"]=96.0;Tenm["Kate"]=97.5; One      Amap<string,float>::iterator it;//to define a forward iterator -      for(It=m.begin (); It!=m.end (); it++) {//traversing output key values and mapping data -cout<< (*it) .first<<" : "<< (*it) .second<<Endl; the     } -     return 0; -}

Operation Result:

bomi:96

jack:98.5

kate:97.5

2. Delete the element:

Like set, the map mapping container's erase () function can delete an element on an iterator position, an element equal to a key value, all elements on an iterator interval, or, of course, use the clear () method to empty the map mapping container.

1#include <iostream>2#include <cstring>3#include <map>4 using namespacestd;5 6 intMain () {7map<int,Char>m;8map[ -]='m';9map[ -]='k';Tenmap[Ten]='x'; Onemap[ -]='a'; A      -M.erase ( -);//Delete an element with a key value of 28 -map<int,Char>:: iterator it; the      for(It=m.begin (); It!=m.end (); it++){ -cout<< (*it) .first<<" : "<< (*it) .second<<Endl; -     }  -     return 0; +}

Operation Result:

10:x

25:m

30:a

3. Element Reverse Traversal:

As with set, we can use the reverse iterator reverse_iterator to reverse traverse the data in the Map mapping container, which requires rbegin () and rend () to indicate the starting and ending positions of the introspection traversal.

1#include <iostream>2#include <cstring>3#include <map>4 using namespacestd;5 6 intMain () {7map<int,Char>m;8map[ -]='m';9map[ -]='k';Tenmap[Ten]='x'; Onemap[ -]='a'; A      -map<int,Char>::reverse_iterator it;//defining a reverse iterator -Fro (It=m.rbegin (); It!=m.rend (); it++) {//Reverse Traversal thecout<< (*it) .first<<" : "<< (*it) .second<<Endl; -     } -     return 0; -}

Operation Result:

30:a

28:k

25:m

10:x

4. Search for elements:

Still, like set, searches for a key value using Find (), returns the position of the iterator where the key value is found, or returns the end () iterator position.

1#include <iosteram>2#include <cstring>3#include <map>4 using namespacestd;5 6 intMain () {7map<int,Char>m;8m[ -]='m';9m[ -]='k';Tenm[Ten]='x'; Onem[ -]='a'; A      -map<int,Char>:: iterator it; -It=find ( -);//search for elements with a key value of 28 the     if(It!=m.end ()) {//Find -cout<< (*it) .first<<" : "<< (*it) .second<<Endl; -     } -     Else{  +cout<<"Not found it"<<Endl; -     } +     return 0; A}

Operation Result:

28:k

Intuitively, one of the main features of the map container, which differs from the set container, is that map is the fast insertion, deletion, and retrieval of data for record-type elements with key values, while set can be considered as a processing of a single data. Map creates a container's data distribution by dividing an element into a key-value section and specifying the function comparison rules for the entire element by this local key value. The element key value of map is unique and does not allow duplicate element key values to be inserted. Both set and map are a generalization of the generic library to a two-fork tree.

Map disadvantage: Map is a more advanced usage, suitable for the use of list, vector can be done by the operation, there is no need to use map.
Map Advantage: Map can handle fast insertion, deletion, and retrieval of record-based data with key values compared to set. The speed of the retrieval is also very fast.

Map Mapping Container

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.