Associative containers (map, set, multimap, multiset)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/liuyuchen282828/article/details/102508702

Relational concept

  • The STL portion of the container, such as: vector, list, deque, forward_list (C ++ 11), these sequences referred to as container vessels, because the underlying data structure of a linear sequence, which is stored in the element itself.
  • Associative containers are used to store data, a sequence of different containers is that there is a stored key <key, value> pair structure , higher efficiency than the sequential retrieval of data containers.
Binary search balanced structure:

Sequence key ordered, time complexity of O (logN)

  • map <key, value> requires: key must not be repeated
  • SET: key requirements: key must not be repeated
  • multimap: <key, value> key can be repeated
  • multiset: key, key can be repeated

Key-value pairs

Has a structure used to represent one relationship, the structure typically contains only two member variables and key value, key representative of the key, value represents a corresponding key information . For example: Translation is now to build a dictionary, then the dictionary must have their corresponding English word meaning Chinese and English words with their Chinese meaning is one to one relationship, that is, through the word should, in the dictionary you can find the corresponding Chinese meaning

SGI-STL underlying implementation in key-value pairs
 template <class T1, class T2> 
 struct pair 
 { 
	  typedef T1 first_type;  
	  typedef T2 second_type;
	 
	T1 first;  
	T2 second;  
pair()     
	  : first(T1())      
	  , second(T2())  
 	  {
 	  }      
pair(const T1& a, const T2& b)
      : first(a)
     , second(b) 
      {
      }
 }; 

map

  1. map is associated with a container, which (according to the comparison key) is stored in a particular order and combination of elements of the key values ​​of key value formed.
  2. In the map, the key is typically used to sort key and uniquely identifies the element, and the value of this key value is stored in the content key is associated. Key and key value may be different types of values, and within the map, the key value by binding together members value_type type, referred to as its alias pair: typedef pair value_type;
  3. In the interior, map the elements always be compared sorted by key key.
  4. map by accessing a single key element velocity is generally slower than unordered_map container, but allowing the elements directly map the order iteration (i.e., to map the elements in the iteration, can be an ordered sequence).
  5. Support map indexed access identifier, i.e., placed in the key [], the key can be found with the corresponding value.
  6. map is typically implemented as a binary search tree (more precisely: balanced binary search tree (red-black tree)).

Here Insert Picture Description

map::insert()

Here Insert Picture Description
There are two ways by inserting insert data

  1. pair<类型,类型>(数据,数据)
  2. make_pair(数据,数据)

Can also subscript operator []
to map, a case where if we provide the key does not exist, he will insert this data
because of its underlying subscript operator is realized as

(*((this->insert(make_pair(k,mapped_type()))).first)).second 

If the key has been in existence, he would not insert

void TestMap()
{
	map<string, string>m;
	m.insert(pair<string,string>("宋江","及时雨"));
	m.insert(pair<string, string>("李逵", "黑旋风"));

	//pair<iterator,bool>
	//iterator:代表map中的一个key-value的键值对
	//bool:insert插入是否成功
	auto ret = m.insert(make_pair("孙二娘","母夜叉"));
	if (ret.second){
		cout << (*ret.first).first << "--->" << ret.first->second << endl;
	}
	ret = m.insert(make_pair("李逵", "铁牛"));
	//key 已经存在不会插入
	cout << (*ret.first).first << "--->" << ret.first->second << endl;

	cout << m.size() << endl;

	cout << m["李逵"] << endl; //把value返回来
	//用户提供key---->[]返回key所对应的value
	m["李逵"] = "铁牛";  //修改value
	cout << m["李逵"] << endl;
	m["林冲"] = "豹子头";
	cout << m.size() << endl;
}

Why subscript operator to use insert?
Because if the key does not exist, the return value is not returned, because there is a return, if the key inserted into the key does not exist and a default value if the key exists

void TestMap2()
{
	int array[] = { 3, 1, 9, 4, 0, 7, 6, 2, 5 };
	map<int, int>m;
	for (auto e : array){
		m.insert(make_pair(e, e));
	}

	//测试按照迭代器方式进行遍历,能否得到一个关于key有序的序列
	auto it = m.begin();
	while (it != m.end())
	{
		cout << it->first << "---->" << it->second << endl;
		++it;
	}
	cout << endl;

	for (auto& e : m)
	{
		cout << e.first << "---->" << e.second << endl;
	}
	cout << endl;
}

key is ordered, but the value is not necessarily
the rest of the functional demo Reference: Other features of the demo map

set

Demonstration set of interfaces

set container has no budget subscript characters.
set: Key, Key necessarily unique
set most important function is to heavy and sorting

Test set

int main()
{
	int array[] = { 3, 1, 3, 4, 5, 6, 7, 8, 9, 0, 9, 4, 0, 7, 6, 2, 5, 8 };
	set<int>s;
	for (auto e : array)
		s.insert(e);
	cout << s.size() << endl;

	for (auto e : s)
		cout << e << " ";
	cout << endl;
	system("pause");
	return 0;
}

Here Insert Picture Description

multimap

multimap basic operations

Precautions

  1. multimap The key can be repeated.
  2. multimap key elements in accordance with the default will be less than the compare
  3. It does not override the multimap operator[]operation
  4. Using the same map file header contains

#include #include

void TestMultimap1() 
{
 multimap<string, string> m; 
 m.insert(make_pair("李逵", "黑旋风")); 
 m.insert(make_pair("林冲", "豹子头")); 
 m.insert(make_pair("鲁达", "花和尚"));
  // 尝试插入key相同的元素 
  
 m.insert(make_pair("李逵", "铁牛")); 
 cout << m.size() << endl;
 
for (auto& e : m)    
cout << "<" << e.first << "," << e.second << ">" << endl;
    // key为李逵的元素有多少个    
    cout << m.count("李逵") << endl; return 0; 
}
 void TestMultimap2() 
 {    
 	 multimap<int, int> m;
   for (int i = 0; i < 10; ++i)    
   m.insert(pair<int, int>(i, i));
	for (auto& e : m)    
	cout << e.first << "--->" << e.second << endl; 
	cout << endl;
 
	 // 返回m中大于等于5的第一个元素 
 	auto it = m.lower_bound(5); 
 	cout << it->first << "--->" << it->second << endl;
 
 	// 返回m中大于5的元素 
 	it = m.upper_bound(5); 
 	cout << it->first << "--->" << it->second << endl;
 }

multiset

The basic operation of the multiset

multiset: only storage key, key can be repeated on key order

test

int main()
{
	int array[] = { 3, 1, 3, 4, 5, 6, 7, 8, 9, 0, 9, 4, 0, 7, 6, 2, 5, 8 };
		multiset<int>s;
		for (auto e : array)
			s.insert(e);
		cout << s.size() << endl;
	
		for (auto e : s)
			cout << e << " ";
		cout << endl;
	system("pause");
	return 0;
}

Here Insert Picture Description

Guess you like

Origin blog.csdn.net/liuyuchen282828/article/details/102508702