- Multimap, like map, uses a red-black tree to make quick insertions, deletions, and retrievals by comparing relationships of element key values to record-based element data, unlike Multimap, which allows elements with duplicate key values to be inserted.
- In the Multimap container, the relationship between the key value of the element and the mapping data of the element is many-to-many, that is, the Multimap is a multi-mapping container.
- Because the element key value allows repetition, so that the array operator "[]" uses the key value to access the element is meaningless, so Multimap does not define the array mode of "[]" Operation operations.
Multimap and the map container share the same C + + standard header file map, so you need to add a header file #include<map>
to compile and run.
Create a Multimap object
- Multimap (); Create a Multimap object without any elements
- Multimap (constkey_compare& comp); Specifies a comparison function object comp to create the Multimap object, and the memory allocator as the default value.
- Multimap (constmap&); A copy constructor that copies a new Multimap container object with a Multimap container element and comparison function.
- Multimap (Inputiteratorfirst, Inputiterator last); Creates a Multimap container object as an element of the Multimap container (including key values and mapping data), using the data referred to by the iterator interval [first].
Insertion of elements
Like the map container, the Multimap container can use the Insert function to insert elements into the container's red-black tree structure. However, you cannot use the array operator "[]" to add elements like map.
pair<iterator,bool>insert(const value_type& v)
Inserts the element V (including the key value and the mapping data) into the Multimap container, allowing the key value of V and the key value of an element in the container to repeat. Returns a pair pair object that provides the iterator position of the inserted element and the True/false Insert success flag.
iteratorinsert(iterator position, const value type& v)
inserting element V (including key values and mapping data) into the Multimap container, the parameter position just prompts to insert v before the position position, and the insertion position returned is not necessarily inserted before the position position, depending on the situation.
voidinsert(InputIterator first, InputIterator last)
Inserts the data referred to by the iterator interval [first,last] as a container element (including key values and mapping data) into the Multimap container.
Deletion of elements
void erase(iteratorposition)
Delete the element that position refers to
size_type erase(const key_type& k)
Deletes an element equal to the key value K, returning the number of deleted elements
void erase(iterator first, iterator last)
Delete all elements on the map iterator interval [first,last]
void clear()
Delete all elements of the map container
Traversal access of elements
Unlike the map container, the Multimap container can only be used as an iterator, and cannot be traversed in an array manner.
- Iterator Begin ()
- Iterator End ()
- Reverse_iterator Rbegin ()
- Reverse_iterator rend ()
Other functions
Same as map. In short, the application is still very convenient, the following will be the implementation of the source code analysis.
Reprint Please specify source: http://blog.csdn.net/lsh_2013/article/details/46839513
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
STL's Multimap Container