Maps of C ++ standard library learning # include <map>
C ++ Maps is an associated container that contains "key/value" Pairs
1. Insert an element:
Insert () function:
1) insert val to the back of pos and return an iterator pointing to this element:
Iterator insert (iterator pos, const pair <KEY_TYPE, VALUE_TYPE> & val );
2) insert the start-to-end elements to the map:
Void insert (input_iterator start, input_iterator end );
3) Insert val only when val does not exist. The returned value is an iterator pointing to the inserted element and a bool value that describes whether to insert the element:
Pair <iterator, bool> insert (const pair <KEY_TYPE, VALUE_TYPE> & val );
Note: it is complicated to insert elements, so it is necessary to take time to study them.
2. delete an element:
The erase () function deletes the elements at the pos position, the elements between start and end, or all elements whose values are key.
Syntax:
Void erase (iterator pos );
Void erase (iterator start, iterator end );
Size_type erase (const KEY_TYPE & key)
3. Search for an element:
Iterator find (const KEY_TYPE & key): returns an iterator pointing to an element whose key value is key. If no key is found, the iterator pointing to the end of map is returned.
Iterator lower_bound (const KEY_TYPE & key): returns an iterator pointing to the first element of key> = key in the map.
Iterator upper_bound (const KEY_TYPE & key): returns an iterator pointing to the first element of key> key in the map.
4. iterator begin (): returns the first element that an iterator points to a map.
Iterator end (): returns an iterator pointing to the end of the map.
Rbegin () returns a reverse iterator pointing to the end of the map.
Rend () returns a reverse iterator pointing to the map header.
5. size_type count (const KEY_TYPE & key): returns the number of elements whose key value is the same as the key in the map.
Pair performance_range (const KEY_TYPE & key): returns two iterators-one pointing to the first key-value element and the other pointing to the last key-value element.
6. get_allocator () returns the map configurator (not understanding !!! I have heard of the adapter mode)
Key_compare key_comp (): returns a function that compares keys. (Do not understand)
Value_compare value_comp (): returns a function that compares the value of an element.
7. Other common functions:
Max_size () returns the maximum number of elements that can be accommodated.
Clear () delete all elements
Empty () returns true if map is empty.
Size () returns the number of elements in the map.
Swap (map & obj): swap elements in obj and current map.
This article is from the "Change" blog