STL_ Map and Multimap containers

Time:2021-4-9

1、 Introduction of map / Multimap

Map is standardCorrelationContainer, a map is a sequence of key value pairs, namely (key, value) pairs. It provides fast retrieval capability based on key.

In mapkeyThe value is unique. The elements in the collection are in a certain orderArrange. The insertion process of an element is inserted according to a collation, so the insertion position cannot be specified.

The implementation of map adopts the balanced binary tree data structure of red black tree variant, which is faster than vector in insert and delete operations.

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

The difference between Multimap and map: Map supports unique key values, and each key can only appear once, while the same key can appear multiple times in Multimap. Multimap does not support the [] operator.

include

2、 Default construction of map / Multimap objects

Map / Multimap is implemented by template class, and the default construction form of the object is as follows:

  • map mapTT;
  • multimap multimapTT;
map mapA;
map mapB;
//T1 and T2 can also use various pointer types or user-defined types

3、 Map insertion and iterator

map.insert (…); / / insert an element into the container and return pair

Hypothetical mapMymap, how to insert elements in the map:

  1. Insert objects by pair method: mymap.insert (pair(10, 10));

  2. Insert objects by pair method: mymap.insert (make_ pair(20, 20));

  3. Through value_ Type to insert objects: mymap.insert (map::value_type(30, 30));

  4. Insert value through array: mymap [40] = 40.

//Map container template parameter, the type of the first parameter key and the type of the second parameter value
	map mymap;

	//Insert data pair.first  Key value piar.second  Value value
	//The first kind
	pair::iterator, bool> ret = mymap.insert(pair(10, 10));
	//The second kind
	mymap.insert(make_pair(20, 20));
	//The third kind
	mymap.insert(map::value_type(30, 30));
	//Fourth, if the key does not exist, the pair is created and inserted into the map container; if the key exists, the value corresponding to the key is modified.
	mymap[40] = 40;
	mymap[10] = 20;
	mymap[50] = 50;

	for (map::iterator it = mymap.begin(); it != mymap.end(); it++) {
		cout << "key:" << (*it).first << " value:" << it->second << endl;
	}
/*
result:
key:10 value:20
key:20 value:20
key:30 value:30
key:40 value:40
key:50 value:50
*/

The first three methods use the insert () method, and the return value of this method is pair

pair::iterator, bool> ret = mymap.insert(pair(10, 10));
	if (ret.second) {
		Cout < "insert successfully for the first time!" < endl;
	}
	else {
		Cout < "insert failed!" < endl;
	}
/*
result:
The first insertion is successful!
*/

The fourth method is very intuitive, but there is a performance problem. When inserting 3, first look up the item with primary key 3 in mapstu. If it is not found, insert a pair group with key 3 and initialization value into mapstu, and then insert the value into mapstumodify. If the key 3 already exists, modify the value corresponding to the key.

map>Mapa; / / this container is used to arrange elements in ascending order of keys. Function object is not specified. Less is adopted by defaultFunction object.

map>Mapb; / / this container is used to arrange elements in descending order of keys.

lessAnd greaterIt can be replaced with other function objects.

You can write a user-defined function object to compare user-defined types. The method is the same as the function object used in set construction.

4、 Copy construction and assignment of map object

Map (const Map & MP); / / copy constructor

Map & operator = (const Map & MP); / / overload equal sign operator

map.swap (MP); / / exchange two collection containers

map mapA;
mapA.insert (pair (3, Xiao Zhang));
mapA.insert (pair (1, "Xiao Yang");    
mapA.insert (pair (7, "Xiao Zhao");    
mapA.insert (pair (5, "Xiao Wang");    
Map mapb (mapa); // copy construction
map mapC;
MAPC = mapa; // assignment
MAPC [3] ='lao Zhang ';
mapC.swap (mapa); // exchange

5、 The size of the map

map.size (); / / returns the number of elements in the container

map.empty (); / / judge whether the container is empty

map mapA;
mapA.insert (pair (3, Xiao Zhang));    
mapA.insert (pair (1, "Xiao Yang");    
if (mapA.empty())
{
   int iSize = mapA.size();       //iSize == 4
}

6、 Deletion of map

map.clear (); / / delete all elements

map.erase (POS); / / delete the element indicated by the POS iterator and return the iterator of the next element.

map.erase (beg, end); / / delete all elements in the interval [beg, end], and return the iterator of the next element.

map.erase (keyelem); / / delete the pairing group whose key is keyelem in the container.

map mapA;
mapA.insert (pair (3, Xiao Zhang));   
mapA.insert (pair (1, "Xiao Yang");   
mapA.insert (pair (7, "Xiao Zhao");   
mapA.insert (pair (5, "Xiao Wang");    
 //Delete the elements in the interval
map::iterator itBegin=mapA.begin();
++ itBegin;
++ itBegin;

map::iterator itEnd=mapA.end();
mapA.erase (itbegin, itend); // at this time, the container mapa contains two elements {1, "Xiao Yang"} {3, "Xiao Zhang"} in order.

mapA.insert (pair (7, "Xiao Zhao");    
mapA.insert (pair (5, "Xiao Wang");    

//Delete the first element in the container
mapA.erase ( mapA.begin ()); // in this case, mapa contains three elements in order: {3, Xiao Zhang} {5, Xiao Wang} {7, Xiao Zhao}

//Delete the element with key 5 in the container
mapA.erase(5);  

//Delete all elements of mapa
mapA.clear (); // the container is empty

7、 Search of map

map.find (key); / / find out whether the key exists. If it exists, return the iterator of the key’s element; if it does not exist, return map.end ();

map.count (keyelem); / / returns the number of pairs whose key is keyelem in the container. For map, it’s either 0 or 1. For Multimap, the value may be greater than 1.

map.begin (); / / returns the iterator of the first data in the container.

map.end (); / / returns the iterator after the last data in the container.

map.rbegin (); / / returns the iterator of the penultimate element in the container.

map.rend (); / / returns the iterator after the last element in the container.

map.lower_ Bound (keyelem); / / returns the iterator of the first key > = keyelem element.

map.upper_ Bound (keyelem); / / returns the iterator of the first key > keyelem element

map.equal_ Range (keyelem); / / returns two iterators of the upper and lower limits of the container where the key and keyelem are equal. The upper limit is a closed interval, and the lower limit is an open interval, such as [beg, end].

map mymap;
	mymap.insert(make_pair(1, 4));
	mymap.insert(make_pair(2, 5));
	mymap.insert(make_pair(3, 6));

	pair::iterator, map::iterator> ret = mymap.equal_range(2);
	if (ret.first != mymap.end()) {
		Cout < "find the lower_ bound! " << endl;
	}
	else {
		Cout < not found;
	}

	if (ret.second != mymap.end()) {
		Cout < "find upper_ bound! " << endl;
	}
	else {
		Cout < not found;
	}
/*
result:
Find the lower_ bound!
Find upper_ bound!
*/