[C ++] - how to use the map and set what the difference between them??

Foreword

We know that the C ++ STL library not only provides us with vector, string, list and other convenient containers, more importantly, STL package many complex data structures, algorithms and data structures operations. Packaging vector array, list the package list, map and set the binary packages , in these containers, will provide the basis for the operation: add, delete, sort, search and the like.

1、set

map and set are associated container, he is characterized by adding or deleting impact on the iterator is very small, in addition to the node operation, almost no impact on other nodes

set is used to store the same type of data, which elements are only a key value , he supports efficient keyword query and insert operations, such as checking whether a keyword in the set. If it existed before this key value, it is not inserted.

The following is a commonly used functions on the map and set, here insert, find and delete methods:
Here Insert Picture Description

Insert insert:

	set<int> s;
	s.insert(1);
	s.insert(1);
	s.insert(2);
	s.insert(3);
	s.insert(3);
	s.insert(4);
	for (auto e : s)
	{
		cout << e << " ";
		++e;
	}
	cout << endl;
	//或者你可以采用一下输出方式
	//set<int> iterator::it;
	//for(it=s.begin();it!=s.end();it++)
	//{ 
	//		cout<<*it<<" ";
	//}
	//	cout<<endl;
	

Run shot:
Here Insert Picture Description
you can see the insert is set to re-sort +
Find Find:

set<int>::iterator pos = s.find(3);
	if (pos != s.end())
	{
		cout << "找到啦!"<<endl;
	}

Run shot:
Here Insert Picture Description
delete erase:

s.erase(pos);
	cout << "删除后:" << endl;
	for (auto e : s)
	{
		cout << e << " ";
		++e;
	}
	cout << endl;

Screenshot:
Here Insert Picture Description
Empty clear:

s.clear();
	for (auto e : s)
	{
		cout << e << " ";
		++e;
	}

Screenshot:
Here Insert Picture Description

2、map

map are the same as set and the associated container, the bottom of the container they are red-black tree, the difference is that the map is not the value as a key and key values ​​are separated

Insert Insert

map<string, string> m;
	m.insert(make_pair("Apple", "苹果"));
	m.insert(make_pair("Pair", "梨"));
	m.insert(make_pair("Right", "右边"));
	//m.insert(make_pair("Right", "正确"));
	m.insert(make_pair("Banana", "香蕉"));
	m.insert(make_pair("Orange", "橘子"));
	m.insert(make_pair("Grap", "葡萄"));

	for(map<string,string>::iterator e=m.begin();e!=m.end();e++)
	{
		cout << e->first << ":" << e->second << endl;
	}
	cout << endl;

Find Find

it = m.find("Banana");
	if (it != m.end())
	{
		cout << "找到了!该单词的中文是:" << it->second << endl;
	}
	else
	{
		cout << "没有找到!";
	}
	cout << endl;

Delete erase

m.erase(it);
	cout << "删除刚找到的元素后,剩余的元素为:" << endl;
	map<string, string>::iterator e1;
	for (e1 = m.begin(); e1 != m.end(); e1++)
	{
		cout << e1->first << ":" << e1->second << endl;
	}

3, the difference between the map and set of

set the following features:

  • is set as the underlying container RBTree
  • The resulting element is not only a key value, value is the key
  • Do not allow duplicate key values
  • The resulting element to automatically re-ordering +
  • Iterator can not be set to change the value of

map features are as follows:

  • In the bottom of the container as a map RBTree
  • All elements are present in the key + value
  • Do not allow duplicate keys
  • All the elements are automatically sorted by key
  • The map key can not be changed, but the value corresponding to the keys can be modified

Set on a simple application and the map, posted a connection here, is the number of statistics fruits appear, and the statistics of the fruit before topk the favorite count the number of fruits

4、multiset和multimap

The Chinese name is multiset multiset
extended version is actually a collection of, the difference is in the set, a value can appear only once, and in multiple collections, a value can occur multiple times. And a number of insertion and deletion can be done in time O (logN), it can ensure the number in the sequence is ordered.

multimap and supported by the same map operations (except multimap does not support subscripting), multimap allow duplicate elements. The following are common interface code demonstrates the multimap:

#include <map> 
#include <string> 
#include <iostream> 
using namespace std; 
 
int main() 
{ 
  ///1. 初始化 
  multimap<int, string> mapStudent; 
  multimap<int, string>::iterator iter, beg, end; 
   
  ///2. 添加元素 
  ///multimap不支持下标操作 
  mapStudent.insert(pair<int, string>(0, "student_one")); 
  mapStudent.insert(pair<int, string>(0, "student_one_copy"));///一对多 
  mapStudent.insert(pair<int, string>(1, "student_two")); 
  mapStudent.insert(pair<int, string>(5, "Fear Kubrick")); 
  mapStudent.insert(pair<int, string>(2, "Akemi Homura")); 
  mapStudent.insert(pair<int, string>(-1, "Eren Jaeger")); 
  mapStudent.insert(pair<int, string>(99, "lin")); 
  cout << mapStudent.size() << endl; 
  cout << endl; 
   
  ///3. 遍历 
  for (iter = mapStudent.begin(); iter != mapStudent.end(); iter++) 
    cout << iter->first << " " << iter->second << endl; 
  cout << endl; 
   
  ///4. 单键查询与范围查询 
  ///单键查询 
  int count = mapStudent.count(0); 
  iter = mapStudent.find(0); 
  for (int i = 0; i < count; i++, iter++) 
    cout << iter->first << " " << iter->second << endl; 
  cout << endl; 
  ///范围查询 
  beg = mapStudent.lower_bound(1);/// >=1 
  end = mapStudent.upper_bound(5);/// <=5 
  for (; beg != end; beg++) 
    cout << beg->first << " " << beg->second << endl; 
  cout << endl; 
   
  ///5. 删除 
  iter = mapStudent.find(1); 
  mapStudent.erase(iter); 
  cout << mapStudent.size() << endl; 
  for (iter = mapStudent.begin(); iter != mapStudent.end(); iter++) 
    cout << iter->first << " " << iter->second << endl; 
  cout << endl; 
   
  ///6. 判空与清空 
  if (!mapStudent.empty()) 
    mapStudent.clear(); 
} 

Published 33 original articles · won praise 13 · views 1047

Guess you like

Origin blog.csdn.net/Vicky_Cr/article/details/104640922