STLdoc
STLdocumentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Functions
stl_multimap.h File Reference
#include <bits/concept_check.h>

Go to the source code of this file.

Functions

namespace std _GLIBCXX_VISIBILITY (default)
 

Detailed Description

This is an internal header file, included by other library headers. Do not attempt to use it directly. {map}

Function Documentation

namespace std _GLIBCXX_VISIBILITY ( default  )

A standard container made up of (key,value) pairs, which can be retrieved based on a key, in logarithmic time.

Template Parameters
_KeyType of key objects.
_TpType of mapped objects.
_CompareComparison function object type, defaults to less<_Key>.
_AllocAllocator type, defaults to allocator<pair<const _Key, _Tp>.

Meets the requirements of a container, a reversible container, and an associative container (using equivalent keys). For a multimap<Key,T> the key_type is Key, the mapped_type is T, and the value_type is std::pair<const Key,T>.

Multimaps support bidirectional iterators.

The private tree data is declared exactly the same way for map and multimap; the distinction is made entirely in how the tree functions are called (*_unique versus *_equal, same as the standard).

This turns a red-black tree into a [multi]map.

The actual tree structure.

Default constructor creates no elements.

Creates a multimap with no elements.

Parameters
__compA comparison object.
__aAn allocator object.

Multimap copy constructor.

Parameters
__xA multimap of identical element and allocator types.

The newly-created multimap uses a copy of the allocation object used by __x.

Builds a multimap from a range.

Parameters
__firstAn input iterator.
__lastAn input iterator.

Create a multimap consisting of copies of the elements from [__first,__last). This is linear in N if the range is already sorted, and NlogN otherwise (where N is distance(__first,__last)).

Builds a multimap from a range.

Parameters
__firstAn input iterator.
__lastAn input iterator.
__compA comparison functor.
__aAn allocator object.

Create a multimap consisting of copies of the elements from [__first,__last). This is linear in N if the range is already sorted, and NlogN otherwise (where N is distance(__first,__last)).

The dtor only erases the elements, and note that if the elements themselves are pointers, the pointed-to memory is not touched in any way. Managing the pointer is the user's responsibility.

Multimap assignment operator.

Parameters
__xA multimap of identical element and allocator types.

All the elements of __x are copied, but unlike the copy constructor, the allocator object is not copied.

Get a copy of the memory allocation object.

Returns a read/write iterator that points to the first pair in the multimap. Iteration is done in ascending order according to the keys.

Returns a read-only (constant) iterator that points to the first pair in the multimap. Iteration is done in ascending order according to the keys.

Returns a read/write iterator that points one past the last pair in the multimap. Iteration is done in ascending order according to the keys.

Returns a read-only (constant) iterator that points one past the last pair in the multimap. Iteration is done in ascending order according to the keys.

Returns a read/write reverse iterator that points to the last pair in the multimap. Iteration is done in descending order according to the keys.

Returns a read-only (constant) reverse iterator that points to the last pair in the multimap. Iteration is done in descending order according to the keys.

Returns a read/write reverse iterator that points to one before the first pair in the multimap. Iteration is done in descending order according to the keys.

Returns a read-only (constant) reverse iterator that points to one before the first pair in the multimap. Iteration is done in descending order according to the keys.

Returns true if the multimap is empty.

Returns the size of the multimap.

Returns the maximum size of the multimap.

Inserts a std::pair into the multimap.

Parameters
__xPair to be inserted (see std::make_pair for easy creation of pairs).
Returns
An iterator that points to the inserted (key,value) pair.

This function inserts a (key, value) pair into the multimap. Contrary to a std::map the multimap does not rely on unique keys and thus multiple pairs with the same key can be inserted.

Insertion requires logarithmic time.

Inserts a std::pair into the multimap.

Parameters
__positionAn iterator that serves as a hint as to where the pair should be inserted.
__xPair to be inserted (see std::make_pair for easy creation of pairs).
Returns
An iterator that points to the inserted (key,value) pair.

This function inserts a (key, value) pair into the multimap. Contrary to a std::map the multimap does not rely on unique keys and thus multiple pairs with the same key can be inserted. Note that the first parameter is only a hint and can potentially improve the performance of the insertion process. A bad hint would cause no gains in efficiency.

For more on hinting, see: http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt07ch17.html

Insertion requires logarithmic time (if the hint is not taken).

A template function that attempts to insert a range of elements.

Parameters
__firstIterator pointing to the start of the range to be inserted.
__lastIterator pointing to the end of the range.

Complexity similar to that of the range constructor.

Erases an element from a multimap.

Parameters
__positionAn iterator pointing to the element to be erased.

This function erases an element, pointed to by the given iterator, from a multimap. Note that this function only erases the element, and that if the element is itself a pointer, the pointed-to memory is not touched in any way. Managing the pointer is the user's responsibility.

Erases elements according to the provided key.

Parameters
__xKey of element to be erased.
Returns
The number of elements erased.

This function erases all elements located by the given key from a multimap. Note that this function only erases the element, and that if the element is itself a pointer, the pointed-to memory is not touched in any way. Managing the pointer is the user's responsibility.

Erases a [first,last) range of elements from a multimap.

Parameters
__firstIterator pointing to the start of the range to be erased.
__lastIterator pointing to the end of the range to be erased.

This function erases a sequence of elements from a multimap. Note that this function only erases the elements, and that if the elements themselves are pointers, the pointed-to memory is not touched in any way. Managing the pointer is the user's responsibility.

Swaps data with another multimap.

Parameters
__xA multimap of the same element and allocator types.

This exchanges the elements between two multimaps in constant time. (It is only swapping a pointer, an integer, and an instance of the Compare type (which itself is often stateless and empty), so it should be quite fast.) Note that the global std::swap() function is specialized such that std::swap(m1,m2) will feed to this function.

Erases all elements in a multimap. Note that this function only erases the elements, and that if the elements themselves are pointers, the pointed-to memory is not touched in any way. Managing the pointer is the user's responsibility.

Returns the key comparison object out of which the multimap was constructed.

Returns a value comparison object, built from the key comparison object out of which the multimap was constructed.

Tries to locate an element in a multimap.

Parameters
__xKey of (key, value) pair to be located.
Returns
Iterator pointing to sought-after element, or end() if not found.

This function takes a key and tries to locate the element with which the key matches. If successful the function returns an iterator pointing to the sought after pair. If unsuccessful it returns the past-the-end ( end() ) iterator.

Tries to locate an element in a multimap.

Parameters
__xKey of (key, value) pair to be located.
Returns
Read-only (constant) iterator pointing to sought-after element, or end() if not found.

This function takes a key and tries to locate the element with which the key matches. If successful the function returns a constant iterator pointing to the sought after pair. If unsuccessful it returns the past-the-end ( end() ) iterator.

Finds the number of elements with given key.

Parameters
__xKey of (key, value) pairs to be located.
Returns
Number of elements with specified key.

Finds the beginning of a subsequence matching given key.

Parameters
__xKey of (key, value) pair to be located.
Returns
Iterator pointing to first element equal to or greater than key, or end().

This function returns the first element of a subsequence of elements that matches the given key. If unsuccessful it returns an iterator pointing to the first element that has a greater value than given key or end() if no such element exists.

Finds the beginning of a subsequence matching given key.

Parameters
__xKey of (key, value) pair to be located.
Returns
Read-only (constant) iterator pointing to first element equal to or greater than key, or end().

This function returns the first element of a subsequence of elements that matches the given key. If unsuccessful the iterator will point to the next greatest element or, if no such greater element exists, to end().

Finds the end of a subsequence matching given key.

Parameters
__xKey of (key, value) pair to be located.
Returns
Iterator pointing to the first element greater than key, or end().

Finds the end of a subsequence matching given key.

Parameters
__xKey of (key, value) pair to be located.
Returns
Read-only (constant) iterator pointing to first iterator greater than key, or end().

Finds a subsequence matching given key.

Parameters
__xKey of (key, value) pairs to be located.
Returns
Pair of iterators that possibly points to the subsequence matching given key.

This function is equivalent to

std::make_pair(c.lower_bound(val),
c.upper_bound(val))

(but is faster than making the calls separately).

Finds a subsequence matching given key.

Parameters
__xKey of (key, value) pairs to be located.
Returns
Pair of read-only (constant) iterators that possibly points to the subsequence matching given key.

This function is equivalent to

std::make_pair(c.lower_bound(val),
c.upper_bound(val))

(but is faster than making the calls separately).

Multimap equality comparison.

Parameters
__xA multimap.
__yA multimap of the same type as __x.
Returns
True iff the size and elements of the maps are equal.

This is an equivalence relation. It is linear in the size of the multimaps. Multimaps are considered equivalent if their sizes are equal, and if corresponding elements compare equal.

Multimap ordering relation.

Parameters
__xA multimap.
__yA multimap of the same type as __x.
Returns
True iff x is lexicographically less than y.

This is a total ordering relation. It is linear in the size of the multimaps. The elements must be comparable with <.

See std::lexicographical_compare() for how the determination is made.

Based on operator==

Based on operator<

Based on operator<

Based on operator<

See std::multimap::swap().

65 {
66 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
67 
92  template <typename _Key, typename _Tp,
93  typename _Compare = std::less<_Key>,
94  typename _Alloc = std::allocator<std::pair<const _Key, _Tp> > >
95  class multimap
96  {
97  public:
98  typedef _Key key_type;
99  typedef _Tp mapped_type;
100  typedef std::pair<const _Key, _Tp> value_type;
101  typedef _Compare key_compare;
102  typedef _Alloc allocator_type;
103 
104  private:
105  // concept requirements
106  typedef typename _Alloc::value_type _Alloc_value_type;
107  __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
108  __glibcxx_class_requires4(_Compare, bool, _Key, _Key,
109  _BinaryFunctionConcept)
110  __glibcxx_class_requires2(value_type, _Alloc_value_type, _SameTypeConcept)
111 
112  public:
113  class value_compare
114  : public std::binary_function<value_type, value_type, bool>
115  {
116  friend class multimap<_Key, _Tp, _Compare, _Alloc>;
117  protected:
118  _Compare comp;
119 
120  value_compare(_Compare __c)
121  : comp(__c) { }
122 
123  public:
124  bool operator()(const value_type& __x, const value_type& __y) const
125  { return comp(__x.first, __y.first); }
126  };
127 
128  private:
130  typedef typename _Alloc::template rebind<value_type>::other
131  _Pair_alloc_type;
132 
133  typedef _Rb_tree<key_type, value_type, _Select1st<value_type>,
134  key_compare, _Pair_alloc_type> _Rep_type;
136  _Rep_type _M_t;
137 
138  public:
139  // many of these are specified differently in ISO, but the following are
140  // "functionally equivalent"
141  typedef typename _Pair_alloc_type::pointer pointer;
142  typedef typename _Pair_alloc_type::const_pointer const_pointer;
143  typedef typename _Pair_alloc_type::reference reference;
144  typedef typename _Pair_alloc_type::const_reference const_reference;
145  typedef typename _Rep_type::iterator iterator;
146  typedef typename _Rep_type::const_iterator const_iterator;
147  typedef typename _Rep_type::size_type size_type;
148  typedef typename _Rep_type::difference_type difference_type;
149  typedef typename _Rep_type::reverse_iterator reverse_iterator;
150  typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator;
151 
152  // [23.3.2] construct/copy/destroy
153  // (get_allocator() is also listed in this section)
157  multimap()
158  : _M_t() { }
159 
165  explicit
166  multimap(const _Compare& __comp,
167  const allocator_type& __a = allocator_type())
168  : _M_t(__comp, _Pair_alloc_type(__a)) { }
169 
177  multimap(const multimap& __x)
178  : _M_t(__x._M_t) { }
179 
180 #if __cplusplus >= 201103L
181 
188  multimap(multimap&& __x)
189  noexcept(is_nothrow_copy_constructible<_Compare>::value)
190  : _M_t(std::move(__x._M_t)) { }
191 
202  multimap(initializer_list<value_type> __l,
203  const _Compare& __comp = _Compare(),
204  const allocator_type& __a = allocator_type())
205  : _M_t(__comp, _Pair_alloc_type(__a))
206  { _M_t._M_insert_equal(__l.begin(), __l.end()); }
207 #endif
208 
218  template<typename _InputIterator>
219  multimap(_InputIterator __first, _InputIterator __last)
220  : _M_t()
221  { _M_t._M_insert_equal(__first, __last); }
222 
234  template<typename _InputIterator>
235  multimap(_InputIterator __first, _InputIterator __last,
236  const _Compare& __comp,
237  const allocator_type& __a = allocator_type())
238  : _M_t(__comp, _Pair_alloc_type(__a))
239  { _M_t._M_insert_equal(__first, __last); }
240 
241  // FIXME There is no dtor declared, but we should have something generated
242  // by Doxygen. I don't know what tags to add to this paragraph to make
243  // that happen:
257  multimap&
258  operator=(const multimap& __x)
259  {
260  _M_t = __x._M_t;
261  return *this;
262  }
263 
264 #if __cplusplus >= 201103L
265 
272  multimap&
273  operator=(multimap&& __x)
274  {
275  // NB: DR 1204.
276  // NB: DR 675.
277  this->clear();
278  this->swap(__x);
279  return *this;
280  }
281 
293  multimap&
294  operator=(initializer_list<value_type> __l)
295  {
296  this->clear();
297  this->insert(__l.begin(), __l.end());
298  return *this;
299  }
300 #endif
301 
303  allocator_type
304  get_allocator() const _GLIBCXX_NOEXCEPT
305  { return allocator_type(_M_t.get_allocator()); }
306 
307  // iterators
313  iterator
314  begin() _GLIBCXX_NOEXCEPT
315  { return _M_t.begin(); }
316 
322  const_iterator
323  begin() const _GLIBCXX_NOEXCEPT
324  { return _M_t.begin(); }
325 
331  iterator
332  end() _GLIBCXX_NOEXCEPT
333  { return _M_t.end(); }
334 
340  const_iterator
341  end() const _GLIBCXX_NOEXCEPT
342  { return _M_t.end(); }
343 
349  reverse_iterator
350  rbegin() _GLIBCXX_NOEXCEPT
351  { return _M_t.rbegin(); }
352 
358  const_reverse_iterator
359  rbegin() const _GLIBCXX_NOEXCEPT
360  { return _M_t.rbegin(); }
361 
367  reverse_iterator
368  rend() _GLIBCXX_NOEXCEPT
369  { return _M_t.rend(); }
370 
376  const_reverse_iterator
377  rend() const _GLIBCXX_NOEXCEPT
378  { return _M_t.rend(); }
379 
380 #if __cplusplus >= 201103L
381 
386  const_iterator
387  cbegin() const noexcept
388  { return _M_t.begin(); }
389 
395  const_iterator
396  cend() const noexcept
397  { return _M_t.end(); }
398 
404  const_reverse_iterator
405  crbegin() const noexcept
406  { return _M_t.rbegin(); }
407 
413  const_reverse_iterator
414  crend() const noexcept
415  { return _M_t.rend(); }
416 #endif
417 
418  // capacity
420  bool
421  empty() const _GLIBCXX_NOEXCEPT
422  { return _M_t.empty(); }
423 
425  size_type
426  size() const _GLIBCXX_NOEXCEPT
427  { return _M_t.size(); }
428 
430  size_type
431  max_size() const _GLIBCXX_NOEXCEPT
432  { return _M_t.max_size(); }
433 
434  // modifiers
435 #if __cplusplus >= 201103L
436 
452  template<typename... _Args>
453  iterator
454  emplace(_Args&&... __args)
455  { return _M_t._M_emplace_equal(std::forward<_Args>(__args)...); }
456 
479  template<typename... _Args>
480  iterator
481  emplace_hint(const_iterator __pos, _Args&&... __args)
482  {
483  return _M_t._M_emplace_hint_equal(__pos,
484  std::forward<_Args>(__args)...);
485  }
486 #endif
487 
500  iterator
501  insert(const value_type& __x)
502  { return _M_t._M_insert_equal(__x); }
503 
504 #if __cplusplus >= 201103L
505  template<typename _Pair, typename = typename
506  std::enable_if<std::is_constructible<value_type,
507  _Pair&&>::value>::type>
508  iterator
509  insert(_Pair&& __x)
510  { return _M_t._M_insert_equal(std::forward<_Pair>(__x)); }
511 #endif
512 
533  iterator
534 #if __cplusplus >= 201103L
535  insert(const_iterator __position, const value_type& __x)
536 #else
537  insert(iterator __position, const value_type& __x)
538 #endif
539  { return _M_t._M_insert_equal_(__position, __x); }
540 
541 #if __cplusplus >= 201103L
542  template<typename _Pair, typename = typename
543  std::enable_if<std::is_constructible<value_type,
544  _Pair&&>::value>::type>
545  iterator
546  insert(const_iterator __position, _Pair&& __x)
547  { return _M_t._M_insert_equal_(__position,
548  std::forward<_Pair>(__x)); }
549 #endif
550 
560  template<typename _InputIterator>
561  void
562  insert(_InputIterator __first, _InputIterator __last)
563  { _M_t._M_insert_equal(__first, __last); }
564 
565 #if __cplusplus >= 201103L
566 
573  void
574  insert(initializer_list<value_type> __l)
575  { this->insert(__l.begin(), __l.end()); }
576 #endif
577 
578 #if __cplusplus >= 201103L
579  // _GLIBCXX_RESOLVE_LIB_DEFECTS
580  // DR 130. Associative erase should return an iterator.
594  iterator
595  erase(const_iterator __position)
596  { return _M_t.erase(__position); }
597 
598  // LWG 2059.
599  _GLIBCXX_ABI_TAG_CXX11
600  iterator
601  erase(iterator __position)
602  { return _M_t.erase(__position); }
603 #else
604 
614  void
615  erase(iterator __position)
616  { _M_t.erase(__position); }
617 #endif
618 
630  size_type
631  erase(const key_type& __x)
632  { return _M_t.erase(__x); }
633 
634 #if __cplusplus >= 201103L
635  // _GLIBCXX_RESOLVE_LIB_DEFECTS
636  // DR 130. Associative erase should return an iterator.
651  iterator
652  erase(const_iterator __first, const_iterator __last)
653  { return _M_t.erase(__first, __last); }
654 #else
655  // _GLIBCXX_RESOLVE_LIB_DEFECTS
656  // DR 130. Associative erase should return an iterator.
670  void
671  erase(iterator __first, iterator __last)
672  { _M_t.erase(__first, __last); }
673 #endif
674 
686  void
687  swap(multimap& __x)
688  { _M_t.swap(__x._M_t); }
689 
696  void
697  clear() _GLIBCXX_NOEXCEPT
698  { _M_t.clear(); }
699 
700  // observers
705  key_compare
706  key_comp() const
707  { return _M_t.key_comp(); }
708 
713  value_compare
714  value_comp() const
715  { return value_compare(_M_t.key_comp()); }
716 
717  // multimap operations
729  iterator
730  find(const key_type& __x)
731  { return _M_t.find(__x); }
732 
744  const_iterator
745  find(const key_type& __x) const
746  { return _M_t.find(__x); }
747 
753  size_type
754  count(const key_type& __x) const
755  { return _M_t.count(__x); }
756 
768  iterator
769  lower_bound(const key_type& __x)
770  { return _M_t.lower_bound(__x); }
771 
783  const_iterator
784  lower_bound(const key_type& __x) const
785  { return _M_t.lower_bound(__x); }
786 
793  iterator
794  upper_bound(const key_type& __x)
795  { return _M_t.upper_bound(__x); }
796 
803  const_iterator
804  upper_bound(const key_type& __x) const
805  { return _M_t.upper_bound(__x); }
806 
820  std::pair<iterator, iterator>
821  equal_range(const key_type& __x)
822  { return _M_t.equal_range(__x); }
823 
837  std::pair<const_iterator, const_iterator>
838  equal_range(const key_type& __x) const
839  { return _M_t.equal_range(__x); }
840 
841  template<typename _K1, typename _T1, typename _C1, typename _A1>
842  friend bool
843  operator==(const multimap<_K1, _T1, _C1, _A1>&,
844  const multimap<_K1, _T1, _C1, _A1>&);
845 
846  template<typename _K1, typename _T1, typename _C1, typename _A1>
847  friend bool
848  operator<(const multimap<_K1, _T1, _C1, _A1>&,
849  const multimap<_K1, _T1, _C1, _A1>&);
850  };
851 
862  template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
863  inline bool
864  operator==(const multimap<_Key, _Tp, _Compare, _Alloc>& __x,
865  const multimap<_Key, _Tp, _Compare, _Alloc>& __y)
866  { return __x._M_t == __y._M_t; }
867 
879  template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
880  inline bool
881  operator<(const multimap<_Key, _Tp, _Compare, _Alloc>& __x,
882  const multimap<_Key, _Tp, _Compare, _Alloc>& __y)
883  { return __x._M_t < __y._M_t; }
884 
886  template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
887  inline bool
888  operator!=(const multimap<_Key, _Tp, _Compare, _Alloc>& __x,
889  const multimap<_Key, _Tp, _Compare, _Alloc>& __y)
890  { return !(__x == __y); }
891 
893  template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
894  inline bool
895  operator>(const multimap<_Key, _Tp, _Compare, _Alloc>& __x,
896  const multimap<_Key, _Tp, _Compare, _Alloc>& __y)
897  { return __y < __x; }
898 
900  template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
901  inline bool
902  operator<=(const multimap<_Key, _Tp, _Compare, _Alloc>& __x,
903  const multimap<_Key, _Tp, _Compare, _Alloc>& __y)
904  { return !(__y < __x); }
905 
907  template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
908  inline bool
909  operator>=(const multimap<_Key, _Tp, _Compare, _Alloc>& __x,
910  const multimap<_Key, _Tp, _Compare, _Alloc>& __y)
911  { return !(__x < __y); }
912 
914  template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
915  inline void
916  swap(multimap<_Key, _Tp, _Compare, _Alloc>& __x,
917  multimap<_Key, _Tp, _Compare, _Alloc>& __y)
918  { __x.swap(__y); }
919 
920 _GLIBCXX_END_NAMESPACE_CONTAINER
921 } // namespace std
bool operator>=(const _Safe_iterator< _IteratorL, _Sequence > &__lhs, const _Safe_iterator< _IteratorR, _Sequence > &__rhs)
Definition: safe_iterator.h:644
bool operator==(const exception_ptr &, const exception_ptr &) _GLIBCXX_USE_NOEXCEPT __attribute__((__pure__))
#define __glibcxx_class_requires(_a, _b)
Definition: concept_check.h:48
bool operator>(const _Safe_iterator< _IteratorL, _Sequence > &__lhs, const _Safe_iterator< _IteratorR, _Sequence > &__rhs)
Definition: safe_iterator.h:612
#define __glibcxx_class_requires2(_a, _b, _c)
Definition: concept_check.h:49
bool operator!=(const exception_ptr &, const exception_ptr &) _GLIBCXX_USE_NOEXCEPT __attribute__((__pure__))
void swap(exception_ptr &__lhs, exception_ptr &__rhs)
Definition: exception_ptr.h:160
#define __glibcxx_class_requires4(_a, _b, _c, _d, _e)
Definition: concept_check.h:51