Multimap
Map and multimap containers are containers that manage key/value pairs as elements.
Definition
The multimap template is defined in the standard header <map>, and in the nonstandard backward-compatibility header <multimap.h>.
#include <map>
namespace std{
template < class Key, class T,
class Compare = less<Key>,
class Allocator = allocator<pair<const Key,T> > >
class multimap;
}
Description
Multimap container, just like map container manage key/value pairs as elements.
The difference between the two is that a multimap allows different elements to have the same key value. (Because there is no limit on the number of elements with the same key multimap is also a Multiple Associative Container.)
Another difference between the two is that multimap does not support the brackets operator ([]) for element access, since there might be several different keys with the same value.
A short example declaring a map is:
map<string, integer> aMap; // associates strings with integer
Performance
Multimap have the important property that gives fast access to stored values of type T that are indexed by keys of type Key. The default operation for key comparison is the < operator.
Multimap supports bidirectional iterators that point to an instance of pair <const Key x, T y> where x is the key and y is the stored value associated with that key. The definition of multimap refers to a typedef to this pair called value_type.
See also: http://codecogs.izyba.com/reference/computing/containers/map.php"Map"
Multimap Operations
Create, Copy, and Destroy Operations
| Operation | Effect |
| map mm | Creates an empty multimap without any elements |
| map mm(op) | Creates an empty multimap that uses op as the sorting criterion |
| map mm1(mm2) | Creates a copy of another multimap of the same type (all elements are copied) |
| map mm(beg,end) | Creates a multimap initialized by the elements of the range [beg,end) |
| map mm(beg,end,op) | Creates a multimap with the sorting criterion op initialized by the elements of the range [beg,end) |
| mm.~map() | Destroys all elements and frees the memory |
| Multimap | Effect |
| multimap<Key,el> | A multimap that sorts keys with less<> (operator <) |
| multimap<Key,el,op> | A multimap that sorts keys with op |
Nonmodifying Operations of Multimaps
| Operation | Effect |
| mm.size() | Returns the actual number of elements |
| mm.empty() | Returns if the container is empty |
| mm.max_size() | Returns the maximum number of elements possible |
| mm1==mm2 | Returns if mm1 is equal to mm2 |
| mm1!=mm2 | Returns if mm1 is not equal to mm2 (equivalent to !(mm1==mm2)) |
| mm1<mm2 | Returns if mm1 is less than mm2 |
| mm1>mm2 | Returns if mm1 is greater than mm2 |
| mm1<=mm2 | Returns if mm1 is less than or equal to mm2 (equivalent to !(mm2<mm1)) |
| mm1>=mm2 | Returns if mm1 is greater than or equal to mm2 (equivalent to !(mm1<mm2)) |
Special Search Operations of Multimaps
| Operation | Effect |
| count(key) | Returns the number of elements with key key |
| find(key) | Returns the position of the first element with key key or end() |
| lower_bound(key) | Returns the first position where an element with key key would get inserted (the first element with key >= key) |
| upper_bound(key) | Returns the last position where an element with key key would get inserted (the first element with key > key) |
| equal_range(key) | Returns the first and last positions where elements with key key would get inserted (the range of elements with key == key) |
Assignment Operations of Maps
| Operation | Effect |
| mm1=mm2 | Assigns all elements of mm2 to mm1 |
| mm1.swap(mm2) | Swaps the data of mm1 and mm2 |
| swap(c1,c2) | Same (as global function) |
Iterator Operations of Multimaps
| Operation | Effect |
| mm.begin() | Returns a bidirectional iterator for the first element (keys are considered const) |
| mm.end() | Returns a bidirectional iterator for the position after the last element (keys are considered const) |
| mm.rbegin() | Returns a reverse iterator for the first element of a reverse iteration |
| mm.rend() | Returns a reverse iterator for the position after the last element of a reverse iteration |
Inserting and Removing Elements of Maps
| Operation | Effect |
| mm.insert(el) | Inserts a copy of el and returns the position of the new element and, for maps, if it succeeded |
| mm.insert(pos,el) | Inserts a copy of el and returns the position of the new element (pos is used as a hint pointing to where the insert should start the search) |
| mm.insert(beg,end) | Inserts a copy of all elements of the range [beg,end) (returns nothing) |
| mm.erase(el) | Removes all elements with value el and returns the number of removed elements |
| mm.erase(pos) | Removes the element at iterator position pos (returns nothing) |
| mm.erase(beg,end) | Removes all elements of the range [beg,end) (returns nothing) |
| mm.clear() | Removes all elements |
References
- http://stdcxx.apache.org
- http://www.sgi.com
- Nicolai M. Josuttis: "The C++ Standard Library"