The underlying data structure of the associative container: unordered_map/set is based on the hash table, and the insertion order is not guaranteed; map/set is based on the red-black tree and automatically sorted according to the key

The elements in the associative container are saved and accessed by keywords, which supports efficient keyword search and access.

1. Map container:
The elements in the map container are key-value pairs : the key functions as an index, and the value represents the data associated with the index.
The type of map associative container:

  • map :Associative array based on red-black tree; Save key-value pairs;The data is stored after the key is automatically sorted
  • multimap: A map whose keywords can be repeated
  • unordered_map:In a map organized by a hash function, the data in the container is stored out of order, and it is not guaranteed to be consistent with the insertion order
  • unordered_multimap: map organized by hash; keywords can appear repeatedly
map<string, int> val1;
map<string, int> val2 = {
    
    {
    
    "111",1},{
    
    "222",4}};
map<string, int> val3 = val2 ;
auto ptr = val3.find("111"); // 返回的是迭代器类型
val["234"] = 4; // 最常用的插入方式
val3.insert(pair<string,int>("123",3)); // 插入的方式
val3.erase(ptr); // 删除的方法1
val3.erase("123"); // 删除的方法2

Note: due tomap does not contain duplicate keywords, So repeated insertion of an existing keyword element will not be added to the map container.The value corresponding to the keyword stored in the map container is still the value corresponding to the keyword added to the container for the first time

2. Set container:
Each element in the set container contains only one keyword . You can think of it this way. In fact, a set is a collection used to store elements of the same type.
Type of set associative container:

  • set: The keyword is the value, that is, the container that only stores the keyword,The underlying data structure is a red-black tree, Orderly, not repeated;
  • multiset: The underlying data structure is a red-black tree, which is ordered and repeatable.
  • unordered_set: unordered set
  • unordered_multiset: an unordered set where keywords can be repeated
set<string> a1={
    
    "fengxin","666"};
set<string> a2=a1;
set<string> a;  //empty set
a.insert("123");  // 插入一个元素
a.erase("123");    //删除关键字为123的元素
int numb = a.count("123"); // 返回0表示没有
auto ptr = val3.find("fengxin"); // 返回的是迭代器类型,不是val3.end()则表明存在

Note: unordered: The bottom layer is implemented by a hash table (hash bucket algorithm). If there is no such keyword, the bottom layer is implemented by a red-black tree.

Why use unordered_map instead of hash_map?
Because of the advancement of standardization, unordered_map originally belonged to the boost branch and std::tr1, while hash_map belonged to non-standard containers.
In addition, after use, it feels that the speed is similar to hash_map, but it supports string as a key, and complex objects can also be used as keys.

Reference: C++ associative container summary

to sum up:

1. The underlying data structure of unordered_map/set is based on the hash table, so the insertion order is inconsistent with the order in the container. If you want to keep the insertion order consistent with the order in the container, you need to use a linked list list<pair<int,int>> container, or use a linked list to save the key, and use map to maintain the corresponding value .
2. The underlying data structure of map/set is based on a red-black tree, which is automatically sorted according to the key.

Guess you like

Origin blog.csdn.net/qq_33726635/article/details/106553317