c++STL standard class template library (associative containers (map, multimap class templates), the assignment methods of these two containers are different from others)

This blog takes over the associative containers (set, multiset) in the previous blog, both of which have similarities and differences. The data and key values ​​​​of the elements in the map and multimap types are separated, while set, multiset The data and key value of the elements in the multiset type is a location, that is, the stored data is also called the key of the data.

map container

The syntax for creating a map class template is as follows:

std::map<key,type,predicate> name;

This method creates an empty map object named name that contains data of type type. This object uses the function specified by the predicate to order the elements in the collection. For example, to create an empty map object for integers, write:

std::map<int,int,std::less<int>> intmap;

 map<key,type,predicate> name(mymap);

//This method uses the copy constructor to generate a map object from an existing map mymap.

map<key,type,predicate> name(first,last);

//This method creates a map from a range of elements based on the start and end positions indicated by the multiple pointers.

The method description in the map class template is shown in the table:


function illustrate
begin Returns an iterator pointing to the first element in the collection
end Returns an iterator pointing to the last element in the collection
clear delete all elements in the set
empty Determine whether the collection is empty, if it is empty return true
equal_range(x) Returns two iterators representing the lower and upper bounds of x, where the lower bound represents the first element in the collection with a value equal to x and the upper bound represents the first element with a value greater than x
erase(i) Delete the collection element pointed by the iterator, or delete the collection element by key value
erase(x) removes the element with value x from the set
find(x) Returns an iterator pointing to x, if x does not exist, the returned iterator is equal to end
lower_bound(x) Returns an iterator pointing to the element preceding and immediately adjacent to x
max_size Returns the largest container for the collection
rbegin Returns a reverse iterator pointing to the last element of the collection
rend Returns a reverse iterator pointing to the first element of the collection
size Returns the size of the collection
swap() swaps the contents of two collections
upper_bound(x) returns a container pointing to x
value_comp Returns an object of value_compare type, which is used to determine the order of elements in the collection

For example, create a map object and add new elements using insertion:

#include<iostream>
#include<map>
using namespace std;
int main()
{
	map<int,char> imap;    //创建map映射对象
	imap.insert(map<int,char>::value_type(1,'c'));    //插入新元素(与之前的插入有所不同)
	imap.insert(map<int,char>::value_type(4,'a'));
	imap.insert(map<int,char>::value_type(3,'d'));
	imap.insert(map<int,char>::value_type(15,'f'));
	map<int,char>::iterator it;
	for(it=imap.begin();it!=imap.end();it++)        //循环map映射显示元素值
	{    
		cout<<(*it).first<<"->";
		cout<<(*it).second<<endl;
	}
	return 0;	
} 

 The result of the operation is as follows:

It can be seen from the running results that the container is sorted by the previous retrieval code rather than the data itself, and the insertion and traversal output of this type of template are different from the previous template, using it.first to represent the first A retrieval code, it.second represents the second data, and then inserting is using imap.insert(map<int,char>::value_type()); the difference is that the value_type data is used.

Note that these two class templates store string data differently from other class template operations. The specific operations are as follows:

#include<iostream>
#include<map>
using namespace std;
int main()
{
	map<double,string> mm;
	mm.insert(pair<double,string>(5.24,string("李牧歌")));    //用pair的格式插入数据
	mm.insert(make_pair(3.21,string("侯伊美")));        //使用make_pair存储数据
	mm.insert(make_pair(10.9,string("王润怡")));
	mm.insert(make_pair(9.21,string("王俐元")));
	map<double,string>::iterator it=mm.begin();
	cout<<"mm:"<<endl;
	while(it!=mm.end())
	{
		cout<<"生日"<<it->first<<endl;
		cout<<"姓名"<<it->second<<endl;
		cout<<endl; 
		*it++;	
	}	
	return 0;	
} 

The specific running results are as follows (the ages are also sorted):

 Specifically, two formats are used to insert string data, make_pair and pair. I think the first method is more convenient to operate. You can also assign mm[1.5]=string("hym) directly in the form of an array ; pay special attention to the fact that the assignment here is different from that of other class templates, so you need to pay special attention.

multimap container


        Multimap can store a set of values ​​sequentially. It is the same as map in that each element can contain a key value and associated data items. Unlike map, multimap can contain repeated data, but it cannot be used [ ] operator assigns values ​​to multiple elements.

The statement to construct the multimap class template is as follows:

std::multimap<key,type,predicate> name;

        This method creates an empty multimap object named name that contains a data type of type. This object uses the function specified by the predicate to order the elements in the collection. For example, to create an empty multimap object for integers you can write:

std::multimap<int,int,std::less<int> > intmap;
//注意《int》后需要敲一个空格

multimap<key,type,predicate> name(mymap);

//This method uses the copy constructor to generate a multimap object from an existing map mymap.

multimap<key,type,predicate> name(first,last);

//This method creates a map from a range of elements based on the start and end positions indicated by the multiple pointers.

The biggest difference between it and the map class template is that [] cannot be used to assign values ​​to the elements in the collection, and its data items can appear repeatedly, as shown in the following code:

#include<iostream>
#include<map>
using namespace std;
int main()
{
	multimap<double,char> Cmap;
	Cmap.insert(make_pair(1,'c'));
	Cmap.insert(make_pair(4,'c'));
	Cmap.insert(make_pair(2,'b'));
	Cmap.insert(make_pair(6,'r'));
	Cmap.insert(make_pair(13,'a'));
	cout<<"Cmap :"<<endl;
	map<double,char>::iterator it=Cmap.begin();
	while(it!=Cmap.end())
	{
		cout<<" "<<it->first;
		cout<<" "<<it->second<<endl;
		cout<<endl; 
		*it++;	
	}	
	return 0;	
} 

You can also use the pair method to assign values, and you can also use map<int,cahr>::value_type() to perform assignment operations. If you have any convenient and simple assignment operations, I hope you can share them in the comment area. For this multimap The use method in the class template is exactly the same as that of the map class template, so there is no diagram of its use method here, and it is only necessary to distinguish the difference between it and the map class template.

        This is the end of this blog. I promised to update the blog on Wednesday, but I did participate in some school activities and competitions on Wednesday, so Wednesday was very busy and I didn’t update the blog. Today’s class is a little less. After writing the blog, I hope you can read these blogs about the STL standard template library, you can have a certain understanding of the STL standard template library, and if you are interested, you can go down and think about the code yourself. It is better to use your brain than to do it. Do. The next blog should be about a more important module algorithm in the STL template library. You can pay attention to the follow-up content (anyway, I will not promise to update the blog this time, and the most important thing is to update the blog as you like.) Every I can learn some small knowledge points that I haven’t discovered before when I update a blog. It may be that I didn’t learn very carefully when I was studying hahaha, so we must always practice and don’t imagine. The program may run out and follow your imagination. It's totally different.

Guess you like

Origin blog.csdn.net/m0_61886762/article/details/124158923
Recommended