Detailed explanation of STL associated container

STL associative container category 1. map Defined in the header file, the key of each element of the data stored in thi...
STL associative container category
map does not allow duplicate keys

STL associative container category

1. map
Defined in the header file, the key of each element of the data stored in this container must be unique (that is, it cannot be repeated). The container will sort in ascending order by default according to the size of the key of each element (call std::less).
2. set
Defined in the header file, using the data stored in the container, the keys and values of each element are identical, and the values of each element cannot be repeated (ensuring the uniqueness of each element key). The container will automatically sort in ascending order according to the size of each element's key (in fact, the element value) (call std::less).
3. multimap
Defined in the header file, the only difference from the map container is that the keys for storing elements in the multimap container can be repeated.
4. multiset
Defined in the header file, the only difference from the set container is that the values of the elements stored in the multiset container can be repeated (once the values are repeated, it means that the keys are also repeated).

map does not allow duplicate keys

Considering that the "key value pair" is not a common type of data, the C++ STL standard library provides a pair class template, which is specially used to combine the two common elements first and second
The pair object is also overloaded with <, < =, >, > =, = == The operation rule of these 6 operators is: for the two pair objects to be compared, first compare the size of pair.first element. If they are equal, continue to compare the size of pair.second element.

#include<bits/stdc++.h> using namespace std; pair<string,int>p; map<string,int,greater<int> >m; int main(){ pair <string, int> pair1("STL course", 20); pair <string, int> pair3("C++course", 30); // It is equivalent to modifying the key value pair <string, int> pair2("C++course", 20); m.insert(pair1); m.insert(pair2); m.insert(pair3); for(auto it:m){ cout<<it.first<<" "<<it.second<<endl; } // You can also access and modify directly cout<<"***********"<<endl; m["C++course"]=50; for(auto it:m){ cout<<it.first<<" "<<it.second<<endl; } return 0; }


Sort from large to small

#include<bits/stdc++.h> using namespace std; pair<string,int>p; map<string,int,greater<string> >m;//From big to small int main(){ pair <string, int> pair2("C++course", 30); pair <string, int> pair1("STL course", 20); m.insert(pair1); m.insert(pair2); for(auto it:m){ cout<<it.first<<" "<<it.second<<endl; } return 0; }


Common member methods

  • find(key)
    Find the key value pair with key in the map container. If it is found successfully, return the bidirectional iterator pointing to the key value pair; Otherwise, the same iterator as the end() method is returned. In addition, if the map container is qualified with const, the method returns a bidirectional iterator of type const.
#include<bits/stdc++.h> using namespace std; pair<string,int>p; map<string,int>m;//From big to small int main(){ pair <string, int> pair2("C++course", 30); pair <string, int> pair1("STL course", 20); m.insert(pair1); m.insert(pair2); auto it=m.find("C++course"); if(it!=m.end()){ cout<<it->second<<endl; }else{ cout<<"not find"<<endl; } it=m.find("000"); if(it!=m.end()){ cout<<it->second<<endl; }else{ cout<<"not find"<<endl; } return 0; }


The following usage will be changed!!!

  • lower_bound(key)
    Returns a bidirectional iterator pointing to the first key value pair greater than or equal to key in the current map container. If the map container is qualified with const, the method returns a bidirectional iterator of type const.
  • upper_bound(key)
    Returns an iterator pointing to the first key value pair greater than key in the current map container. If the map container is qualified with const, the method returns a bidirectional iterator of type const.
  • equal_range(key)
    This method returns a pair object (containing two bidirectional iterators), where pair.first and lower_ The return value of the bound () method is equivalent, pair.second and upper_ The return value of the bound () method is equivalent. That is, the method will return a range containing key value pairs (the map container key value pairs are unique, so the range contains at most one key value pair).
  • empty()
    If the container is empty, return true; Otherwise, false.
  • size()
    Returns the number of key value pairs stored in the current map container.
  • max_size()
    Returns the maximum number of key value pairs that the map container can hold. The return values are different for different operating systems.
  • operator[]
    The [] operator is overloaded in the map container. As long as you know the value of the key of a key value pair in the map container, you can directly obtain the corresponding value through the key like obtaining the elements in the array.
  • at(key)
    Find the value corresponding to the key key in the map container. If it is not found, the function will raise out_of_range exception.
  • insert()
    Insert key value pairs into the map container.
  • erase()
    Delete the specified location, key value or key value pair in the specified area of the map container. The following chapters will focus on this method.
  • swap()
    Exchange the key value pairs stored in the two map containers, which means that the two key value pairs of the operation must be of the same type.
  • clear()
    Empty all key value pairs in the map container, even if the size() of the map container is 0.
  • emplace()
    Constructs a new key value pair at the specified location in the current map container. The effect is the same as inserting key value pairs, but it is more efficient.
  • emplace_hint()
    Essentially, the method constructs a new key value pair in the map container in the same way as replace (), except that the user must provide the method with an iterator indicating the generation position of the key value pair as the first parameter of the method.
  • count(key)
    In the current map container, find the number of key value pairs with key and return. Note that since the key value of each key value pair in the map container is unique, the maximum return value of this function is 1.

26 November 2021, 04:03 | Views: 7268

Add new comment

For adding a comment, please log in
or create account

0 comments