STL Map and Multimap containers

Source: Internet
Author: User

1.Map and Multimap containers

1) map is a standard associative container, and a map is a sequence of key-value pairs, i.e. (key,value) pairs. It provides fast retrieval capability based on key.

2) The key value in map is unique . The elements in the collection are sorted in a certain order . The element insertion procedure is inserted by collation, so you cannot specify the insertion position.

3) The concrete implementation of map adopts the data structure of the balanced binary tree of red and black tree variant. Faster than vector on insert and delete operations.

4) Map can directly access the value corresponding to key, support the [] operator, such as Map[key]=value.

5) The difference between Multimap and map: Map supports unique key values, each key can only occur once, while the same key may appear multiple times in Multimap. The [] operator is not supported by Multimap.

6) #include <map>

2.map/multimap default constructs for objects

Map/multimap is implemented as a template class, with the default construction of objects:

Map<t1,t2> Maptt;

Multimap<t1,t2> Multimaptt;

Such as:

Map<int, char> MapA;

Map<string,float> MAPB;

Where t1,t2 can also be used with various pointer types or custom types

3.map the insertion and iterator

Map.insert (...); Insert element into container, return pair<iterator,bool>

Three ways to insert elements in a map:

Suppose Map<int, string> mapstu;

I. Inserting an object by means of a pair

Mapstu.insert (pair<int,string> (3, "Xiao Zhang"));

Ii. inserting an object by means of a pair

Mapstu.inset (Make_pair (-1, "headmaster-1"));

Iii. inserting an object by means of Value_type

Mapstu.insert (Map<int,string>::value_type (1, "Xiao Li"));

Iv. inserting a value by means of an array

MAPSTU[3] = "Xiao Liu";

MAPSTU[5] = "Xiao Wang";

Note:

The first three methods, using the Insert () method, the method returns a value of pair<iterator,bool>

The fourth approach is straightforward, but there is a performance issue. Insert 3 o'clock, first find the key 3 in Mapstu, if not found, a key is 3, the value is the initialization value of the group inserted into the Mapstu, and then modify the value to "Xiao Liu." If a key of 3 is found, the value corresponding to this key is modified.

String strName = Mapstu[2]; Take an action or insert operation

The correct fetch operation is only if the Mapstu exists with 2, otherwise it will automatically insert an instance, the key is 2, and the value is the initialization value.

Suppose map<int,string>Mapa;pair< map<int,string>::iterator,BOOL> Pairresult = Mapa.insert (pair<int,string> (3,"Xiao Zhang"));//Insert Method OneintIfirstfirst = (Pairresult.first)->first;//Ifirst = = 3;stringStrfirstsecond = (Pairresult.first)->second;//strfirstsecond for "Xiao Zhang"BOOLBsecond = Pairresult.second;//Bsecond = = true;Mapa.insert (Map<int,string>::value_type (1,"Xiao Li"));//Insertion Mode Twomapa[3] ="Xiao Liu";//Modify Valuemapa[5] ="Xiao Wang";//Insert Method ThreestringSTR1 = mapa[2];//executes the Insert string () operation, returning the string contents of the str1 to null. stringSTR2 = mapa[3];//get value,str2 as "Xiao Liu"//iterator Traversal     for(map<int,string>::iterator It=mapa.begin (); It!=mapa.end (); ++it) {Pair<int,string> PR = *it; intIKey =Pr.first; stringstrvalue =Pr.second; }

Note:

map<t1,t2,less<t1> > MapA; The container is the ascending order of the keys to arrange the elements. The function object is not specified, and the Less<t1> function object is used by default.

Map<t1,t2,greater<t1>> MAPB; The container is the descending order of the keys to arrange the elements.

Less<t1> and greater<t1> can be replaced by other function object functor.

You can write custom function objects for custom type comparisons, using the same method as the function object used when the set is constructed.

Map.begin (); An iterator that returns the first data in the container.

Map.end (); Returns an iterator after the last data in the container.

Map.rbegin (); Returns an iterator to the first element in the container.

Map.rend (); Returns an iterator to the back of the last element in the container.

Copy construction and assignment of 4.map objects

Map (const map &MP); Copy constructor

map& operator= (const map &MP); Overloaded equals operator

Map.swap (MP); Swap Two collection containers

Example: Map<int,string>MapA; Mapa.insert (Pair<int,string> (3,"Xiao Zhang")); Mapa.insert (Pair<int,string> (1,"Xiao Yang")); Mapa.insert (Pair<int,string> (7,"Xiao Zhao")); Mapa.insert (Pair<int,string> (5,"Xiao Wang")); Map<int,string> MAPB (MapA);//Copy ConstructionMap<int,string>MAPC; MAPC= MapA;//Assign Valuemapc[3] ="Lao Zhang";            Mapc.swap (MapA); //Exchange

Size of 5.map

Map.size (); Returns the number of elements in a container

Map.empty ();//Determine if the container is empty

map<int,string>Mapa;mapa.insert (Pair<int,string> (3,"Xiao Zhang")); Mapa.insert (Pair<int,string> (1,"Xiao Yang")); Mapa.insert (Pair<int,string> (7,"Xiao Zhao")); Mapa.insert (Pair<int,string> (5,"Xiao Wang")); if(Mapa.empty ()) {intIsize = Mapa.size ();//Isize = = 4}

Removal of 6.map

Map.clear (); Delete all elements

Map.erase (POS); Removes the element referred to by the POS iterator and returns an iterator to the next element.

Map.erase (Beg,end); Removes all elements of the interval [beg,end] and returns an iterator to the next element.

Map.erase (Keyelem); Removes the pair of key keyelem in the container.

map<int,string>MapA; Mapa.insert (Pair<int,string> (3,"Xiao Zhang")); Mapa.insert (Pair<int,string> (1,"Xiao Yang")); Mapa.insert (Pair<int,string> (7,"Xiao Zhao")); Mapa.insert (Pair<int,string> (5,"Xiao Wang")); //Delete elements within a rangemap<int,string>::iterator itbegin=Mapa.begin (); ++Itbegin; ++Itbegin; Map<int,string>::iterator itend=Mapa.end ();            Mapa.erase (Itbegin,itend); //at this time the container Mapa contains two elements in order {1, "Xiao Yang"}{3, "Xiao Zhang"}. Mapa.insert (Pair<int,string> (7,"Xiao Zhao")); Mapa.insert (Pair<int,string> (5,"Xiao Wang")); //Delete the first element in a containerMapa.erase (Mapa.begin ());//at this time the container Mapa contains the order of {3, "Xiao Zhang"}{5, "Xiao Wang"}{7, "Xiao Zhao"} three elements//Delete the element with key 5 in the containerMapa.erase (5);
//Delete all elements of MapaMapa.clear ();//container is empty

7.map Lookup

Map.find (key); Find if key key exists, if present, returns an iterator to the element of the key; Map.end () If it does not exist;

Map.count (Keyelem); Returns the number of pairs in the container where key is Keyelem. For map, it is either 0 or 1. For Multimap, the value may be greater than 1.

map<int,string>::iterator It=mapstu.find (3);if(It = =Mapstu.end ()) {        //didn't find}Else{        //found thepair<int,string> pairstu = *it; intIID = Pairstu.first;//or int iID = it->first;      stringStrName = Pairstu.second;//or string strName = it->second;}
Map.lower_bound (Keyelem);//returns an iterator to the first Key>=keyelem element. Map.upper_bound (Keyelem);//returns an iterator to the first Key>keyelem element. For example: Mapstu is a map<int,stringThe container for the > declaration, which already contains {1,"Xiao Li"}{3,"Xiao Zhang"}{5,"Xiao Wang"}{7,"Xiao Zhao"}{9,"Xiao Chen"Element map<int,string>:: Iterator it;it= Mapstu.lower_bound (5);//it->first==5 it->second== "Xiao Wang"it = Mapstu.upper_bound (5);//it->first==7 it->second== "Xiao Zhao"it = Mapstu.lower_bound (6);//it->first==7 it->second== "Xiao Zhao"it = Mapstu.upper_bound (6);//it->first==7 it->second== "Xiao Zhao"
Map.equal_range (Keyelem);//returns a two iterator of the upper and lower bounds of the key and Keyelem equal in the container. The upper limit is the closed interval, the lower limit is the open interval, such as [Beg,end]. The above function returns two iterators, and the two iterators are encapsulated in a pair. such as Map<int,string>Mapstu, .....//inserting elements into the Mapstu container {1, "Xiao Li"}{3, "Xiao Zhang"}{5, "Xiao Wang"}{7, "Xiao Zhao"}{9, "Xiao Chen"}pair< map<int,string>::iterator, map<int,string>::iterator > Pairit = Mapstu.equal_range (5); Map<int,string>::iterator Itbeg =Pairit.first;map<int,string>::iterator Itend =Pairit.second;//at this time itbeg->first==5, Itend->first = = 7,itbeg->second=="Xiao Wang", itend->second=="Xiao Zhao"

STL Map and Multimap containers

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.