Introduction to STL associated containers: the associated container is the key-Value Pair container. The key is used to store and read elements. There are four associated containers in STL:
- Map key-value pairs are stored in key-value pairs. Keys cannot be duplicated. That is, a key can only correspond to one value, corresponding to the header file <map>
- The multimap key-value pairs are stored in key-value pairs. The key can be repeated. That is, one key can correspond to multiple values, corresponding to the header file <map>
- Set has only the key, and the key cannot be repeated. The corresponding header file <set>
- Multiset only has the key and the key can be repeated. The corresponding header file <set>
STL associated container features
- The underlying data structure of STL associated containers is a red-black tree, so the time complexity of addition, deletion, and query is O (logn)
- By default, map is inserted in ascending order of keys. Non-basic data types must be reloaded. <Operator
- Map reloads the [] operator to facilitate insertion and search.
- When map uses the [] operator to access an element, if this key does not exist, the key is automatically inserted and the value is the initial value.
- Do not modify the key object of MAP after it is used. If it must be modified, delete it and insert it again.
- The key-value of multimap is one-to-multiple, and the [] operator is not overloaded.
Map common functions
# Structure: map C: # create an empty ing that does not contain any element map C (OP): # Use OP as the sorting criterion to generate an empty mapmap C1 (C2 ): # copy the elements in C2 to map C (const value_type * First, const value_type * Last) in C1: # copy [first, last) the elements form a new ing between map C (const value_type * First, const value_type * Last, OP): # Use OP as the sorting criterion and copy [first, last) the elements form a new multiing. multimap M: # create an empty ing and do not contain any elements. multimap M (OP): # Use OP as the sorting criterion to generate an empty multimapmultimap M1 (m2 ): # copying the elements in m2 to M1 multimap M (const value_type * First, const value_type * Last): # copying [first, last) the elements form a new multiing multimap M (const value_type * First, const value_type * Last, OP): # copy [first, last) using OP as the sorting criterion) new ing between elements # add and delete iterator insert (const value_type & X): # insert element xiterator insert (iterator it, const value_type & X ): # Insert the element xvoid insert (const value_type * First, const value_type * Last) in the iteration pointer it: # Insert the iterator erase (iterator it) element between [first, last ): # Delete the iterator erase (iterator first, iterator last) element in it: # Delete the size_type erase (const key & Key) element between [first, last ): # deleting an element whose key value is equal to the key # traversing iterator begin (): # returning the iterator pointer to the first element iterator end (): # returning the iterator pointer to the last element reverse_iterator rbegin (): # returns the reverse iterator pointer reverse_iterator rend (): # returns the iterator pointer reference operator [] (const key & K) at the previous position of the first element ): # Only use the ing map class to overload [] and return a reference # function int size () const: # Return the number of container elements bool empty () const: # Check whether the container is empty. If true is returned, the container is empty. const_iterator find (const key & Key) const: # Find the iterator pointer int count (const key & Key) const: # number of elements whose return key value is equal to key const_iterator lower_bound (const key & Key ): # const_iterator upper_bound (const key & Key): # pair <const_iterator, const_iterator> performance_range (const key & Key) const: # returns a pair of iterators so that the element in [first, last) is equal to the key
Set common functions
# Construct set C: # create an empty set that does not contain any element set C (OP): # Use OP as the sorting criterion to generate an empty setset C1 (C2 ): # copy the elements in C2 to set C (const value_type * First, const value_type * Last) in C1: # copy [first, last) the elements form a new set C (const value_type * First, const value_type * Last, OP): # Use OP as the sorting criterion and copy [first, last) the elements between them constitute the new set Multiset M: # create an empty set and do not contain any element Multiset M (OP): # Use OP as the sorting criterion to generate an empty setmultiset M1 (m2 ): # copy the elements in m2 to M1 Multiset M (const value_type * First, const value_type * Last): # copy [first, last) the elements form a new set Multiset M (const value_type * First, const value_type * Last, OP): # Use OP as the sorting criterion and copy [first, last) elements between them form a new set # add or delete pair <iterator, bool> insert (x): # insert element xiterator insert (iterator it, X ): # Insert the element xvoid insert (const value_type * First, const value_type * Last) in the it part of the iterator: # insert iterator erase (iterator it) between [first, last ): # Delete the iterator erase (iterator first, iterator last) element in it: # Delete the size_type erase (const key & Key) element between [first, last ): # deleting elements whose element value is equal to the key # traversing iterator begin (): # returning the iterator pointer to the first element iterator end (): # returning the iterator pointer to the last element reverse_iterator rbegin (): # returns the reverse iterator pointer reverse_iterator rend (): # returns the iterator pointer at the previous position of the first element # function int size () const: # Return the number of container elements bool empty () const: # determine whether the container is empty. If true is returned, the container is empty. const_iterator find (const key & Key) const: # Find the iterator pointer int count (const key & Key) const: # Return the number of elements in the container whose element is equal to key. const_iterator lower_bound (const key & Key): # const_iterator upper_bound (const key & Key): # pair <const_iterator, const_iterator> performance_range (const key & Key) const: # returns a pair of iterators so that the elements in [first, last) are equal to keyvoid swap (set & S ): # switching set element void swap (Multiset & S): # switching multiple set Elements
Map example
# Include <iostream >#include <map> using namespace STD; Class cat {public: int age; CAT (INT age) {This-> age = age ;} /* the bottom layer of map is a red/black tree. Objects used as keys must be sorted. The <operator */bool operator <(const cat & C) const {return age <C. age ;}}; int main () {Map <char, char> m; M. insert (Map <char, char >:: value_type ('A', 'A'); m ['B'] = 'B '; cout <"m ['a'] is:" <m ['a'] <","; // use the operator [] to Access Map <char, char>: iterator it = m. find ('B ');// Access cout with an iterator <"m ['B'] is:" <it-> second <","; cout <"m ['C'] is:" <m ['C'] <Endl; // use [] to determine whether access exists first, if it does not exist, M ['C'] = 'C'; m ['D'] = 'D'; For (Map <char, char> :: iterator it = m. begin (); it! = M. end (); It ++) {cout <it-> first <":"; // 'first' is the key and cannot be modified (const modification) cout <(* it ). second <","; // 'second' is value, which can be modified} Map <char, char>: iterator it_low, it_up; it_low = m. lower_bound ('C'); it_up = m. upper_bound ('C'); cout <"\ nlower_bound ('C') is:" <it_low-> first <Endl; cout <"upper_bound ('C') is:" <it_up-> first <Endl; pair <Map <char, char >:: iterator, Map <char, char>: iterator> ret; ret = m. pai_range ('C'); cout <"pai_range ('C'):" <ret. first-> first <"," <ret. second-> first <Endl; Map <cat, char> T; cat c (1); t [c] = 'a'; C. age = 2; // after the key object is modified, you cannot find this key-Value Pair cout from Map <"after modifying the key object, and then find the number of times this key appears again: "<t. count (c) <Endl;}/* output: M ['a'] is: a, m ['B'] is: B, M ['C'] is: A, B: B, C: C, D: D, lower_bound ('C') is: C upper_bound ('C ') is: D sort _range ('C'): c, d after modifying the key object, find the number of times this key appears again: 0 */
[Address: http://blog.csdn.net/thisinnocence/article/details/39646813]
STL notes (2): Associate container map, set, multimap, and multimap