STL standard library of C++ study notes (7) map/multimap associative container

Include header file #include <map> declare namespace using namespace std;

Map/multimap is a type of associative container. Each node stores an element of type pair<const Key, T>. They will automatically sort the stored elements. Only const Key participates in the order comparison of storage. The value cannot be changed directly. The purpose of changing the element value can only be achieved by deleting the old value and inserting the new value. Multiset allows two adjacent elements to have keys in the same order, while map does not. This is their only difference. Associative container set, multiset and the following map, multimap uses a very efficient balanced retrieval binary tree: red-black tree.

Compared with set, multiset, and multimap, the template class map has a unique feature. It defines a member operator: T& operator[const Key& key], which associates the key value key with the value it maps. Can be used to find an element that is uniquely determined for a given key value, or to add one if no such element exists.

1) Constructor

  map(); //Default constructor, create an empty map object, such as: map<int,string> a;

  multimap(); //Default constructor, create an empty multimap object, such as: multimap<int,int> b;

  map(const Pred& comp); //Create an empty map object and specify the collation

  multimap(const Pred& comp); //Create an empty multimap object and specify the collation

  map(const map& x);//map copy constructor

  multimap(const multimap& x);//multimap copy constructor

  map(InIt first,InIt last); //Copy the elements in the sequence [first,last) into the map

  multimap(InIt first,InIt last); //Copy the elements in the sequence [first,last) into the multimap

  map(InIt first,InIt last,const Pred& comp);//Copy the elements in the sequence [first,last) sequence to map, and specify the collation

  multimap(InIt first,InIt last,const Pred& comp);//Copy the elements in the sequence [first,last) sequence to multimap, and specify the collation

2) Traversal function

  iterator begin(); //return the map/multimap header pointer, pointing to the first element

  iterator end(); //return the end pointer of map/multimap, pointing to the next position of the last element of map/multimap

  reverse_iterator rbegin(); //Reverse iterator, pointing to the last element

  reverse_iterator rend(); //reverse iterator, pointing to the position before the first element

  mapped_type operator[](const Key& key); //map is unique, access the element value corresponding to the key value

3) Judgment function

  bool empty() const; //When the controlled sequence is empty, the member function returns true

4) Size function

  size_type size() const; //return the length of the controlled sequence

  size_type max_size() const; //Return the length of the longest sequence that the container object can control

5) Interpolation function

  iterator insert(const T& val); //multimap, insert element val, and automatically sort

  pair<iterator,bool> insert(const T& val); //map, insert the element val, and automatically sort, why is the return a pair? There is a bool variable in the pair, indicating whether the insertion is successful, because the map does not allow duplicates The key value, so when inserting an element, you need to determine whether the key value already exists in the container

  iterator insert(iterator it, const T& val); //Specify to insert the element value val at iterator it. Note that it is only a reference position, because map and multimap are automatically sorted, so the insertion position is finally determined according to the size of the inserted element value, and the insertion position of this input is only given when the programmer knows the position of the sequence element Reference position, which may speed up insertion

  void insert(InIt first,InIt last); //Call insert(*it) once for each element in the sequence [first,last)

6) Delete function

  iterator erase(iterator it); //delete the element specified by the iterator it in the controlled sequence

  iterator erase(iterator first, iterator last); // delete all elements in the interval [first, last) in the controlled sequence

  size_type erase(const Key& key); //Delete the element whose element value is equal to key, and return the number of deleted elements

  void clear(); //clear the controlled sequence

7) Other operation functions

  void swap(map& x); //Swap map elements

  void swap(multimap& x); //Swap multimap elements

  key_compare key_comp() const; //Returns a function for comparing values ​​between elements

  value_compare value_comp() const; //returns a function for comparing values ​​between elements

  iterator find(const Key& key) const; //find function, return the iterator pointer whose element value is equal to key

  size_type count(const Key& key) const; //Returns the number of elements in the container whose elements are equal to the key. Since a key value can only appear 0 or 1 times in the map, it can be used to determine whether a key value is in the map. appear in the map

  iterator lower_bound(const Key& key) const; //Return the iterator pointer whose value is greater than or equal to key in the container

  iterator upper_bound(const Key& key) const; //Return the iterator pointer whose value is greater than key in the container

  pair<iterator,iterator> equal_range(const Key& key) const; //Return a pair of iterators, representing the first element greater than or equal to key and the first element greater than key, the return value is a pair type

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325125931&siteId=291194637