| Member function | Description |
data_type& operator[](const key_type& k) [3] | Returns a reference to the object that is associated with a particular key. If the map does not already contain such an object, operator[] inserts the default object data_type(). [3] |
Notes [1] Map::iterator is not a mutable iterator, because map::value_type is not Assignable. That is, if i is of type map::iterator and p is of type map::value_type, then *i = p is not a valid expression. However, map::iterator isn't a constant iterator either, because it can be used to modify the object that it points to. Using the same notation as above, (*i).second = p is a valid expression. The same point applies to map::reverse_iterator.
[2] This member function relies on member template functions, which at present (early 1998) are not supported by all compilers. If your compiler supports member templates, you can call this function with any type of input iterator. If your compiler does not yet support member templates, though, then the arguments must either be of type const value_type* or of type map::const_iterator.
[3] Since operator[] might insert a new element into the map, it can't possibly be a const member function. Note that the definition of operator[] is extremely simple: m[k] is equivalent to (*((m.insert(value_type(k, data_type()))).first)).second. Strictly speaking, this member function is unnecessary: it exists only for convenience.
See also Associative Container, Sorted Associative Container, Pair Associative Container, Unique Sorted Associative Container, set multiset, multimap, hash_set, hash_map, hash_multiset, hash_multimap
multiset<Key, Compare, Alloc>
Category: containers
Component type: type
Description Multiset is a Sorted Associative Container that stores objects of type Key. Multiset is a Simple Associative Container, meaning that its value type, as well as its key type, is Key. It is also a Multiple Associative Container, meaning that two or more elements may be identical.
Set and multiset are particularly well suited to the set algorithms includes, set_union, set_intersection, set_difference, and set_symmetric_difference. The reason for this is twofold. First, the set algorithms require their arguments to be sorted ranges, and, since set and multiset are Sorted Associative Containers, their elements are always sorted in ascending order. Second, the output range of these algorithms is always sorted, and inserting a sorted range into a set or multiset is a fast operation: the Unique Sorted Associative Container and Multiple Sorted Associative Container requirements guarantee that inserting a range takes only linear time if the range is already sorted.
Multiset has the important property that inserting a new element into a multiset does not invalidate iterators that point to existing elements. Erasing an element from a multiset also does not invalidate any iterators, except, of course, for iterators that actually point to the element that is being erased.
Example int main() {
const int N = 10;
int a[N] = {4, 1, 1, 1, 1, 1, 0, 5, 1, 0};
int b[N] = {4, 4, 2, 4, 2, 4, 0, 1, 5, 5};
multiset<int> A(a, a + N);
multiset<int> B(b, b + N);
multiset<int> C;
cout << 'Set A: ';
copy(A.begin(), A.end(), ostream_iterator<int>(cout, ' '));
cout << endl;
cout << 'Set B: ';
copy(B.begin(), B.end(), ostream_iterator<int>(cout, ' '));
cout << endl;
cout << 'Union: ';
set_union(A.begin(), A.end(), B.begin(), B.end(), ostream_iterator<int>(cout, ' '));
cout << endl;
cout << 'Intersection: ';
set_intersection(A.begin(), A.end(), B.begin(), B.end(), ostream_iterator<int>(cout, ' '));
cout << endl;
set_difference(A.begin(), A.end(), B.begin(), B.end(), inserter(C, C.begin()));
cout << 'Set C (difference of A and B): ';
copy(C.begin(), C.end(), ostream_iterator<int>(cout, ' '));
cout << endl;
}
Definition Defined in the standard header set, and in the nonstandard backward-compatibility header multiset.h.
Template parameters | Parameter | Description | Default |
Key | The set's key type and value type. This is also defined as |