Книга: Standard Template Library Programmer

multimap

multimap<Key, Data, Compare, Alloc>

Category: containers

Component type: type

Description

Multimap is a Sorted Associative Container that associates objects of type Key with objects of type Data. multimap is a Pair Associative Container, meaning that its value type is pair<const Key, Data>. It is also a Multiple Associative Container, meaning that there is no limit on the number of elements with the same key.

Multimap has the important property that inserting a new element into a multimap does not invalidate iterators that point to existing elements. Erasing an element from a multimap also does not invalidate any iterators, except, of course, for iterators that actually point to the element that is being erased.

Example

struct ltstr {
 bool operator()(const char* s1, const char* s2) const {
  return strcmp(s1, s2) < 0;
 }
};
int main() {
 multimap<const char*, int, ltstr> m;
 m.insert(pair<const char* const, int>("a", 1));
 m.insert(pair<const char* const, int>("c", 2));
 m.insert(pair<const char* const, int>("b", 3));
 m.insert(pair<const char* const, int>("b", 4));
 m.insert(pair<const char* const, int>("a", 5));
 m.insert(pair<const char* const, int>("b", 6));
 cout << "Number of elements with key a: " << m.count("a") << endl;
 cout << "Number of elements with key b: " << m.count("b") << endl;
 cout << "Number of elements with key c: " << m.count("c") << endl;
 cout << "Elements in m: " << endl;
 for (multimap<const char*, int, ltstr>::iterator it = m.begin(); it != m.end(); ++it)
  cout << " [" << (*it).first << ", " << (*it).second << "]" << endl;
}

Definition

Defined in the standard header map, and in the nonstandard backward-compatibility header multimap.h.

Template parameters

Parameter Description Default
Key The multimap's key type. This is also defined as multimap::key_type.
Data The multimap's data type. This is also defined as multimap::data_type.
Compare The key comparison function, a Strict Weak Ordering whose argument type is key_type; it returns true if its first argument is less than its second argument, and false otherwise. This is also defined as multimap::key_compare. less<Key>
Alloc The multimap 's allocator, used for all internal memory management. alloc

Model of

Multiple Sorted Associative Container, Pair Associative Container

Type requirements

• Data is Assignable.

• Compare is a Strict Weak Ordering whose argument type is Key.

• Alloc is an Allocator.

Public base classes

None.

Members

Member Where defined Description
key_type Associative Container The multimap 's key type, Key.
data_type Pair Associative Container The type of object associated with the keys.
value_type Pair Associative Container The type of object, pair<const key_type, data_type>, stored in the multimap.
key_compare Sorted Associative Container Function object that compares two keys for ordering.
value_compare Sorted Associative Container Function object that compares two values for ordering.
pointer Container Pointer to T.
reference Container Reference to T
const_reference Container Const reference to T
size_type Container An unsigned integral type.
difference_type Container A signed integral type.
iterator Container Iterator used to iterate through a multimap. [1]
const_iterator Container Const iterator used to iterate through a multimap.
reverse_iterator Reversible Container Iterator used to iterate backwards through a multimap. [1]
const_reverse_iterator Reversible Container Const iterator used to iterate backwards through a multimap.
iterator begin() Container Returns an iterator pointing to the beginning of the multimap.
iterator end() Container Returns an iterator pointing to the end of the multimap.
const_iterator begin() const Container Returns a const_iterator pointing to the beginning of the multimap.
const_iterator end() const Container Returns a const_iterator pointing to the end of the multimap.
reverse_iterator rbegin() Reversible Container Returns a reverse_iterator pointing to the beginning of the reversed multimap.
reverse_iterator rend() Reversible Container Returns a reverse_iterator pointing to the end of the reversed multimap.
const_reverse_iterator rbegin() const Reversible Container Returns a const_reverse_iterator pointing to the beginning of the reversed multimap.
const_reverse_iterator rend() const Reversible Container Returns a const_reverse_iterator pointing to the end of the reversed multimap.
size_type size() const Container Returns the size of the multimap.
size_type max_size() const Container Returns the largest possible size of the multimap.
bool empty() const Container true if the multimap 's size is 0.
key_compare key_comp() const Sorted Associative Container Returns the key_compare object used by the multimap.
value_compare value_comp() const Sorted Associative Container Returns the value_compare object used by the multimap.
multimap() Container Creates an empty multimap.
multimap(const key_compare& comp) Sorted Associative Container Creates an empty multimap, using comp as the key_compare object.
template <class InputIterator> multimap(InputIterator f, InputIterator l) [2] Multiple Sorted Associative Container Creates a multimap with a copy of a range.
template <class InputIterator> multimap(InputIterator f, InputIterator l, const key_compare& comp) [2] Multiple Sorted Associative Container Creates a multimap with a copy of a range, using comp as the key_compare object.
multimap(const multimap&) Container The copy constructor.
multimap& operator=(const multimap&) Container The assignment operator
void swap(multimap&) Container Swaps the contents of two multimaps.
iterator insert(const value_type& x) Multiple Associative Container Inserts x into the multimap.
iterator insert(iterator pos, const value_type& x) Multiple Sorted Associative Container Inserts x into the multimap, using pos as a hint to where it will be inserted.
template <class InputIterator> void insert(InputIterator, InputIterator) [2] Multiple Sorted Associative Container Inserts a range into the multimap.
void erase(iterator pos) Associative Container Erases the element pointed to by pos.
size_type erase(const key_type& k) Associative Container Erases the element whose key is k.
void erase(iterator first, iterator last) Associative Container Erases all elements in a range.
void clear() Associative Container Erases all of the elements.
iterator find(const key_type& k) Associative Container Finds an element whose key is k.
const_iterator find(const key_type& k) const Associative Container Finds an element whose key is k.
size_type count(const key_type& k) Associative Container Counts the number of elements whose key is k.
iterator lower_bound(const key_type& k) Sorted Associative Container Finds the first element whose key is not less than k.
const_iterator lower_bound(const key_type& k) const Sorted Associative Container Finds the first element whose key is not less than k.
iterator upper_bound(const key_type& k) Sorted Associative Container Finds the first element whose key greater than k.
const_iterator upper_bound(const key_type& k) const Sorted Associative Container Finds the first element whose key greater than k.
pair<iterator, iterator> equal_range(const key_type& k) Sorted Associative Container Finds a range containing all elements whose key is k.
pair<const_iterator, const_iterator> equal_range(const key_type& k) const Sorted Associative Container Finds a range containing all elements whose key is k.
bool operator==(const multimap&, const multimap&) Forward Container Tests two multimaps for equality. This is a global function, not a member function.
bool operator<(const multimap&, const multimap&) Forward Container Lexicographical comparison. This is a global function, not a member function.

New members

All of multimap 's members are defined in the Multiple Sorted Associative Container and Pair Associative Container requirements. Multimap does not introduce any new members.

Notes

[1] Multimap::iterator is not a mutable iterator, because multimap::value_type is not Assignable. That is, if i is of type multimap::iterator and p is of type multimap::value_type, then *i = p is not a valid expression. However, multimap::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 multimap::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 multimap::const_iterator.

See also

Associative Container, Sorted Associative Container, Pair Associative Container, Multiple Sorted Associative Container, set, map, multiset, hash_set, hash_map, hash_multiset, hash_multimap

Оглавление книги

Оглавление статьи/книги

Генерация: 1.497. Запросов К БД/Cache: 3 / 1
поделиться
Вверх Вниз