The associative container in STL-map (mapping)

The associative container in STL-map (mapping)

  map, that is, "mapping". A map is a kind of associative container, which implements a data structure of a balanced binary tree using a red-black tree. It provides one-to-one data processing capability (the first one is called the keyword key, each keyword can only appear once in the map, and the second is called the value of the keyword value) data processing capability. Due to this feature, it It is possible to provide a fast channel in programming when we are processing one-to-one data. Because it is implemented using red-black trees, it is possible to quickly insert key-value records and quickly delete records.
  And for the iterator, the value can be modified, but the key cannot be modified, and it modifies the value record according to the key, so adding and deleting nodes has little impact on the iterator, except for the operation node, it has no effect on other nodes .
  map is an important member of the C++ STL, you need to include the header file when using it:

#include <map>; //包含了map和multimap两种容器的定义

1. Initialization of map

There are the following ways, examples are as follows:

map<int,string> a;
map<int,string> b(a); //拷贝队列a给队列b

Second, the important operations of the map object

Listed as follows:

a.insert(); //插入元素 
a.erase(); //删除一个元素 
a.find(); //查找一个元素 
a.clear(); //删除所有元素 
a.empty(); //如果map为空则返回true 
a.count(); //返回指定元素出现的次数 
a.size(); //返回map中元素的个数 
a.max_size(); //返回可以容纳的最大元素个数 
a.swap(); //交换两个map 

Three, priority setting

There are several ways, listed as follows:

struct cmp1
{
    
    
    bool operator ()(const int &a,const int &b)
    {
    
    
       return a>b;//最大值优先
   	}
};
struct cmp2
{
    
    
	bool operator ()(const int &a,const int &b)
	{
    
    
	    return a<b;//最小值优先
	}
};
struct node1
{
    
    
	int u;
	bool operator < (const node1 &a) const
	{
    
    
		return u>a.u;//最大值优先
	}
};
struct node2
{
    
    
	int u;
	bool operator < (const node2 &a) const
	{
    
    
		return u<a.u;//最小值优先
	}
};
map<int,string>q1;//采用默认优先级构造队列,最小值优先
map<int,string,cmp1>q2;//最大值优先 
map<int,string,cmp2>q3;//最小值优先
map<int,string,greater<int> >q4;//最大值优先
//注意“>>”会被认为错误,因为这是右移运算符,所以这里用空格号隔开
map<int,string,less<int> >q5;//最小值优先
map<node1,string>q6;//最大值优先
map<node2,string>q7;//最小值优先

Guess you like

Origin blog.csdn.net/hyl1181/article/details/108569128