libstdc++
stl_multimap.h
Go to the documentation of this file.
1 // Multimap implementation -*- C++ -*-
2 
3 // Copyright (C) 2001-2014 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10 
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19 
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24 
25 /*
26  *
27  * Copyright (c) 1994
28  * Hewlett-Packard Company
29  *
30  * Permission to use, copy, modify, distribute and sell this software
31  * and its documentation for any purpose is hereby granted without fee,
32  * provided that the above copyright notice appear in all copies and
33  * that both that copyright notice and this permission notice appear
34  * in supporting documentation. Hewlett-Packard Company makes no
35  * representations about the suitability of this software for any
36  * purpose. It is provided "as is" without express or implied warranty.
37  *
38  *
39  * Copyright (c) 1996,1997
40  * Silicon Graphics Computer Systems, Inc.
41  *
42  * Permission to use, copy, modify, distribute and sell this software
43  * and its documentation for any purpose is hereby granted without fee,
44  * provided that the above copyright notice appear in all copies and
45  * that both that copyright notice and this permission notice appear
46  * in supporting documentation. Silicon Graphics makes no
47  * representations about the suitability of this software for any
48  * purpose. It is provided "as is" without express or implied warranty.
49  */
50 
51 /** @file bits/stl_multimap.h
52  * This is an internal header file, included by other library headers.
53  * Do not attempt to use it directly. @headername{map}
54  */
55 
56 #ifndef _STL_MULTIMAP_H
57 #define _STL_MULTIMAP_H 1
58 
59 #include <bits/concept_check.h>
60 #if __cplusplus >= 201103L
61 #include <initializer_list>
62 #endif
63 
64 namespace std _GLIBCXX_VISIBILITY(default)
65 {
66 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
67 
68  /**
69  * @brief A standard container made up of (key,value) pairs, which can be
70  * retrieved based on a key, in logarithmic time.
71  *
72  * @ingroup associative_containers
73  *
74  * @tparam _Key Type of key objects.
75  * @tparam _Tp Type of mapped objects.
76  * @tparam _Compare Comparison function object type, defaults to less<_Key>.
77  * @tparam _Alloc Allocator type, defaults to
78  * allocator<pair<const _Key, _Tp>.
79  *
80  * Meets the requirements of a <a href="tables.html#65">container</a>, a
81  * <a href="tables.html#66">reversible container</a>, and an
82  * <a href="tables.html#69">associative container</a> (using equivalent
83  * keys). For a @c multimap<Key,T> the key_type is Key, the mapped_type
84  * is T, and the value_type is std::pair<const Key,T>.
85  *
86  * Multimaps support bidirectional iterators.
87  *
88  * The private tree data is declared exactly the same way for map and
89  * multimap; the distinction is made entirely in how the tree functions are
90  * called (*_unique versus *_equal, same as the standard).
91  */
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;
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:
129  /// This turns a red-black tree into a [multi]map.
131  rebind<value_type>::other _Pair_alloc_type;
132 
133  typedef _Rb_tree<key_type, value_type, _Select1st<value_type>,
134  key_compare, _Pair_alloc_type> _Rep_type;
135  /// The actual tree structure.
136  _Rep_type _M_t;
137 
139 
140  public:
141  // many of these are specified differently in ISO, but the following are
142  // "functionally equivalent"
143  typedef typename _Alloc_traits::pointer pointer;
144  typedef typename _Alloc_traits::const_pointer const_pointer;
145  typedef typename _Alloc_traits::reference reference;
146  typedef typename _Alloc_traits::const_reference const_reference;
147  typedef typename _Rep_type::iterator iterator;
148  typedef typename _Rep_type::const_iterator const_iterator;
149  typedef typename _Rep_type::size_type size_type;
150  typedef typename _Rep_type::difference_type difference_type;
153 
154  // [23.3.2] construct/copy/destroy
155  // (get_allocator() is also listed in this section)
156  /**
157  * @brief Default constructor creates no elements.
158  */
160  : _M_t() { }
161 
162  /**
163  * @brief Creates a %multimap with no elements.
164  * @param __comp A comparison object.
165  * @param __a An allocator object.
166  */
167  explicit
168  multimap(const _Compare& __comp,
169  const allocator_type& __a = allocator_type())
170  : _M_t(__comp, _Pair_alloc_type(__a)) { }
171 
172  /**
173  * @brief %Multimap copy constructor.
174  * @param __x A %multimap of identical element and allocator types.
175  *
176  * The newly-created %multimap uses a copy of the allocation object
177  * used by @a __x.
178  */
179  multimap(const multimap& __x)
180  : _M_t(__x._M_t) { }
181 
182 #if __cplusplus >= 201103L
183  /**
184  * @brief %Multimap move constructor.
185  * @param __x A %multimap of identical element and allocator types.
186  *
187  * The newly-created %multimap contains the exact contents of @a __x.
188  * The contents of @a __x are a valid, but unspecified %multimap.
189  */
190  multimap(multimap&& __x)
191  noexcept(is_nothrow_copy_constructible<_Compare>::value)
192  : _M_t(std::move(__x._M_t)) { }
193 
194  /**
195  * @brief Builds a %multimap from an initializer_list.
196  * @param __l An initializer_list.
197  * @param __comp A comparison functor.
198  * @param __a An allocator object.
199  *
200  * Create a %multimap consisting of copies of the elements from
201  * the initializer_list. This is linear in N if the list is already
202  * sorted, and NlogN otherwise (where N is @a __l.size()).
203  */
205  const _Compare& __comp = _Compare(),
206  const allocator_type& __a = allocator_type())
207  : _M_t(__comp, _Pair_alloc_type(__a))
208  { _M_t._M_insert_equal(__l.begin(), __l.end()); }
209 
210  /// Allocator-extended default constructor.
211  explicit
212  multimap(const allocator_type& __a)
213  : _M_t(_Compare(), _Pair_alloc_type(__a)) { }
214 
215  /// Allocator-extended copy constructor.
216  multimap(const multimap& __m, const allocator_type& __a)
217  : _M_t(__m._M_t, _Pair_alloc_type(__a)) { }
218 
219  /// Allocator-extended move constructor.
220  multimap(multimap&& __m, const allocator_type& __a)
221  noexcept(is_nothrow_copy_constructible<_Compare>::value
222  && _Alloc_traits::_S_always_equal())
223  : _M_t(std::move(__m._M_t), _Pair_alloc_type(__a)) { }
224 
225  /// Allocator-extended initialier-list constructor.
226  multimap(initializer_list<value_type> __l, const allocator_type& __a)
227  : _M_t(_Compare(), _Pair_alloc_type(__a))
228  { _M_t._M_insert_equal(__l.begin(), __l.end()); }
229 
230  /// Allocator-extended range constructor.
231  template<typename _InputIterator>
232  multimap(_InputIterator __first, _InputIterator __last,
233  const allocator_type& __a)
234  : _M_t(_Compare(), _Pair_alloc_type(__a))
235  { _M_t._M_insert_equal(__first, __last); }
236 #endif
237 
238  /**
239  * @brief Builds a %multimap from a range.
240  * @param __first An input iterator.
241  * @param __last An input iterator.
242  *
243  * Create a %multimap consisting of copies of the elements from
244  * [__first,__last). This is linear in N if the range is already sorted,
245  * and NlogN otherwise (where N is distance(__first,__last)).
246  */
247  template<typename _InputIterator>
248  multimap(_InputIterator __first, _InputIterator __last)
249  : _M_t()
250  { _M_t._M_insert_equal(__first, __last); }
251 
252  /**
253  * @brief Builds a %multimap from a range.
254  * @param __first An input iterator.
255  * @param __last An input iterator.
256  * @param __comp A comparison functor.
257  * @param __a An allocator object.
258  *
259  * Create a %multimap consisting of copies of the elements from
260  * [__first,__last). This is linear in N if the range is already sorted,
261  * and NlogN otherwise (where N is distance(__first,__last)).
262  */
263  template<typename _InputIterator>
264  multimap(_InputIterator __first, _InputIterator __last,
265  const _Compare& __comp,
266  const allocator_type& __a = allocator_type())
267  : _M_t(__comp, _Pair_alloc_type(__a))
268  { _M_t._M_insert_equal(__first, __last); }
269 
270  // FIXME There is no dtor declared, but we should have something generated
271  // by Doxygen. I don't know what tags to add to this paragraph to make
272  // that happen:
273  /**
274  * The dtor only erases the elements, and note that if the elements
275  * themselves are pointers, the pointed-to memory is not touched in any
276  * way. Managing the pointer is the user's responsibility.
277  */
278 
279  /**
280  * @brief %Multimap assignment operator.
281  * @param __x A %multimap of identical element and allocator types.
282  *
283  * All the elements of @a __x are copied, but unlike the copy
284  * constructor, the allocator object is not copied.
285  */
286  multimap&
287  operator=(const multimap& __x)
288  {
289  _M_t = __x._M_t;
290  return *this;
291  }
292 
293 #if __cplusplus >= 201103L
294  /**
295  * @brief %Multimap move assignment operator.
296  * @param __x A %multimap of identical element and allocator types.
297  *
298  * The contents of @a __x are moved into this multimap (without copying).
299  * @a __x is a valid, but unspecified multimap.
300  */
301  multimap&
302  operator=(multimap&& __x) noexcept(_Alloc_traits::_S_nothrow_move())
303  {
304  if (!_M_t._M_move_assign(__x._M_t))
305  {
306  // The rvalue's allocator cannot be moved and is not equal,
307  // so we need to individually move each element.
308  clear();
309  insert(std::__make_move_if_noexcept_iterator(__x.begin()),
310  std::__make_move_if_noexcept_iterator(__x.end()));
311  __x.clear();
312  }
313  return *this;
314  }
315 
316  /**
317  * @brief %Multimap list assignment operator.
318  * @param __l An initializer_list.
319  *
320  * This function fills a %multimap with copies of the elements
321  * in the initializer list @a __l.
322  *
323  * Note that the assignment completely changes the %multimap and
324  * that the resulting %multimap's size is the same as the number
325  * of elements assigned. Old data may be lost.
326  */
327  multimap&
329  {
330  this->clear();
331  this->insert(__l.begin(), __l.end());
332  return *this;
333  }
334 #endif
335 
336  /// Get a copy of the memory allocation object.
337  allocator_type
338  get_allocator() const _GLIBCXX_NOEXCEPT
339  { return allocator_type(_M_t.get_allocator()); }
340 
341  // iterators
342  /**
343  * Returns a read/write iterator that points to the first pair in the
344  * %multimap. Iteration is done in ascending order according to the
345  * keys.
346  */
347  iterator
348  begin() _GLIBCXX_NOEXCEPT
349  { return _M_t.begin(); }
350 
351  /**
352  * Returns a read-only (constant) iterator that points to the first pair
353  * in the %multimap. Iteration is done in ascending order according to
354  * the keys.
355  */
356  const_iterator
357  begin() const _GLIBCXX_NOEXCEPT
358  { return _M_t.begin(); }
359 
360  /**
361  * Returns a read/write iterator that points one past the last pair in
362  * the %multimap. Iteration is done in ascending order according to the
363  * keys.
364  */
365  iterator
366  end() _GLIBCXX_NOEXCEPT
367  { return _M_t.end(); }
368 
369  /**
370  * Returns a read-only (constant) iterator that points one past the last
371  * pair in the %multimap. Iteration is done in ascending order according
372  * to the keys.
373  */
374  const_iterator
375  end() const _GLIBCXX_NOEXCEPT
376  { return _M_t.end(); }
377 
378  /**
379  * Returns a read/write reverse iterator that points to the last pair in
380  * the %multimap. Iteration is done in descending order according to the
381  * keys.
382  */
384  rbegin() _GLIBCXX_NOEXCEPT
385  { return _M_t.rbegin(); }
386 
387  /**
388  * Returns a read-only (constant) reverse iterator that points to the
389  * last pair in the %multimap. Iteration is done in descending order
390  * according to the keys.
391  */
392  const_reverse_iterator
393  rbegin() const _GLIBCXX_NOEXCEPT
394  { return _M_t.rbegin(); }
395 
396  /**
397  * Returns a read/write reverse iterator that points to one before the
398  * first pair in the %multimap. Iteration is done in descending order
399  * according to the keys.
400  */
402  rend() _GLIBCXX_NOEXCEPT
403  { return _M_t.rend(); }
404 
405  /**
406  * Returns a read-only (constant) reverse iterator that points to one
407  * before the first pair in the %multimap. Iteration is done in
408  * descending order according to the keys.
409  */
410  const_reverse_iterator
411  rend() const _GLIBCXX_NOEXCEPT
412  { return _M_t.rend(); }
413 
414 #if __cplusplus >= 201103L
415  /**
416  * Returns a read-only (constant) iterator that points to the first pair
417  * in the %multimap. Iteration is done in ascending order according to
418  * the keys.
419  */
420  const_iterator
421  cbegin() const noexcept
422  { return _M_t.begin(); }
423 
424  /**
425  * Returns a read-only (constant) iterator that points one past the last
426  * pair in the %multimap. Iteration is done in ascending order according
427  * to the keys.
428  */
429  const_iterator
430  cend() const noexcept
431  { return _M_t.end(); }
432 
433  /**
434  * Returns a read-only (constant) reverse iterator that points to the
435  * last pair in the %multimap. Iteration is done in descending order
436  * according to the keys.
437  */
438  const_reverse_iterator
440  { return _M_t.rbegin(); }
441 
442  /**
443  * Returns a read-only (constant) reverse iterator that points to one
444  * before the first pair in the %multimap. Iteration is done in
445  * descending order according to the keys.
446  */
447  const_reverse_iterator
448  crend() const noexcept
449  { return _M_t.rend(); }
450 #endif
451 
452  // capacity
453  /** Returns true if the %multimap is empty. */
454  bool
455  empty() const _GLIBCXX_NOEXCEPT
456  { return _M_t.empty(); }
457 
458  /** Returns the size of the %multimap. */
459  size_type
460  size() const _GLIBCXX_NOEXCEPT
461  { return _M_t.size(); }
462 
463  /** Returns the maximum size of the %multimap. */
464  size_type
465  max_size() const _GLIBCXX_NOEXCEPT
466  { return _M_t.max_size(); }
467 
468  // modifiers
469 #if __cplusplus >= 201103L
470  /**
471  * @brief Build and insert a std::pair into the %multimap.
472  *
473  * @param __args Arguments used to generate a new pair instance (see
474  * std::piecewise_contruct for passing arguments to each
475  * part of the pair constructor).
476  *
477  * @return An iterator that points to the inserted (key,value) pair.
478  *
479  * This function builds and inserts a (key, value) %pair into the
480  * %multimap.
481  * Contrary to a std::map the %multimap does not rely on unique keys and
482  * thus multiple pairs with the same key can be inserted.
483  *
484  * Insertion requires logarithmic time.
485  */
486  template<typename... _Args>
487  iterator
488  emplace(_Args&&... __args)
489  { return _M_t._M_emplace_equal(std::forward<_Args>(__args)...); }
490 
491  /**
492  * @brief Builds and inserts a std::pair into the %multimap.
493  *
494  * @param __pos An iterator that serves as a hint as to where the pair
495  * should be inserted.
496  * @param __args Arguments used to generate a new pair instance (see
497  * std::piecewise_contruct for passing arguments to each
498  * part of the pair constructor).
499  * @return An iterator that points to the inserted (key,value) pair.
500  *
501  * This function inserts a (key, value) pair into the %multimap.
502  * Contrary to a std::map the %multimap does not rely on unique keys and
503  * thus multiple pairs with the same key can be inserted.
504  * Note that the first parameter is only a hint and can potentially
505  * improve the performance of the insertion process. A bad hint would
506  * cause no gains in efficiency.
507  *
508  * For more on @a hinting, see:
509  * http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt07ch17.html
510  *
511  * Insertion requires logarithmic time (if the hint is not taken).
512  */
513  template<typename... _Args>
514  iterator
515  emplace_hint(const_iterator __pos, _Args&&... __args)
516  {
517  return _M_t._M_emplace_hint_equal(__pos,
518  std::forward<_Args>(__args)...);
519  }
520 #endif
521 
522  /**
523  * @brief Inserts a std::pair into the %multimap.
524  * @param __x Pair to be inserted (see std::make_pair for easy creation
525  * of pairs).
526  * @return An iterator that points to the inserted (key,value) pair.
527  *
528  * This function inserts a (key, value) pair into the %multimap.
529  * Contrary to a std::map the %multimap does not rely on unique keys and
530  * thus multiple pairs with the same key can be inserted.
531  *
532  * Insertion requires logarithmic time.
533  */
534  iterator
535  insert(const value_type& __x)
536  { return _M_t._M_insert_equal(__x); }
537 
538 #if __cplusplus >= 201103L
539  template<typename _Pair, typename = typename
540  std::enable_if<std::is_constructible<value_type,
541  _Pair&&>::value>::type>
542  iterator
543  insert(_Pair&& __x)
544  { return _M_t._M_insert_equal(std::forward<_Pair>(__x)); }
545 #endif
546 
547  /**
548  * @brief Inserts a std::pair into the %multimap.
549  * @param __position An iterator that serves as a hint as to where the
550  * pair should be inserted.
551  * @param __x Pair to be inserted (see std::make_pair for easy creation
552  * of pairs).
553  * @return An iterator that points to the inserted (key,value) pair.
554  *
555  * This function inserts a (key, value) pair into the %multimap.
556  * Contrary to a std::map the %multimap does not rely on unique keys and
557  * thus multiple pairs with the same key can be inserted.
558  * Note that the first parameter is only a hint and can potentially
559  * improve the performance of the insertion process. A bad hint would
560  * cause no gains in efficiency.
561  *
562  * For more on @a hinting, see:
563  * http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt07ch17.html
564  *
565  * Insertion requires logarithmic time (if the hint is not taken).
566  */
567  iterator
568 #if __cplusplus >= 201103L
569  insert(const_iterator __position, const value_type& __x)
570 #else
571  insert(iterator __position, const value_type& __x)
572 #endif
573  { return _M_t._M_insert_equal_(__position, __x); }
574 
575 #if __cplusplus >= 201103L
576  template<typename _Pair, typename = typename
577  std::enable_if<std::is_constructible<value_type,
578  _Pair&&>::value>::type>
579  iterator
580  insert(const_iterator __position, _Pair&& __x)
581  { return _M_t._M_insert_equal_(__position,
582  std::forward<_Pair>(__x)); }
583 #endif
584 
585  /**
586  * @brief A template function that attempts to insert a range
587  * of elements.
588  * @param __first Iterator pointing to the start of the range to be
589  * inserted.
590  * @param __last Iterator pointing to the end of the range.
591  *
592  * Complexity similar to that of the range constructor.
593  */
594  template<typename _InputIterator>
595  void
596  insert(_InputIterator __first, _InputIterator __last)
597  { _M_t._M_insert_equal(__first, __last); }
598 
599 #if __cplusplus >= 201103L
600  /**
601  * @brief Attempts to insert a list of std::pairs into the %multimap.
602  * @param __l A std::initializer_list<value_type> of pairs to be
603  * inserted.
604  *
605  * Complexity similar to that of the range constructor.
606  */
607  void
609  { this->insert(__l.begin(), __l.end()); }
610 #endif
611 
612 #if __cplusplus >= 201103L
613  // _GLIBCXX_RESOLVE_LIB_DEFECTS
614  // DR 130. Associative erase should return an iterator.
615  /**
616  * @brief Erases an element from a %multimap.
617  * @param __position An iterator pointing to the element to be erased.
618  * @return An iterator pointing to the element immediately following
619  * @a position prior to the element being erased. If no such
620  * element exists, end() is returned.
621  *
622  * This function erases an element, pointed to by the given iterator,
623  * from a %multimap. Note that this function only erases the element,
624  * and that if the element is itself a pointer, the pointed-to memory is
625  * not touched in any way. Managing the pointer is the user's
626  * responsibility.
627  */
628  iterator
629  erase(const_iterator __position)
630  { return _M_t.erase(__position); }
631 
632  // LWG 2059.
633  _GLIBCXX_ABI_TAG_CXX11
634  iterator
635  erase(iterator __position)
636  { return _M_t.erase(__position); }
637 #else
638  /**
639  * @brief Erases an element from a %multimap.
640  * @param __position An iterator pointing to the element to be erased.
641  *
642  * This function erases an element, pointed to by the given iterator,
643  * from a %multimap. Note that this function only erases the element,
644  * and that if the element is itself a pointer, the pointed-to memory is
645  * not touched in any way. Managing the pointer is the user's
646  * responsibility.
647  */
648  void
649  erase(iterator __position)
650  { _M_t.erase(__position); }
651 #endif
652 
653  /**
654  * @brief Erases elements according to the provided key.
655  * @param __x Key of element to be erased.
656  * @return The number of elements erased.
657  *
658  * This function erases all elements located by the given key from a
659  * %multimap.
660  * Note that this function only erases the element, and that if
661  * the element is itself a pointer, the pointed-to memory is not touched
662  * in any way. Managing the pointer is the user's responsibility.
663  */
664  size_type
665  erase(const key_type& __x)
666  { return _M_t.erase(__x); }
667 
668 #if __cplusplus >= 201103L
669  // _GLIBCXX_RESOLVE_LIB_DEFECTS
670  // DR 130. Associative erase should return an iterator.
671  /**
672  * @brief Erases a [first,last) range of elements from a %multimap.
673  * @param __first Iterator pointing to the start of the range to be
674  * erased.
675  * @param __last Iterator pointing to the end of the range to be
676  * erased .
677  * @return The iterator @a __last.
678  *
679  * This function erases a sequence of elements from a %multimap.
680  * Note that this function only erases the elements, and that if
681  * the elements themselves are pointers, the pointed-to memory is not
682  * touched in any way. Managing the pointer is the user's
683  * responsibility.
684  */
685  iterator
686  erase(const_iterator __first, const_iterator __last)
687  { return _M_t.erase(__first, __last); }
688 #else
689  // _GLIBCXX_RESOLVE_LIB_DEFECTS
690  // DR 130. Associative erase should return an iterator.
691  /**
692  * @brief Erases a [first,last) range of elements from a %multimap.
693  * @param __first Iterator pointing to the start of the range to be
694  * erased.
695  * @param __last Iterator pointing to the end of the range to
696  * be erased.
697  *
698  * This function erases a sequence of elements from a %multimap.
699  * Note that this function only erases the elements, and that if
700  * the elements themselves are pointers, the pointed-to memory is not
701  * touched in any way. Managing the pointer is the user's
702  * responsibility.
703  */
704  void
705  erase(iterator __first, iterator __last)
706  { _M_t.erase(__first, __last); }
707 #endif
708 
709  /**
710  * @brief Swaps data with another %multimap.
711  * @param __x A %multimap of the same element and allocator types.
712  *
713  * This exchanges the elements between two multimaps in constant time.
714  * (It is only swapping a pointer, an integer, and an instance of
715  * the @c Compare type (which itself is often stateless and empty), so it
716  * should be quite fast.)
717  * Note that the global std::swap() function is specialized such that
718  * std::swap(m1,m2) will feed to this function.
719  */
720  void
721  swap(multimap& __x)
722 #if __cplusplus >= 201103L
723  noexcept(_Alloc_traits::_S_nothrow_swap())
724 #endif
725  { _M_t.swap(__x._M_t); }
726 
727  /**
728  * Erases all elements in a %multimap. Note that this function only
729  * erases the elements, and that if the elements themselves are pointers,
730  * the pointed-to memory is not touched in any way. Managing the pointer
731  * is the user's responsibility.
732  */
733  void
734  clear() _GLIBCXX_NOEXCEPT
735  { _M_t.clear(); }
736 
737  // observers
738  /**
739  * Returns the key comparison object out of which the %multimap
740  * was constructed.
741  */
742  key_compare
743  key_comp() const
744  { return _M_t.key_comp(); }
745 
746  /**
747  * Returns a value comparison object, built from the key comparison
748  * object out of which the %multimap was constructed.
749  */
750  value_compare
751  value_comp() const
752  { return value_compare(_M_t.key_comp()); }
753 
754  // multimap operations
755  /**
756  * @brief Tries to locate an element in a %multimap.
757  * @param __x Key of (key, value) pair to be located.
758  * @return Iterator pointing to sought-after element,
759  * or end() if not found.
760  *
761  * This function takes a key and tries to locate the element with which
762  * the key matches. If successful the function returns an iterator
763  * pointing to the sought after %pair. If unsuccessful it returns the
764  * past-the-end ( @c end() ) iterator.
765  */
766  iterator
767  find(const key_type& __x)
768  { return _M_t.find(__x); }
769 
770  /**
771  * @brief Tries to locate an element in a %multimap.
772  * @param __x Key of (key, value) pair to be located.
773  * @return Read-only (constant) iterator pointing to sought-after
774  * element, or end() if not found.
775  *
776  * This function takes a key and tries to locate the element with which
777  * the key matches. If successful the function returns a constant
778  * iterator pointing to the sought after %pair. If unsuccessful it
779  * returns the past-the-end ( @c end() ) iterator.
780  */
781  const_iterator
782  find(const key_type& __x) const
783  { return _M_t.find(__x); }
784 
785  /**
786  * @brief Finds the number of elements with given key.
787  * @param __x Key of (key, value) pairs to be located.
788  * @return Number of elements with specified key.
789  */
790  size_type
791  count(const key_type& __x) const
792  { return _M_t.count(__x); }
793 
794  /**
795  * @brief Finds the beginning of a subsequence matching given key.
796  * @param __x Key of (key, value) pair to be located.
797  * @return Iterator pointing to first element equal to or greater
798  * than key, or end().
799  *
800  * This function returns the first element of a subsequence of elements
801  * that matches the given key. If unsuccessful it returns an iterator
802  * pointing to the first element that has a greater value than given key
803  * or end() if no such element exists.
804  */
805  iterator
806  lower_bound(const key_type& __x)
807  { return _M_t.lower_bound(__x); }
808 
809  /**
810  * @brief Finds the beginning of a subsequence matching given key.
811  * @param __x Key of (key, value) pair to be located.
812  * @return Read-only (constant) iterator pointing to first element
813  * equal to or greater than key, or end().
814  *
815  * This function returns the first element of a subsequence of
816  * elements that matches the given key. If unsuccessful the
817  * iterator will point to the next greatest element or, if no
818  * such greater element exists, to end().
819  */
820  const_iterator
821  lower_bound(const key_type& __x) const
822  { return _M_t.lower_bound(__x); }
823 
824  /**
825  * @brief Finds the end of a subsequence matching given key.
826  * @param __x Key of (key, value) pair to be located.
827  * @return Iterator pointing to the first element
828  * greater than key, or end().
829  */
830  iterator
831  upper_bound(const key_type& __x)
832  { return _M_t.upper_bound(__x); }
833 
834  /**
835  * @brief Finds the end of a subsequence matching given key.
836  * @param __x Key of (key, value) pair to be located.
837  * @return Read-only (constant) iterator pointing to first iterator
838  * greater than key, or end().
839  */
840  const_iterator
841  upper_bound(const key_type& __x) const
842  { return _M_t.upper_bound(__x); }
843 
844  /**
845  * @brief Finds a subsequence matching given key.
846  * @param __x Key of (key, value) pairs to be located.
847  * @return Pair of iterators that possibly points to the subsequence
848  * matching given key.
849  *
850  * This function is equivalent to
851  * @code
852  * std::make_pair(c.lower_bound(val),
853  * c.upper_bound(val))
854  * @endcode
855  * (but is faster than making the calls separately).
856  */
858  equal_range(const key_type& __x)
859  { return _M_t.equal_range(__x); }
860 
861  /**
862  * @brief Finds a subsequence matching given key.
863  * @param __x Key of (key, value) pairs to be located.
864  * @return Pair of read-only (constant) iterators that possibly points
865  * to the subsequence matching given key.
866  *
867  * This function is equivalent to
868  * @code
869  * std::make_pair(c.lower_bound(val),
870  * c.upper_bound(val))
871  * @endcode
872  * (but is faster than making the calls separately).
873  */
875  equal_range(const key_type& __x) const
876  { return _M_t.equal_range(__x); }
877 
878  template<typename _K1, typename _T1, typename _C1, typename _A1>
879  friend bool
880  operator==(const multimap<_K1, _T1, _C1, _A1>&,
882 
883  template<typename _K1, typename _T1, typename _C1, typename _A1>
884  friend bool
885  operator<(const multimap<_K1, _T1, _C1, _A1>&,
887  };
888 
889  /**
890  * @brief Multimap equality comparison.
891  * @param __x A %multimap.
892  * @param __y A %multimap of the same type as @a __x.
893  * @return True iff the size and elements of the maps are equal.
894  *
895  * This is an equivalence relation. It is linear in the size of the
896  * multimaps. Multimaps are considered equivalent if their sizes are equal,
897  * and if corresponding elements compare equal.
898  */
899  template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
900  inline bool
903  { return __x._M_t == __y._M_t; }
904 
905  /**
906  * @brief Multimap ordering relation.
907  * @param __x A %multimap.
908  * @param __y A %multimap of the same type as @a __x.
909  * @return True iff @a x is lexicographically less than @a y.
910  *
911  * This is a total ordering relation. It is linear in the size of the
912  * multimaps. The elements must be comparable with @c <.
913  *
914  * See std::lexicographical_compare() for how the determination is made.
915  */
916  template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
917  inline bool
918  operator<(const multimap<_Key, _Tp, _Compare, _Alloc>& __x,
920  { return __x._M_t < __y._M_t; }
921 
922  /// Based on operator==
923  template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
924  inline bool
927  { return !(__x == __y); }
928 
929  /// Based on operator<
930  template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
931  inline bool
934  { return __y < __x; }
935 
936  /// Based on operator<
937  template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
938  inline bool
939  operator<=(const multimap<_Key, _Tp, _Compare, _Alloc>& __x,
941  { return !(__y < __x); }
942 
943  /// Based on operator<
944  template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
945  inline bool
948  { return !(__x < __y); }
949 
950  /// See std::multimap::swap().
951  template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
952  inline void
955  { __x.swap(__y); }
956 
957 _GLIBCXX_END_NAMESPACE_CONTAINER
958 } // namespace std
959 
960 #endif /* _STL_MULTIMAP_H */
std::pair< iterator, iterator > equal_range(const key_type &__x)
Finds a subsequence matching given key.
Definition: stl_multimap.h:858
const_iterator cend() const noexcept
Definition: stl_multimap.h:430
multimap(const multimap &__m, const allocator_type &__a)
Allocator-extended copy constructor.
Definition: stl_multimap.h:216
noexcept(is_nothrow_copy_constructible< _Compare >::value)
Multimap move constructor.
Definition: stl_multimap.h:191
iterator erase(const_iterator __first, const_iterator __last)
Erases a [first,last) range of elements from a multimap.
Definition: stl_multimap.h:686
iterator erase(const_iterator __position)
Erases an element from a multimap.
Definition: stl_multimap.h:629
multimap & operator=(const multimap &__x)
Multimap assignment operator.
Definition: stl_multimap.h:287
Uniform interface to C++98 and C++0x allocators.
size_type size() const noexcept
Definition: stl_multimap.h:460
bool empty() const noexcept
Definition: stl_multimap.h:455
multimap(const allocator_type &__a)
Allocator-extended default constructor.
Definition: stl_multimap.h:212
value_compare value_comp() const
Definition: stl_multimap.h:751
_T1 first
second_type is the second bound type
Definition: stl_pair.h:101
void clear() noexcept
Definition: stl_multimap.h:734
multimap(_InputIterator __first, _InputIterator __last)
Builds a multimap from a range.
Definition: stl_multimap.h:248
Struct holding two objects of arbitrary type.
Definition: stl_pair.h:96
multimap(const _Compare &__comp, const allocator_type &__a=allocator_type())
Creates a multimap with no elements.
Definition: stl_multimap.h:168
allocator_type get_allocator() const noexcept
Get a copy of the memory allocation object.
Definition: stl_multimap.h:338
multimap(initializer_list< value_type > __l, const allocator_type &__a)
Allocator-extended initialier-list constructor.
Definition: stl_multimap.h:226
A standard container made up of (key,value) pairs, which can be retrieved based on a key...
Definition: stl_multimap.h:95
iterator end() noexcept
Definition: stl_multimap.h:366
void insert(initializer_list< value_type > __l)
Attempts to insert a list of std::pairs into the multimap.
Definition: stl_multimap.h:608
multimap(_InputIterator __first, _InputIterator __last, const _Compare &__comp, const allocator_type &__a=allocator_type())
Builds a multimap from a range.
Definition: stl_multimap.h:264
iterator begin() noexcept
Definition: stl_multimap.h:348
multimap(const multimap &__x)
Multimap copy constructor.
Definition: stl_multimap.h:179
multimap()
Default constructor creates no elements.
Definition: stl_multimap.h:159
iterator emplace_hint(const_iterator __pos, _Args &&...__args)
Builds and inserts a std::pair into the multimap.
Definition: stl_multimap.h:515
multimap(initializer_list< value_type > __l, const _Compare &__comp=_Compare(), const allocator_type &__a=allocator_type())
Builds a multimap from an initializer_list.
Definition: stl_multimap.h:204
const_iterator find(const key_type &__x) const
Tries to locate an element in a multimap.
Definition: stl_multimap.h:782
size_type max_size() const noexcept
Definition: stl_multimap.h:465
One of the comparison functors.
Definition: stl_function.h:363
const_iterator cbegin() const noexcept
Definition: stl_multimap.h:421
key_compare key_comp() const
Definition: stl_multimap.h:743
multimap(_InputIterator __first, _InputIterator __last, const allocator_type &__a)
Allocator-extended range constructor.
Definition: stl_multimap.h:232
iterator emplace(_Args &&...__args)
Build and insert a std::pair into the multimap.
Definition: stl_multimap.h:488
void insert(_InputIterator __first, _InputIterator __last)
A template function that attempts to insert a range of elements.
Definition: stl_multimap.h:596
const_iterator end() const noexcept
Definition: stl_multimap.h:375
const_reverse_iterator rbegin() const noexcept
Definition: stl_multimap.h:393
const_reverse_iterator rend() const noexcept
Definition: stl_multimap.h:411
iterator find(const key_type &__x)
Tries to locate an element in a multimap.
Definition: stl_multimap.h:767
reverse_iterator rend() noexcept
Definition: stl_multimap.h:402
size_type count(const key_type &__x) const
Finds the number of elements with given key.
Definition: stl_multimap.h:791
iterator lower_bound(const key_type &__x)
Finds the beginning of a subsequence matching given key.
Definition: stl_multimap.h:806
iterator insert(const value_type &__x)
Inserts a std::pair into the multimap.
Definition: stl_multimap.h:535
The standard allocator, as per [20.4].
Definition: allocator.h:92
void swap(function< _Res(_Args...)> &__x, function< _Res(_Args...)> &__y)
Swap the targets of two polymorphic function object wrappers.
Definition: functional:2534
Common iterator class.
initializer_list
reverse_iterator rbegin() noexcept
Definition: stl_multimap.h:384
const_iterator lower_bound(const key_type &__x) const
Finds the beginning of a subsequence matching given key.
Definition: stl_multimap.h:821
const_iterator begin() const noexcept
Definition: stl_multimap.h:357
std::pair< const_iterator, const_iterator > equal_range(const key_type &__x) const
Finds a subsequence matching given key.
Definition: stl_multimap.h:875
multimap & operator=(initializer_list< value_type > __l)
Multimap list assignment operator.
Definition: stl_multimap.h:328
size_type erase(const key_type &__x)
Erases elements according to the provided key.
Definition: stl_multimap.h:665
const_iterator upper_bound(const key_type &__x) const
Finds the end of a subsequence matching given key.
Definition: stl_multimap.h:841
const_reverse_iterator crend() const noexcept
Definition: stl_multimap.h:448
const_reverse_iterator crbegin() const noexcept
Definition: stl_multimap.h:439
iterator upper_bound(const key_type &__x)
Finds the end of a subsequence matching given key.
Definition: stl_multimap.h:831
multimap & operator=(multimap &&__x) noexcept(_Alloc_traits::_S_nothrow_move())
Multimap move assignment operator.
Definition: stl_multimap.h:302