libstdc++
stl_multimap.h
Go to the documentation of this file.
1 // Multimap implementation -*- C++ -*-
2 
3 // Copyright (C) 2001-2015 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  /**
158  * @brief Default constructor creates no elements.
159  */
161 #if __cplusplus >= 201103L
162  noexcept(is_nothrow_default_constructible<allocator_type>::value
163  && is_nothrow_default_constructible<key_compare>::value)
164 #endif
165  : _M_t() { }
166 
167  /**
168  * @brief Creates a %multimap with no elements.
169  * @param __comp A comparison object.
170  * @param __a An allocator object.
171  */
172  explicit
173  multimap(const _Compare& __comp,
174  const allocator_type& __a = allocator_type())
175  : _M_t(__comp, _Pair_alloc_type(__a)) { }
176 
177  /**
178  * @brief %Multimap copy constructor.
179  * @param __x A %multimap of identical element and allocator types.
180  *
181  * The newly-created %multimap uses a copy of the allocation object
182  * used by @a __x.
183  */
184  multimap(const multimap& __x)
185  : _M_t(__x._M_t) { }
186 
187 #if __cplusplus >= 201103L
188  /**
189  * @brief %Multimap move constructor.
190  * @param __x A %multimap of identical element and allocator types.
191  *
192  * The newly-created %multimap contains the exact contents of @a __x.
193  * The contents of @a __x are a valid, but unspecified %multimap.
194  */
196  noexcept(is_nothrow_copy_constructible<_Compare>::value)
197  : _M_t(std::move(__x._M_t)) { }
198 
199  /**
200  * @brief Builds a %multimap from an initializer_list.
201  * @param __l An initializer_list.
202  * @param __comp A comparison functor.
203  * @param __a An allocator object.
204  *
205  * Create a %multimap consisting of copies of the elements from
206  * the initializer_list. This is linear in N if the list is already
207  * sorted, and NlogN otherwise (where N is @a __l.size()).
208  */
210  const _Compare& __comp = _Compare(),
211  const allocator_type& __a = allocator_type())
212  : _M_t(__comp, _Pair_alloc_type(__a))
213  { _M_t._M_insert_equal(__l.begin(), __l.end()); }
214 
215  /// Allocator-extended default constructor.
216  explicit
217  multimap(const allocator_type& __a)
218  : _M_t(_Compare(), _Pair_alloc_type(__a)) { }
219 
220  /// Allocator-extended copy constructor.
221  multimap(const multimap& __m, const allocator_type& __a)
222  : _M_t(__m._M_t, _Pair_alloc_type(__a)) { }
223 
224  /// Allocator-extended move constructor.
225  multimap(multimap&& __m, const allocator_type& __a)
226  noexcept(is_nothrow_copy_constructible<_Compare>::value
227  && _Alloc_traits::_S_always_equal())
228  : _M_t(std::move(__m._M_t), _Pair_alloc_type(__a)) { }
229 
230  /// Allocator-extended initialier-list constructor.
231  multimap(initializer_list<value_type> __l, const allocator_type& __a)
232  : _M_t(_Compare(), _Pair_alloc_type(__a))
233  { _M_t._M_insert_equal(__l.begin(), __l.end()); }
234 
235  /// Allocator-extended range constructor.
236  template<typename _InputIterator>
237  multimap(_InputIterator __first, _InputIterator __last,
238  const allocator_type& __a)
239  : _M_t(_Compare(), _Pair_alloc_type(__a))
240  { _M_t._M_insert_equal(__first, __last); }
241 #endif
242 
243  /**
244  * @brief Builds a %multimap from a range.
245  * @param __first An input iterator.
246  * @param __last An input iterator.
247  *
248  * Create a %multimap consisting of copies of the elements from
249  * [__first,__last). This is linear in N if the range is already sorted,
250  * and NlogN otherwise (where N is distance(__first,__last)).
251  */
252  template<typename _InputIterator>
253  multimap(_InputIterator __first, _InputIterator __last)
254  : _M_t()
255  { _M_t._M_insert_equal(__first, __last); }
256 
257  /**
258  * @brief Builds a %multimap from a range.
259  * @param __first An input iterator.
260  * @param __last An input iterator.
261  * @param __comp A comparison functor.
262  * @param __a An allocator object.
263  *
264  * Create a %multimap consisting of copies of the elements from
265  * [__first,__last). This is linear in N if the range is already sorted,
266  * and NlogN otherwise (where N is distance(__first,__last)).
267  */
268  template<typename _InputIterator>
269  multimap(_InputIterator __first, _InputIterator __last,
270  const _Compare& __comp,
271  const allocator_type& __a = allocator_type())
272  : _M_t(__comp, _Pair_alloc_type(__a))
273  { _M_t._M_insert_equal(__first, __last); }
274 
275  // FIXME There is no dtor declared, but we should have something generated
276  // by Doxygen. I don't know what tags to add to this paragraph to make
277  // that happen:
278  /**
279  * The dtor only erases the elements, and note that if the elements
280  * themselves are pointers, the pointed-to memory is not touched in any
281  * way. Managing the pointer is the user's responsibility.
282  */
283 
284  /**
285  * @brief %Multimap assignment operator.
286  * @param __x A %multimap of identical element and allocator types.
287  *
288  * All the elements of @a __x are copied, but unlike the copy
289  * constructor, the allocator object is not copied.
290  */
291  multimap&
292  operator=(const multimap& __x)
293  {
294  _M_t = __x._M_t;
295  return *this;
296  }
297 
298 #if __cplusplus >= 201103L
299  /// Move assignment operator.
300  multimap&
301  operator=(multimap&&) = default;
302 
303  /**
304  * @brief %Multimap list assignment operator.
305  * @param __l An initializer_list.
306  *
307  * This function fills a %multimap with copies of the elements
308  * in the initializer list @a __l.
309  *
310  * Note that the assignment completely changes the %multimap and
311  * that the resulting %multimap's size is the same as the number
312  * of elements assigned. Old data may be lost.
313  */
314  multimap&
316  {
317  _M_t._M_assign_equal(__l.begin(), __l.end());
318  return *this;
319  }
320 #endif
321 
322  /// Get a copy of the memory allocation object.
323  allocator_type
324  get_allocator() const _GLIBCXX_NOEXCEPT
325  { return allocator_type(_M_t.get_allocator()); }
326 
327  // iterators
328  /**
329  * Returns a read/write iterator that points to the first pair in the
330  * %multimap. Iteration is done in ascending order according to the
331  * keys.
332  */
333  iterator
334  begin() _GLIBCXX_NOEXCEPT
335  { return _M_t.begin(); }
336 
337  /**
338  * Returns a read-only (constant) iterator that points to the first pair
339  * in the %multimap. Iteration is done in ascending order according to
340  * the keys.
341  */
342  const_iterator
343  begin() const _GLIBCXX_NOEXCEPT
344  { return _M_t.begin(); }
345 
346  /**
347  * Returns a read/write iterator that points one past the last pair in
348  * the %multimap. Iteration is done in ascending order according to the
349  * keys.
350  */
351  iterator
352  end() _GLIBCXX_NOEXCEPT
353  { return _M_t.end(); }
354 
355  /**
356  * Returns a read-only (constant) iterator that points one past the last
357  * pair in the %multimap. Iteration is done in ascending order according
358  * to the keys.
359  */
360  const_iterator
361  end() const _GLIBCXX_NOEXCEPT
362  { return _M_t.end(); }
363 
364  /**
365  * Returns a read/write reverse iterator that points to the last pair in
366  * the %multimap. Iteration is done in descending order according to the
367  * keys.
368  */
369  reverse_iterator
370  rbegin() _GLIBCXX_NOEXCEPT
371  { return _M_t.rbegin(); }
372 
373  /**
374  * Returns a read-only (constant) reverse iterator that points to the
375  * last pair in the %multimap. Iteration is done in descending order
376  * according to the keys.
377  */
378  const_reverse_iterator
379  rbegin() const _GLIBCXX_NOEXCEPT
380  { return _M_t.rbegin(); }
381 
382  /**
383  * Returns a read/write reverse iterator that points to one before the
384  * first pair in the %multimap. Iteration is done in descending order
385  * according to the keys.
386  */
387  reverse_iterator
388  rend() _GLIBCXX_NOEXCEPT
389  { return _M_t.rend(); }
390 
391  /**
392  * Returns a read-only (constant) reverse iterator that points to one
393  * before the first pair in the %multimap. Iteration is done in
394  * descending order according to the keys.
395  */
396  const_reverse_iterator
397  rend() const _GLIBCXX_NOEXCEPT
398  { return _M_t.rend(); }
399 
400 #if __cplusplus >= 201103L
401  /**
402  * Returns a read-only (constant) iterator that points to the first pair
403  * in the %multimap. Iteration is done in ascending order according to
404  * the keys.
405  */
406  const_iterator
407  cbegin() const noexcept
408  { return _M_t.begin(); }
409 
410  /**
411  * Returns a read-only (constant) iterator that points one past the last
412  * pair in the %multimap. Iteration is done in ascending order according
413  * to the keys.
414  */
415  const_iterator
416  cend() const noexcept
417  { return _M_t.end(); }
418 
419  /**
420  * Returns a read-only (constant) reverse iterator that points to the
421  * last pair in the %multimap. Iteration is done in descending order
422  * according to the keys.
423  */
424  const_reverse_iterator
425  crbegin() const noexcept
426  { return _M_t.rbegin(); }
427 
428  /**
429  * Returns a read-only (constant) reverse iterator that points to one
430  * before the first pair in the %multimap. Iteration is done in
431  * descending order according to the keys.
432  */
433  const_reverse_iterator
434  crend() const noexcept
435  { return _M_t.rend(); }
436 #endif
437 
438  // capacity
439  /** Returns true if the %multimap is empty. */
440  bool
441  empty() const _GLIBCXX_NOEXCEPT
442  { return _M_t.empty(); }
443 
444  /** Returns the size of the %multimap. */
445  size_type
446  size() const _GLIBCXX_NOEXCEPT
447  { return _M_t.size(); }
448 
449  /** Returns the maximum size of the %multimap. */
450  size_type
451  max_size() const _GLIBCXX_NOEXCEPT
452  { return _M_t.max_size(); }
453 
454  // modifiers
455 #if __cplusplus >= 201103L
456  /**
457  * @brief Build and insert a std::pair into the %multimap.
458  *
459  * @param __args Arguments used to generate a new pair instance (see
460  * std::piecewise_contruct for passing arguments to each
461  * part of the pair constructor).
462  *
463  * @return An iterator that points to the inserted (key,value) pair.
464  *
465  * This function builds and inserts a (key, value) %pair into the
466  * %multimap.
467  * Contrary to a std::map the %multimap does not rely on unique keys and
468  * thus multiple pairs with the same key can be inserted.
469  *
470  * Insertion requires logarithmic time.
471  */
472  template<typename... _Args>
473  iterator
474  emplace(_Args&&... __args)
475  { return _M_t._M_emplace_equal(std::forward<_Args>(__args)...); }
476 
477  /**
478  * @brief Builds and inserts a std::pair into the %multimap.
479  *
480  * @param __pos An iterator that serves as a hint as to where the pair
481  * should be inserted.
482  * @param __args Arguments used to generate a new pair instance (see
483  * std::piecewise_contruct for passing arguments to each
484  * part of the pair constructor).
485  * @return An iterator that points to the inserted (key,value) pair.
486  *
487  * This function inserts a (key, value) pair into the %multimap.
488  * Contrary to a std::map the %multimap does not rely on unique keys and
489  * thus multiple pairs with the same key can be inserted.
490  * Note that the first parameter is only a hint and can potentially
491  * improve the performance of the insertion process. A bad hint would
492  * cause no gains in efficiency.
493  *
494  * For more on @a hinting, see:
495  * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
496  *
497  * Insertion requires logarithmic time (if the hint is not taken).
498  */
499  template<typename... _Args>
500  iterator
501  emplace_hint(const_iterator __pos, _Args&&... __args)
502  {
503  return _M_t._M_emplace_hint_equal(__pos,
504  std::forward<_Args>(__args)...);
505  }
506 #endif
507 
508  /**
509  * @brief Inserts a std::pair into the %multimap.
510  * @param __x Pair to be inserted (see std::make_pair for easy creation
511  * of pairs).
512  * @return An iterator that points to the inserted (key,value) pair.
513  *
514  * This function inserts a (key, value) pair into the %multimap.
515  * Contrary to a std::map the %multimap does not rely on unique keys and
516  * thus multiple pairs with the same key can be inserted.
517  *
518  * Insertion requires logarithmic time.
519  */
520  iterator
521  insert(const value_type& __x)
522  { return _M_t._M_insert_equal(__x); }
523 
524 #if __cplusplus >= 201103L
525  template<typename _Pair, typename = typename
526  std::enable_if<std::is_constructible<value_type,
527  _Pair&&>::value>::type>
528  iterator
529  insert(_Pair&& __x)
530  { return _M_t._M_insert_equal(std::forward<_Pair>(__x)); }
531 #endif
532 
533  /**
534  * @brief Inserts a std::pair into the %multimap.
535  * @param __position An iterator that serves as a hint as to where the
536  * pair should be inserted.
537  * @param __x Pair to be inserted (see std::make_pair for easy creation
538  * of pairs).
539  * @return An iterator that points to the inserted (key,value) pair.
540  *
541  * This function inserts a (key, value) pair into the %multimap.
542  * Contrary to a std::map the %multimap does not rely on unique keys and
543  * thus multiple pairs with the same key can be inserted.
544  * Note that the first parameter is only a hint and can potentially
545  * improve the performance of the insertion process. A bad hint would
546  * cause no gains in efficiency.
547  *
548  * For more on @a hinting, see:
549  * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
550  *
551  * Insertion requires logarithmic time (if the hint is not taken).
552  */
553  iterator
554 #if __cplusplus >= 201103L
555  insert(const_iterator __position, const value_type& __x)
556 #else
557  insert(iterator __position, const value_type& __x)
558 #endif
559  { return _M_t._M_insert_equal_(__position, __x); }
560 
561 #if __cplusplus >= 201103L
562  template<typename _Pair, typename = typename
563  std::enable_if<std::is_constructible<value_type,
564  _Pair&&>::value>::type>
565  iterator
566  insert(const_iterator __position, _Pair&& __x)
567  { return _M_t._M_insert_equal_(__position,
568  std::forward<_Pair>(__x)); }
569 #endif
570 
571  /**
572  * @brief A template function that attempts to insert a range
573  * of elements.
574  * @param __first Iterator pointing to the start of the range to be
575  * inserted.
576  * @param __last Iterator pointing to the end of the range.
577  *
578  * Complexity similar to that of the range constructor.
579  */
580  template<typename _InputIterator>
581  void
582  insert(_InputIterator __first, _InputIterator __last)
583  { _M_t._M_insert_equal(__first, __last); }
584 
585 #if __cplusplus >= 201103L
586  /**
587  * @brief Attempts to insert a list of std::pairs into the %multimap.
588  * @param __l A std::initializer_list<value_type> of pairs to be
589  * inserted.
590  *
591  * Complexity similar to that of the range constructor.
592  */
593  void
595  { this->insert(__l.begin(), __l.end()); }
596 #endif
597 
598 #if __cplusplus >= 201103L
599  // _GLIBCXX_RESOLVE_LIB_DEFECTS
600  // DR 130. Associative erase should return an iterator.
601  /**
602  * @brief Erases an element from a %multimap.
603  * @param __position An iterator pointing to the element to be erased.
604  * @return An iterator pointing to the element immediately following
605  * @a position prior to the element being erased. If no such
606  * element exists, end() is returned.
607  *
608  * This function erases an element, pointed to by the given iterator,
609  * from a %multimap. Note that this function only erases the element,
610  * and that if the element is itself a pointer, the pointed-to memory is
611  * not touched in any way. Managing the pointer is the user's
612  * responsibility.
613  */
614  iterator
615  erase(const_iterator __position)
616  { return _M_t.erase(__position); }
617 
618  // LWG 2059.
619  _GLIBCXX_ABI_TAG_CXX11
620  iterator
621  erase(iterator __position)
622  { return _M_t.erase(__position); }
623 #else
624  /**
625  * @brief Erases an element from a %multimap.
626  * @param __position An iterator pointing to the element to be erased.
627  *
628  * This function erases an element, pointed to by the given iterator,
629  * from a %multimap. Note that this function only erases the element,
630  * and that if the element is itself a pointer, the pointed-to memory is
631  * not touched in any way. Managing the pointer is the user's
632  * responsibility.
633  */
634  void
635  erase(iterator __position)
636  { _M_t.erase(__position); }
637 #endif
638 
639  /**
640  * @brief Erases elements according to the provided key.
641  * @param __x Key of element to be erased.
642  * @return The number of elements erased.
643  *
644  * This function erases all elements located by the given key from a
645  * %multimap.
646  * Note that this function only erases the element, and that if
647  * the element is itself a pointer, the pointed-to memory is not touched
648  * in any way. Managing the pointer is the user's responsibility.
649  */
650  size_type
651  erase(const key_type& __x)
652  { return _M_t.erase(__x); }
653 
654 #if __cplusplus >= 201103L
655  // _GLIBCXX_RESOLVE_LIB_DEFECTS
656  // DR 130. Associative erase should return an iterator.
657  /**
658  * @brief Erases a [first,last) range of elements from a %multimap.
659  * @param __first Iterator pointing to the start of the range to be
660  * erased.
661  * @param __last Iterator pointing to the end of the range to be
662  * erased .
663  * @return The iterator @a __last.
664  *
665  * This function erases a sequence of elements from a %multimap.
666  * Note that this function only erases the elements, and that if
667  * the elements themselves are pointers, the pointed-to memory is not
668  * touched in any way. Managing the pointer is the user's
669  * responsibility.
670  */
671  iterator
672  erase(const_iterator __first, const_iterator __last)
673  { return _M_t.erase(__first, __last); }
674 #else
675  // _GLIBCXX_RESOLVE_LIB_DEFECTS
676  // DR 130. Associative erase should return an iterator.
677  /**
678  * @brief Erases a [first,last) range of elements from a %multimap.
679  * @param __first Iterator pointing to the start of the range to be
680  * erased.
681  * @param __last Iterator pointing to the end of the range to
682  * be erased.
683  *
684  * This function erases a sequence of elements from a %multimap.
685  * Note that this function only erases the elements, and that if
686  * the elements themselves are pointers, the pointed-to memory is not
687  * touched in any way. Managing the pointer is the user's
688  * responsibility.
689  */
690  void
691  erase(iterator __first, iterator __last)
692  { _M_t.erase(__first, __last); }
693 #endif
694 
695  /**
696  * @brief Swaps data with another %multimap.
697  * @param __x A %multimap of the same element and allocator types.
698  *
699  * This exchanges the elements between two multimaps in constant time.
700  * (It is only swapping a pointer, an integer, and an instance of
701  * the @c Compare type (which itself is often stateless and empty), so it
702  * should be quite fast.)
703  * Note that the global std::swap() function is specialized such that
704  * std::swap(m1,m2) will feed to this function.
705  */
706  void
708 #if __cplusplus >= 201103L
709  noexcept(_Alloc_traits::_S_nothrow_swap())
710 #endif
711  { _M_t.swap(__x._M_t); }
712 
713  /**
714  * Erases all elements in a %multimap. Note that this function only
715  * erases the elements, and that if the elements themselves are pointers,
716  * the pointed-to memory is not touched in any way. Managing the pointer
717  * is the user's responsibility.
718  */
719  void
720  clear() _GLIBCXX_NOEXCEPT
721  { _M_t.clear(); }
722 
723  // observers
724  /**
725  * Returns the key comparison object out of which the %multimap
726  * was constructed.
727  */
728  key_compare
729  key_comp() const
730  { return _M_t.key_comp(); }
731 
732  /**
733  * Returns a value comparison object, built from the key comparison
734  * object out of which the %multimap was constructed.
735  */
736  value_compare
737  value_comp() const
738  { return value_compare(_M_t.key_comp()); }
739 
740  // multimap operations
741 
742  //@{
743  /**
744  * @brief Tries to locate an element in a %multimap.
745  * @param __x Key of (key, value) pair to be located.
746  * @return Iterator pointing to sought-after element,
747  * or end() if not found.
748  *
749  * This function takes a key and tries to locate the element with which
750  * the key matches. If successful the function returns an iterator
751  * pointing to the sought after %pair. If unsuccessful it returns the
752  * past-the-end ( @c end() ) iterator.
753  */
754  iterator
755  find(const key_type& __x)
756  { return _M_t.find(__x); }
757 
758 #if __cplusplus > 201103L
759  template<typename _Kt>
760  auto
761  find(const _Kt& __x) -> decltype(_M_t._M_find_tr(__x))
762  { return _M_t._M_find_tr(__x); }
763 #endif
764  //@}
765 
766  //@{
767  /**
768  * @brief Tries to locate an element in a %multimap.
769  * @param __x Key of (key, value) pair to be located.
770  * @return Read-only (constant) iterator pointing to sought-after
771  * element, or end() if not found.
772  *
773  * This function takes a key and tries to locate the element with which
774  * the key matches. If successful the function returns a constant
775  * iterator pointing to the sought after %pair. If unsuccessful it
776  * returns the past-the-end ( @c end() ) iterator.
777  */
778  const_iterator
779  find(const key_type& __x) const
780  { return _M_t.find(__x); }
781 
782 #if __cplusplus > 201103L
783  template<typename _Kt>
784  auto
785  find(const _Kt& __x) const -> decltype(_M_t._M_find_tr(__x))
786  { return _M_t._M_find_tr(__x); }
787 #endif
788  //@}
789 
790  //@{
791  /**
792  * @brief Finds the number of elements with given key.
793  * @param __x Key of (key, value) pairs to be located.
794  * @return Number of elements with specified key.
795  */
796  size_type
797  count(const key_type& __x) const
798  { return _M_t.count(__x); }
799 
800 #if __cplusplus > 201103L
801  template<typename _Kt>
802  auto
803  count(const _Kt& __x) const -> decltype(_M_t._M_count_tr(__x))
804  { return _M_t._M_count_tr(__x); }
805 #endif
806  //@}
807 
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 Iterator pointing to first element equal to or greater
813  * than key, or end().
814  *
815  * This function returns the first element of a subsequence of elements
816  * that matches the given key. If unsuccessful it returns an iterator
817  * pointing to the first element that has a greater value than given key
818  * or end() if no such element exists.
819  */
820  iterator
821  lower_bound(const key_type& __x)
822  { return _M_t.lower_bound(__x); }
823 
824 #if __cplusplus > 201103L
825  template<typename _Kt>
826  auto
827  lower_bound(const _Kt& __x)
828  -> decltype(iterator(_M_t._M_lower_bound_tr(__x)))
829  { return iterator(_M_t._M_lower_bound_tr(__x)); }
830 #endif
831  //@}
832 
833  //@{
834  /**
835  * @brief Finds the beginning 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 element
838  * equal to or greater than key, or end().
839  *
840  * This function returns the first element of a subsequence of
841  * elements that matches the given key. If unsuccessful the
842  * iterator will point to the next greatest element or, if no
843  * such greater element exists, to end().
844  */
845  const_iterator
846  lower_bound(const key_type& __x) const
847  { return _M_t.lower_bound(__x); }
848 
849 #if __cplusplus > 201103L
850  template<typename _Kt>
851  auto
852  lower_bound(const _Kt& __x) const
853  -> decltype(const_iterator(_M_t._M_lower_bound_tr(__x)))
854  { return const_iterator(_M_t._M_lower_bound_tr(__x)); }
855 #endif
856  //@}
857 
858  //@{
859  /**
860  * @brief Finds the end of a subsequence matching given key.
861  * @param __x Key of (key, value) pair to be located.
862  * @return Iterator pointing to the first element
863  * greater than key, or end().
864  */
865  iterator
866  upper_bound(const key_type& __x)
867  { return _M_t.upper_bound(__x); }
868 
869 #if __cplusplus > 201103L
870  template<typename _Kt>
871  auto
872  upper_bound(const _Kt& __x)
873  -> decltype(iterator(_M_t._M_upper_bound_tr(__x)))
874  { return iterator(_M_t._M_upper_bound_tr(__x)); }
875 #endif
876  //@}
877 
878  //@{
879  /**
880  * @brief Finds the end of a subsequence matching given key.
881  * @param __x Key of (key, value) pair to be located.
882  * @return Read-only (constant) iterator pointing to first iterator
883  * greater than key, or end().
884  */
885  const_iterator
886  upper_bound(const key_type& __x) const
887  { return _M_t.upper_bound(__x); }
888 
889 #if __cplusplus > 201103L
890  template<typename _Kt>
891  auto
892  upper_bound(const _Kt& __x) const
893  -> decltype(const_iterator(_M_t._M_upper_bound_tr(__x)))
894  { return const_iterator(_M_t._M_upper_bound_tr(__x)); }
895 #endif
896  //@}
897 
898  //@{
899  /**
900  * @brief Finds a subsequence matching given key.
901  * @param __x Key of (key, value) pairs to be located.
902  * @return Pair of iterators that possibly points to the subsequence
903  * matching given key.
904  *
905  * This function is equivalent to
906  * @code
907  * std::make_pair(c.lower_bound(val),
908  * c.upper_bound(val))
909  * @endcode
910  * (but is faster than making the calls separately).
911  */
913  equal_range(const key_type& __x)
914  { return _M_t.equal_range(__x); }
915 
916 #if __cplusplus > 201103L
917  template<typename _Kt>
918  auto
919  equal_range(const _Kt& __x)
920  -> decltype(pair<iterator, iterator>(_M_t._M_equal_range_tr(__x)))
921  { return pair<iterator, iterator>(_M_t._M_equal_range_tr(__x)); }
922 #endif
923  //@}
924 
925  //@{
926  /**
927  * @brief Finds a subsequence matching given key.
928  * @param __x Key of (key, value) pairs to be located.
929  * @return Pair of read-only (constant) iterators that possibly points
930  * to the subsequence matching given key.
931  *
932  * This function is equivalent to
933  * @code
934  * std::make_pair(c.lower_bound(val),
935  * c.upper_bound(val))
936  * @endcode
937  * (but is faster than making the calls separately).
938  */
940  equal_range(const key_type& __x) const
941  { return _M_t.equal_range(__x); }
942 
943 #if __cplusplus > 201103L
944  template<typename _Kt>
945  auto
946  equal_range(const _Kt& __x) const
948  _M_t._M_equal_range_tr(__x)))
949  {
951  _M_t._M_equal_range_tr(__x));
952  }
953 #endif
954  //@}
955 
956  template<typename _K1, typename _T1, typename _C1, typename _A1>
957  friend bool
960 
961  template<typename _K1, typename _T1, typename _C1, typename _A1>
962  friend bool
963  operator<(const multimap<_K1, _T1, _C1, _A1>&,
965  };
966 
967  /**
968  * @brief Multimap equality comparison.
969  * @param __x A %multimap.
970  * @param __y A %multimap of the same type as @a __x.
971  * @return True iff the size and elements of the maps are equal.
972  *
973  * This is an equivalence relation. It is linear in the size of the
974  * multimaps. Multimaps are considered equivalent if their sizes are equal,
975  * and if corresponding elements compare equal.
976  */
977  template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
978  inline bool
981  { return __x._M_t == __y._M_t; }
982 
983  /**
984  * @brief Multimap ordering relation.
985  * @param __x A %multimap.
986  * @param __y A %multimap of the same type as @a __x.
987  * @return True iff @a x is lexicographically less than @a y.
988  *
989  * This is a total ordering relation. It is linear in the size of the
990  * multimaps. The elements must be comparable with @c <.
991  *
992  * See std::lexicographical_compare() for how the determination is made.
993  */
994  template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
995  inline bool
996  operator<(const multimap<_Key, _Tp, _Compare, _Alloc>& __x,
998  { return __x._M_t < __y._M_t; }
999 
1000  /// Based on operator==
1001  template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
1002  inline bool
1005  { return !(__x == __y); }
1006 
1007  /// Based on operator<
1008  template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
1009  inline bool
1012  { return __y < __x; }
1013 
1014  /// Based on operator<
1015  template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
1016  inline bool
1017  operator<=(const multimap<_Key, _Tp, _Compare, _Alloc>& __x,
1019  { return !(__y < __x); }
1020 
1021  /// Based on operator<
1022  template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
1023  inline bool
1026  { return !(__x < __y); }
1027 
1028  /// See std::multimap::swap().
1029  template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
1030  inline void
1033  { __x.swap(__y); }
1034 
1035 _GLIBCXX_END_NAMESPACE_CONTAINER
1036 } // namespace std
1037 
1038 #endif /* _STL_MULTIMAP_H */
auto lower_bound(const _Kt &__x) -> decltype(iterator(_M_t._M_lower_bound_tr(__x)))
Finds the beginning of a subsequence matching given key.
Definition: stl_multimap.h:827
bool operator==(const multimap< _Key, _Tp, _Compare, _Alloc > &__x, const multimap< _Key, _Tp, _Compare, _Alloc > &__y)
Multimap equality comparison.
Definition: stl_multimap.h:979
auto count(const _Kt &__x) const -> decltype(_M_t._M_count_tr(__x))
Finds the number of elements with given key.
Definition: stl_multimap.h:803
const_reverse_iterator crbegin() const noexcept
Definition: stl_multimap.h:425
const_iterator begin() const noexcept
Definition: stl_multimap.h:343
key_compare key_comp() const
Definition: stl_multimap.h:729
reverse_iterator rbegin() noexcept
Definition: stl_multimap.h:370
size_type count(const key_type &__x) const
Finds the number of elements with given key.
Definition: stl_multimap.h:797
iterator upper_bound(const key_type &__x)
Finds the end of a subsequence matching given key.
Definition: stl_multimap.h:866
multimap(const multimap &__m, const allocator_type &__a)
Allocator-extended copy constructor.
Definition: stl_multimap.h:221
iterator insert(const value_type &__x)
Inserts a std::pair into the multimap.
Definition: stl_multimap.h:521
void insert(_InputIterator __first, _InputIterator __last)
A template function that attempts to insert a range of elements.
Definition: stl_multimap.h:582
auto lower_bound(const _Kt &__x) const -> decltype(const_iterator(_M_t._M_lower_bound_tr(__x)))
Finds the beginning of a subsequence matching given key.
Definition: stl_multimap.h:852
iterator erase(const_iterator __first, const_iterator __last)
Erases a [first,last) range of elements from a multimap.
Definition: stl_multimap.h:672
multimap(const allocator_type &__a)
Allocator-extended default constructor.
Definition: stl_multimap.h:217
std::pair< const_iterator, const_iterator > equal_range(const key_type &__x) const
Finds a subsequence matching given key.
Definition: stl_multimap.h:940
void clear() noexcept
Definition: stl_multimap.h:720
initializer_list
std::pair< iterator, iterator > equal_range(const key_type &__x)
Finds a subsequence matching given key.
Definition: stl_multimap.h:913
multimap & operator=(initializer_list< value_type > __l)
Multimap list assignment operator.
Definition: stl_multimap.h:315
reverse_iterator rend() noexcept
Definition: stl_multimap.h:388
multimap(_InputIterator __first, _InputIterator __last, const allocator_type &__a)
Allocator-extended range constructor.
Definition: stl_multimap.h:237
multimap(initializer_list< value_type > __l, const allocator_type &__a)
Allocator-extended initialier-list constructor.
Definition: stl_multimap.h:231
allocator_type get_allocator() const noexcept
Get a copy of the memory allocation object.
Definition: stl_multimap.h:324
One of the comparison functors.
Definition: stl_function.h:341
size_type erase(const key_type &__x)
Erases elements according to the provided key.
Definition: stl_multimap.h:651
iterator emplace(_Args &&... __args)
Build and insert a std::pair into the multimap.
Definition: stl_multimap.h:474
iterator lower_bound(const key_type &__x)
Finds the beginning of a subsequence matching given key.
Definition: stl_multimap.h:821
void insert(initializer_list< value_type > __l)
Attempts to insert a list of std::pairs into the multimap.
Definition: stl_multimap.h:594
multimap(_InputIterator __first, _InputIterator __last)
Builds a multimap from a range.
Definition: stl_multimap.h:253
value_compare value_comp() const
Definition: stl_multimap.h:737
const_iterator find(const key_type &__x) const
Tries to locate an element in a multimap.
Definition: stl_multimap.h:779
Uniform interface to C++98 and C++0x allocators.
ISO C++ entities toplevel namespace is std.
const_iterator cbegin() const noexcept
Definition: stl_multimap.h:407
iterator end() noexcept
Definition: stl_multimap.h:352
iterator emplace_hint(const_iterator __pos, _Args &&... __args)
Builds and inserts a std::pair into the multimap.
Definition: stl_multimap.h:501
iterator insert(const_iterator __position, const value_type &__x)
Inserts a std::pair into the multimap.
Definition: stl_multimap.h:555
size_type max_size() const noexcept
Definition: stl_multimap.h:451
bool operator!=(const multimap< _Key, _Tp, _Compare, _Alloc > &__x, const multimap< _Key, _Tp, _Compare, _Alloc > &__y)
Based on operator==.
multimap & operator=(const multimap &__x)
Multimap assignment operator.
Definition: stl_multimap.h:292
auto upper_bound(const _Kt &__x) -> decltype(iterator(_M_t._M_upper_bound_tr(__x)))
Finds the end of a subsequence matching given key.
Definition: stl_multimap.h:872
multimap(multimap &&__x) noexcept(is_nothrow_copy_constructible< _Compare >::value)
Multimap move constructor.
Definition: stl_multimap.h:195
Struct holding two objects of arbitrary type.
Definition: stl_pair.h:96
auto upper_bound(const _Kt &__x) const -> decltype(const_iterator(_M_t._M_upper_bound_tr(__x)))
Finds the end of a subsequence matching given key.
Definition: stl_multimap.h:892
bool operator>(const multimap< _Key, _Tp, _Compare, _Alloc > &__x, const multimap< _Key, _Tp, _Compare, _Alloc > &__y)
Based on operator<.
auto equal_range(const _Kt &__x) -> decltype(pair< iterator, iterator >(_M_t._M_equal_range_tr(__x)))
Finds a subsequence matching given key.
Definition: stl_multimap.h:919
The standard allocator, as per [20.4].
Definition: allocator.h:101
bool empty() const noexcept
Definition: stl_multimap.h:441
auto find(const _Kt &__x) -> decltype(_M_t._M_find_tr(__x))
Tries to locate an element in a multimap.
Definition: stl_multimap.h:761
multimap(multimap &&__m, const allocator_type &__a) noexcept(is_nothrow_copy_constructible< _Compare >::value &&_Alloc_traits::_S_always_equal())
Allocator-extended move constructor.
Definition: stl_multimap.h:225
multimap(_InputIterator __first, _InputIterator __last, const _Compare &__comp, const allocator_type &__a=allocator_type())
Builds a multimap from a range.
Definition: stl_multimap.h:269
A standard container made up of (key,value) pairs, which can be retrieved based on a key...
Definition: stl_multimap.h:95
bool operator>=(const multimap< _Key, _Tp, _Compare, _Alloc > &__x, const multimap< _Key, _Tp, _Compare, _Alloc > &__y)
Based on operator<.
auto equal_range(const _Kt &__x) const -> decltype(pair< const_iterator, const_iterator >(_M_t._M_equal_range_tr(__x)))
Finds a subsequence matching given key.
Definition: stl_multimap.h:946
const_iterator cend() const noexcept
Definition: stl_multimap.h:416
const_reverse_iterator crend() const noexcept
Definition: stl_multimap.h:434
void swap(multimap &__x) noexcept(_Alloc_traits::_S_nothrow_swap())
Swaps data with another multimap.
Definition: stl_multimap.h:707
iterator find(const key_type &__x)
Tries to locate an element in a multimap.
Definition: stl_multimap.h:755
const_iterator lower_bound(const key_type &__x) const
Finds the beginning of a subsequence matching given key.
Definition: stl_multimap.h:846
iterator erase(const_iterator __position)
Erases an element from a multimap.
Definition: stl_multimap.h:615
const_iterator upper_bound(const key_type &__x) const
Finds the end of a subsequence matching given key.
Definition: stl_multimap.h:886
multimap() noexcept(is_nothrow_default_constructible< allocator_type >::value &&is_nothrow_default_constructible< key_compare >::value)
Default constructor creates no elements.
Definition: stl_multimap.h:160
multimap(const multimap &__x)
Multimap copy constructor.
Definition: stl_multimap.h:184
_T1 first
second_type is the second bound type
Definition: stl_pair.h:101
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:209
const_reverse_iterator rbegin() const noexcept
Definition: stl_multimap.h:379
const_iterator end() const noexcept
Definition: stl_multimap.h:361
iterator begin() noexcept
Definition: stl_multimap.h:334
const_reverse_iterator rend() const noexcept
Definition: stl_multimap.h:397
auto find(const _Kt &__x) const -> decltype(_M_t._M_find_tr(__x))
Tries to locate an element in a multimap.
Definition: stl_multimap.h:785
multimap(const _Compare &__comp, const allocator_type &__a=allocator_type())
Creates a multimap with no elements.
Definition: stl_multimap.h:173
size_type size() const noexcept
Definition: stl_multimap.h:446