libstdc++
stl_multimap.h
Go to the documentation of this file.
1 // Multimap implementation -*- C++ -*-
2 
3 // Copyright (C) 2001-2017 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  template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
69  class map;
70 
71  /**
72  * @brief A standard container made up of (key,value) pairs, which can be
73  * retrieved based on a key, in logarithmic time.
74  *
75  * @ingroup associative_containers
76  *
77  * @tparam _Key Type of key objects.
78  * @tparam _Tp Type of mapped objects.
79  * @tparam _Compare Comparison function object type, defaults to less<_Key>.
80  * @tparam _Alloc Allocator type, defaults to
81  * allocator<pair<const _Key, _Tp>.
82  *
83  * Meets the requirements of a <a href="tables.html#65">container</a>, a
84  * <a href="tables.html#66">reversible container</a>, and an
85  * <a href="tables.html#69">associative container</a> (using equivalent
86  * keys). For a @c multimap<Key,T> the key_type is Key, the mapped_type
87  * is T, and the value_type is std::pair<const Key,T>.
88  *
89  * Multimaps support bidirectional iterators.
90  *
91  * The private tree data is declared exactly the same way for map and
92  * multimap; the distinction is made entirely in how the tree functions are
93  * called (*_unique versus *_equal, same as the standard).
94  */
95  template <typename _Key, typename _Tp,
96  typename _Compare = std::less<_Key>,
97  typename _Alloc = std::allocator<std::pair<const _Key, _Tp> > >
98  class multimap
99  {
100  public:
101  typedef _Key key_type;
102  typedef _Tp mapped_type;
103  typedef std::pair<const _Key, _Tp> value_type;
104  typedef _Compare key_compare;
105  typedef _Alloc allocator_type;
106 
107  private:
108 #ifdef _GLIBCXX_CONCEPT_CHECKS
109  // concept requirements
110  typedef typename _Alloc::value_type _Alloc_value_type;
111 # if __cplusplus < 201103L
112  __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
113 # endif
114  __glibcxx_class_requires4(_Compare, bool, _Key, _Key,
115  _BinaryFunctionConcept)
116  __glibcxx_class_requires2(value_type, _Alloc_value_type, _SameTypeConcept)
117 #endif
118 
119  public:
120  class value_compare
121  : public std::binary_function<value_type, value_type, bool>
122  {
123  friend class multimap<_Key, _Tp, _Compare, _Alloc>;
124  protected:
125  _Compare comp;
126 
127  value_compare(_Compare __c)
128  : comp(__c) { }
129 
130  public:
131  bool operator()(const value_type& __x, const value_type& __y) const
132  { return comp(__x.first, __y.first); }
133  };
134 
135  private:
136  /// This turns a red-black tree into a [multi]map.
138  rebind<value_type>::other _Pair_alloc_type;
139 
140  typedef _Rb_tree<key_type, value_type, _Select1st<value_type>,
141  key_compare, _Pair_alloc_type> _Rep_type;
142  /// The actual tree structure.
143  _Rep_type _M_t;
144 
145  typedef __gnu_cxx::__alloc_traits<_Pair_alloc_type> _Alloc_traits;
146 
147  public:
148  // many of these are specified differently in ISO, but the following are
149  // "functionally equivalent"
150  typedef typename _Alloc_traits::pointer pointer;
151  typedef typename _Alloc_traits::const_pointer const_pointer;
152  typedef typename _Alloc_traits::reference reference;
153  typedef typename _Alloc_traits::const_reference const_reference;
154  typedef typename _Rep_type::iterator iterator;
155  typedef typename _Rep_type::const_iterator const_iterator;
156  typedef typename _Rep_type::size_type size_type;
157  typedef typename _Rep_type::difference_type difference_type;
158  typedef typename _Rep_type::reverse_iterator reverse_iterator;
159  typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator;
160 
161 #if __cplusplus > 201402L
162  using node_type = typename _Rep_type::node_type;
163 #endif
164 
165  // [23.3.2] construct/copy/destroy
166  // (get_allocator() is also listed in this section)
167 
168  /**
169  * @brief Default constructor creates no elements.
170  */
171 #if __cplusplus < 201103L
172  multimap() : _M_t() { }
173 #else
174  multimap() = default;
175 #endif
176 
177  /**
178  * @brief Creates a %multimap with no elements.
179  * @param __comp A comparison object.
180  * @param __a An allocator object.
181  */
182  explicit
183  multimap(const _Compare& __comp,
184  const allocator_type& __a = allocator_type())
185  : _M_t(__comp, _Pair_alloc_type(__a)) { }
186 
187  /**
188  * @brief %Multimap copy constructor.
189  *
190  * Whether the allocator is copied depends on the allocator traits.
191  */
192 #if __cplusplus < 201103L
193  multimap(const multimap& __x)
194  : _M_t(__x._M_t) { }
195 #else
196  multimap(const multimap&) = default;
197 
198  /**
199  * @brief %Multimap move constructor.
200  *
201  * The newly-created %multimap contains the exact contents of the
202  * moved instance. The moved instance is a valid, but unspecified
203  * %multimap.
204  */
205  multimap(multimap&&) = default;
206 
207  /**
208  * @brief Builds a %multimap from an initializer_list.
209  * @param __l An initializer_list.
210  * @param __comp A comparison functor.
211  * @param __a An allocator object.
212  *
213  * Create a %multimap consisting of copies of the elements from
214  * the initializer_list. This is linear in N if the list is already
215  * sorted, and NlogN otherwise (where N is @a __l.size()).
216  */
218  const _Compare& __comp = _Compare(),
219  const allocator_type& __a = allocator_type())
220  : _M_t(__comp, _Pair_alloc_type(__a))
221  { _M_t._M_insert_equal(__l.begin(), __l.end()); }
222 
223  /// Allocator-extended default constructor.
224  explicit
225  multimap(const allocator_type& __a)
226  : _M_t(_Compare(), _Pair_alloc_type(__a)) { }
227 
228  /// Allocator-extended copy constructor.
229  multimap(const multimap& __m, const allocator_type& __a)
230  : _M_t(__m._M_t, _Pair_alloc_type(__a)) { }
231 
232  /// Allocator-extended move constructor.
233  multimap(multimap&& __m, const allocator_type& __a)
234  noexcept(is_nothrow_copy_constructible<_Compare>::value
235  && _Alloc_traits::_S_always_equal())
236  : _M_t(std::move(__m._M_t), _Pair_alloc_type(__a)) { }
237 
238  /// Allocator-extended initialier-list constructor.
239  multimap(initializer_list<value_type> __l, const allocator_type& __a)
240  : _M_t(_Compare(), _Pair_alloc_type(__a))
241  { _M_t._M_insert_equal(__l.begin(), __l.end()); }
242 
243  /// Allocator-extended range constructor.
244  template<typename _InputIterator>
245  multimap(_InputIterator __first, _InputIterator __last,
246  const allocator_type& __a)
247  : _M_t(_Compare(), _Pair_alloc_type(__a))
248  { _M_t._M_insert_equal(__first, __last); }
249 #endif
250 
251  /**
252  * @brief Builds a %multimap from a range.
253  * @param __first An input iterator.
254  * @param __last An input iterator.
255  *
256  * Create a %multimap consisting of copies of the elements from
257  * [__first,__last). This is linear in N if the range is already sorted,
258  * and NlogN otherwise (where N is distance(__first,__last)).
259  */
260  template<typename _InputIterator>
261  multimap(_InputIterator __first, _InputIterator __last)
262  : _M_t()
263  { _M_t._M_insert_equal(__first, __last); }
264 
265  /**
266  * @brief Builds a %multimap from a range.
267  * @param __first An input iterator.
268  * @param __last An input iterator.
269  * @param __comp A comparison functor.
270  * @param __a An allocator object.
271  *
272  * Create a %multimap consisting of copies of the elements from
273  * [__first,__last). This is linear in N if the range is already sorted,
274  * and NlogN otherwise (where N is distance(__first,__last)).
275  */
276  template<typename _InputIterator>
277  multimap(_InputIterator __first, _InputIterator __last,
278  const _Compare& __comp,
279  const allocator_type& __a = allocator_type())
280  : _M_t(__comp, _Pair_alloc_type(__a))
281  { _M_t._M_insert_equal(__first, __last); }
282 
283 #if __cplusplus >= 201103L
284  /**
285  * The dtor only erases the elements, and note that if the elements
286  * themselves are pointers, the pointed-to memory is not touched in any
287  * way. Managing the pointer is the user's responsibility.
288  */
289  ~multimap() = default;
290 #endif
291 
292  /**
293  * @brief %Multimap assignment operator.
294  *
295  * Whether the allocator is copied depends on the allocator traits.
296  */
297 #if __cplusplus < 201103L
298  multimap&
299  operator=(const multimap& __x)
300  {
301  _M_t = __x._M_t;
302  return *this;
303  }
304 #else
305  multimap&
306  operator=(const multimap&) = default;
307 
308  /// Move assignment operator.
309  multimap&
310  operator=(multimap&&) = default;
311 
312  /**
313  * @brief %Multimap list assignment operator.
314  * @param __l An initializer_list.
315  *
316  * This function fills a %multimap with copies of the elements
317  * in the initializer list @a __l.
318  *
319  * Note that the assignment completely changes the %multimap and
320  * that the resulting %multimap's size is the same as the number
321  * of elements assigned.
322  */
323  multimap&
325  {
326  _M_t._M_assign_equal(__l.begin(), __l.end());
327  return *this;
328  }
329 #endif
330 
331  /// Get a copy of the memory allocation object.
332  allocator_type
333  get_allocator() const _GLIBCXX_NOEXCEPT
334  { return allocator_type(_M_t.get_allocator()); }
335 
336  // iterators
337  /**
338  * Returns a read/write iterator that points to the first pair in the
339  * %multimap. Iteration is done in ascending order according to the
340  * keys.
341  */
342  iterator
343  begin() _GLIBCXX_NOEXCEPT
344  { return _M_t.begin(); }
345 
346  /**
347  * Returns a read-only (constant) iterator that points to the first pair
348  * in the %multimap. Iteration is done in ascending order according to
349  * the keys.
350  */
351  const_iterator
352  begin() const _GLIBCXX_NOEXCEPT
353  { return _M_t.begin(); }
354 
355  /**
356  * Returns a read/write iterator that points one past the last pair in
357  * the %multimap. Iteration is done in ascending order according to the
358  * keys.
359  */
360  iterator
361  end() _GLIBCXX_NOEXCEPT
362  { return _M_t.end(); }
363 
364  /**
365  * Returns a read-only (constant) iterator that points one past the last
366  * pair in the %multimap. Iteration is done in ascending order according
367  * to the keys.
368  */
369  const_iterator
370  end() const _GLIBCXX_NOEXCEPT
371  { return _M_t.end(); }
372 
373  /**
374  * Returns a read/write reverse iterator that points to the last pair in
375  * the %multimap. Iteration is done in descending order according to the
376  * keys.
377  */
378  reverse_iterator
379  rbegin() _GLIBCXX_NOEXCEPT
380  { return _M_t.rbegin(); }
381 
382  /**
383  * Returns a read-only (constant) reverse iterator that points to the
384  * last pair in the %multimap. Iteration is done in descending order
385  * according to the keys.
386  */
387  const_reverse_iterator
388  rbegin() const _GLIBCXX_NOEXCEPT
389  { return _M_t.rbegin(); }
390 
391  /**
392  * Returns a read/write reverse iterator that points to one before the
393  * first pair in the %multimap. Iteration is done in descending order
394  * according to the keys.
395  */
396  reverse_iterator
397  rend() _GLIBCXX_NOEXCEPT
398  { return _M_t.rend(); }
399 
400  /**
401  * Returns a read-only (constant) reverse iterator that points to one
402  * before the first pair in the %multimap. Iteration is done in
403  * descending order according to the keys.
404  */
405  const_reverse_iterator
406  rend() const _GLIBCXX_NOEXCEPT
407  { return _M_t.rend(); }
408 
409 #if __cplusplus >= 201103L
410  /**
411  * Returns a read-only (constant) iterator that points to the first pair
412  * in the %multimap. Iteration is done in ascending order according to
413  * the keys.
414  */
415  const_iterator
416  cbegin() const noexcept
417  { return _M_t.begin(); }
418 
419  /**
420  * Returns a read-only (constant) iterator that points one past the last
421  * pair in the %multimap. Iteration is done in ascending order according
422  * to the keys.
423  */
424  const_iterator
425  cend() const noexcept
426  { return _M_t.end(); }
427 
428  /**
429  * Returns a read-only (constant) reverse iterator that points to the
430  * last pair in the %multimap. Iteration is done in descending order
431  * according to the keys.
432  */
433  const_reverse_iterator
434  crbegin() const noexcept
435  { return _M_t.rbegin(); }
436 
437  /**
438  * Returns a read-only (constant) reverse iterator that points to one
439  * before the first pair in the %multimap. Iteration is done in
440  * descending order according to the keys.
441  */
442  const_reverse_iterator
443  crend() const noexcept
444  { return _M_t.rend(); }
445 #endif
446 
447  // capacity
448  /** Returns true if the %multimap is empty. */
449  bool
450  empty() const _GLIBCXX_NOEXCEPT
451  { return _M_t.empty(); }
452 
453  /** Returns the size of the %multimap. */
454  size_type
455  size() const _GLIBCXX_NOEXCEPT
456  { return _M_t.size(); }
457 
458  /** Returns the maximum size of the %multimap. */
459  size_type
460  max_size() const _GLIBCXX_NOEXCEPT
461  { return _M_t.max_size(); }
462 
463  // modifiers
464 #if __cplusplus >= 201103L
465  /**
466  * @brief Build and insert a std::pair into the %multimap.
467  *
468  * @param __args Arguments used to generate a new pair instance (see
469  * std::piecewise_contruct for passing arguments to each
470  * part of the pair constructor).
471  *
472  * @return An iterator that points to the inserted (key,value) pair.
473  *
474  * This function builds and inserts a (key, value) %pair into the
475  * %multimap.
476  * Contrary to a std::map the %multimap does not rely on unique keys and
477  * thus multiple pairs with the same key can be inserted.
478  *
479  * Insertion requires logarithmic time.
480  */
481  template<typename... _Args>
482  iterator
483  emplace(_Args&&... __args)
484  { return _M_t._M_emplace_equal(std::forward<_Args>(__args)...); }
485 
486  /**
487  * @brief Builds and inserts a std::pair into the %multimap.
488  *
489  * @param __pos An iterator that serves as a hint as to where the pair
490  * should be inserted.
491  * @param __args Arguments used to generate a new pair instance (see
492  * std::piecewise_contruct for passing arguments to each
493  * part of the pair constructor).
494  * @return An iterator that points to the inserted (key,value) pair.
495  *
496  * This function inserts a (key, value) pair into the %multimap.
497  * Contrary to a std::map the %multimap does not rely on unique keys and
498  * thus multiple pairs with the same key can be inserted.
499  * Note that the first parameter is only a hint and can potentially
500  * improve the performance of the insertion process. A bad hint would
501  * cause no gains in efficiency.
502  *
503  * For more on @a hinting, see:
504  * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
505  *
506  * Insertion requires logarithmic time (if the hint is not taken).
507  */
508  template<typename... _Args>
509  iterator
510  emplace_hint(const_iterator __pos, _Args&&... __args)
511  {
512  return _M_t._M_emplace_hint_equal(__pos,
513  std::forward<_Args>(__args)...);
514  }
515 #endif
516 
517  /**
518  * @brief Inserts a std::pair into the %multimap.
519  * @param __x Pair to be inserted (see std::make_pair for easy creation
520  * of pairs).
521  * @return An iterator that points to the inserted (key,value) pair.
522  *
523  * This function inserts a (key, value) pair into the %multimap.
524  * Contrary to a std::map the %multimap does not rely on unique keys and
525  * thus multiple pairs with the same key can be inserted.
526  *
527  * Insertion requires logarithmic time.
528  */
529  iterator
530  insert(const value_type& __x)
531  { return _M_t._M_insert_equal(__x); }
532 
533 #if __cplusplus >= 201103L
534  template<typename _Pair, typename = typename
535  std::enable_if<std::is_constructible<value_type,
536  _Pair&&>::value>::type>
537  iterator
538  insert(_Pair&& __x)
539  { return _M_t._M_insert_equal(std::forward<_Pair>(__x)); }
540 #endif
541 
542  /**
543  * @brief Inserts a std::pair into the %multimap.
544  * @param __position An iterator that serves as a hint as to where the
545  * pair should be inserted.
546  * @param __x Pair to be inserted (see std::make_pair for easy creation
547  * of pairs).
548  * @return An iterator that points to the inserted (key,value) pair.
549  *
550  * This function inserts a (key, value) pair into the %multimap.
551  * Contrary to a std::map the %multimap does not rely on unique keys and
552  * thus multiple pairs with the same key can be inserted.
553  * Note that the first parameter is only a hint and can potentially
554  * improve the performance of the insertion process. A bad hint would
555  * cause no gains in efficiency.
556  *
557  * For more on @a hinting, see:
558  * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
559  *
560  * Insertion requires logarithmic time (if the hint is not taken).
561  */
562  iterator
563 #if __cplusplus >= 201103L
564  insert(const_iterator __position, const value_type& __x)
565 #else
566  insert(iterator __position, const value_type& __x)
567 #endif
568  { return _M_t._M_insert_equal_(__position, __x); }
569 
570 #if __cplusplus >= 201103L
571  template<typename _Pair, typename = typename
572  std::enable_if<std::is_constructible<value_type,
573  _Pair&&>::value>::type>
574  iterator
575  insert(const_iterator __position, _Pair&& __x)
576  { return _M_t._M_insert_equal_(__position,
577  std::forward<_Pair>(__x)); }
578 #endif
579 
580  /**
581  * @brief A template function that attempts to insert a range
582  * of elements.
583  * @param __first Iterator pointing to the start of the range to be
584  * inserted.
585  * @param __last Iterator pointing to the end of the range.
586  *
587  * Complexity similar to that of the range constructor.
588  */
589  template<typename _InputIterator>
590  void
591  insert(_InputIterator __first, _InputIterator __last)
592  { _M_t._M_insert_equal(__first, __last); }
593 
594 #if __cplusplus >= 201103L
595  /**
596  * @brief Attempts to insert a list of std::pairs into the %multimap.
597  * @param __l A std::initializer_list<value_type> of pairs to be
598  * inserted.
599  *
600  * Complexity similar to that of the range constructor.
601  */
602  void
604  { this->insert(__l.begin(), __l.end()); }
605 #endif
606 
607 #if __cplusplus > 201402L
608  /// Extract a node.
609  node_type
610  extract(const_iterator __pos)
611  {
612  __glibcxx_assert(__pos != end());
613  return _M_t.extract(__pos);
614  }
615 
616  /// Extract a node.
617  node_type
618  extract(const key_type& __x)
619  { return _M_t.extract(__x); }
620 
621  /// Re-insert an extracted node.
622  iterator
623  insert(node_type&& __nh)
624  { return _M_t._M_reinsert_node_equal(std::move(__nh)); }
625 
626  /// Re-insert an extracted node.
627  iterator
628  insert(const_iterator __hint, node_type&& __nh)
629  { return _M_t._M_reinsert_node_hint_equal(__hint, std::move(__nh)); }
630 
631  template<typename, typename>
632  friend class _Rb_tree_merge_helper;
633 
634  template<typename _C2>
635  void
636  merge(multimap<_Key, _Tp, _C2, _Alloc>& __source)
637  {
638  using _Merge_helper = _Rb_tree_merge_helper<multimap, _C2>;
639  _M_t._M_merge_equal(_Merge_helper::_S_get_tree(__source));
640  }
641 
642  template<typename _C2>
643  void
644  merge(multimap<_Key, _Tp, _C2, _Alloc>&& __source)
645  { merge(__source); }
646 
647  template<typename _C2>
648  void
649  merge(map<_Key, _Tp, _C2, _Alloc>& __source)
650  {
651  using _Merge_helper = _Rb_tree_merge_helper<multimap, _C2>;
652  _M_t._M_merge_equal(_Merge_helper::_S_get_tree(__source));
653  }
654 
655  template<typename _C2>
656  void
657  merge(map<_Key, _Tp, _C2, _Alloc>&& __source)
658  { merge(__source); }
659 #endif // C++17
660 
661 #if __cplusplus >= 201103L
662  // _GLIBCXX_RESOLVE_LIB_DEFECTS
663  // DR 130. Associative erase should return an iterator.
664  /**
665  * @brief Erases an element from a %multimap.
666  * @param __position An iterator pointing to the element to be erased.
667  * @return An iterator pointing to the element immediately following
668  * @a position prior to the element being erased. If no such
669  * element exists, end() is returned.
670  *
671  * This function erases an element, pointed to by the given iterator,
672  * from a %multimap. Note that this function only erases the element,
673  * and that if the element is itself a pointer, the pointed-to memory is
674  * not touched in any way. Managing the pointer is the user's
675  * responsibility.
676  *
677  * @{
678  */
679  iterator
680  erase(const_iterator __position)
681  { return _M_t.erase(__position); }
682 
683  // LWG 2059.
684  _GLIBCXX_ABI_TAG_CXX11
685  iterator
686  erase(iterator __position)
687  { return _M_t.erase(__position); }
688  // @}
689 #else
690  /**
691  * @brief Erases an element from a %multimap.
692  * @param __position An iterator pointing to the element to be erased.
693  *
694  * This function erases an element, pointed to by the given iterator,
695  * from a %multimap. Note that this function only erases the element,
696  * and that if the element is itself a pointer, the pointed-to memory is
697  * not touched in any way. Managing the pointer is the user's
698  * responsibility.
699  */
700  void
701  erase(iterator __position)
702  { _M_t.erase(__position); }
703 #endif
704 
705  /**
706  * @brief Erases elements according to the provided key.
707  * @param __x Key of element to be erased.
708  * @return The number of elements erased.
709  *
710  * This function erases all elements located by the given key from a
711  * %multimap.
712  * Note that this function only erases the element, and that if
713  * the element is itself a pointer, the pointed-to memory is not touched
714  * in any way. Managing the pointer is the user's responsibility.
715  */
716  size_type
717  erase(const key_type& __x)
718  { return _M_t.erase(__x); }
719 
720 #if __cplusplus >= 201103L
721  // _GLIBCXX_RESOLVE_LIB_DEFECTS
722  // DR 130. Associative erase should return an iterator.
723  /**
724  * @brief Erases a [first,last) range of elements from a %multimap.
725  * @param __first Iterator pointing to the start of the range to be
726  * erased.
727  * @param __last Iterator pointing to the end of the range to be
728  * erased .
729  * @return The iterator @a __last.
730  *
731  * This function erases a sequence of elements from a %multimap.
732  * Note that this function only erases the elements, and that if
733  * the elements themselves are pointers, the pointed-to memory is not
734  * touched in any way. Managing the pointer is the user's
735  * responsibility.
736  */
737  iterator
738  erase(const_iterator __first, const_iterator __last)
739  { return _M_t.erase(__first, __last); }
740 #else
741  // _GLIBCXX_RESOLVE_LIB_DEFECTS
742  // DR 130. Associative erase should return an iterator.
743  /**
744  * @brief Erases a [first,last) range of elements from a %multimap.
745  * @param __first Iterator pointing to the start of the range to be
746  * erased.
747  * @param __last Iterator pointing to the end of the range to
748  * be erased.
749  *
750  * This function erases a sequence of elements from a %multimap.
751  * Note that this function only erases the elements, and that if
752  * the elements themselves are pointers, the pointed-to memory is not
753  * touched in any way. Managing the pointer is the user's
754  * responsibility.
755  */
756  void
757  erase(iterator __first, iterator __last)
758  { _M_t.erase(__first, __last); }
759 #endif
760 
761  /**
762  * @brief Swaps data with another %multimap.
763  * @param __x A %multimap of the same element and allocator types.
764  *
765  * This exchanges the elements between two multimaps in constant time.
766  * (It is only swapping a pointer, an integer, and an instance of
767  * the @c Compare type (which itself is often stateless and empty), so it
768  * should be quite fast.)
769  * Note that the global std::swap() function is specialized such that
770  * std::swap(m1,m2) will feed to this function.
771  *
772  * Whether the allocators are swapped depends on the allocator traits.
773  */
774  void
776  _GLIBCXX_NOEXCEPT_IF(__is_nothrow_swappable<_Compare>::value)
777  { _M_t.swap(__x._M_t); }
778 
779  /**
780  * Erases all elements in a %multimap. Note that this function only
781  * erases the elements, and that if the elements themselves are pointers,
782  * the pointed-to memory is not touched in any way. Managing the pointer
783  * is the user's responsibility.
784  */
785  void
786  clear() _GLIBCXX_NOEXCEPT
787  { _M_t.clear(); }
788 
789  // observers
790  /**
791  * Returns the key comparison object out of which the %multimap
792  * was constructed.
793  */
794  key_compare
795  key_comp() const
796  { return _M_t.key_comp(); }
797 
798  /**
799  * Returns a value comparison object, built from the key comparison
800  * object out of which the %multimap was constructed.
801  */
802  value_compare
803  value_comp() const
804  { return value_compare(_M_t.key_comp()); }
805 
806  // multimap operations
807 
808  //@{
809  /**
810  * @brief Tries to locate an element in a %multimap.
811  * @param __x Key of (key, value) pair to be located.
812  * @return Iterator pointing to sought-after element,
813  * or end() if not found.
814  *
815  * This function takes a key and tries to locate the element with which
816  * the key matches. If successful the function returns an iterator
817  * pointing to the sought after %pair. If unsuccessful it returns the
818  * past-the-end ( @c end() ) iterator.
819  */
820  iterator
821  find(const key_type& __x)
822  { return _M_t.find(__x); }
823 
824 #if __cplusplus > 201103L
825  template<typename _Kt>
826  auto
827  find(const _Kt& __x) -> decltype(_M_t._M_find_tr(__x))
828  { return _M_t._M_find_tr(__x); }
829 #endif
830  //@}
831 
832  //@{
833  /**
834  * @brief Tries to locate an element in a %multimap.
835  * @param __x Key of (key, value) pair to be located.
836  * @return Read-only (constant) iterator pointing to sought-after
837  * element, or end() if not found.
838  *
839  * This function takes a key and tries to locate the element with which
840  * the key matches. If successful the function returns a constant
841  * iterator pointing to the sought after %pair. If unsuccessful it
842  * returns the past-the-end ( @c end() ) iterator.
843  */
844  const_iterator
845  find(const key_type& __x) const
846  { return _M_t.find(__x); }
847 
848 #if __cplusplus > 201103L
849  template<typename _Kt>
850  auto
851  find(const _Kt& __x) const -> decltype(_M_t._M_find_tr(__x))
852  { return _M_t._M_find_tr(__x); }
853 #endif
854  //@}
855 
856  //@{
857  /**
858  * @brief Finds the number of elements with given key.
859  * @param __x Key of (key, value) pairs to be located.
860  * @return Number of elements with specified key.
861  */
862  size_type
863  count(const key_type& __x) const
864  { return _M_t.count(__x); }
865 
866 #if __cplusplus > 201103L
867  template<typename _Kt>
868  auto
869  count(const _Kt& __x) const -> decltype(_M_t._M_count_tr(__x))
870  { return _M_t._M_count_tr(__x); }
871 #endif
872  //@}
873 
874  //@{
875  /**
876  * @brief Finds the beginning of a subsequence matching given key.
877  * @param __x Key of (key, value) pair to be located.
878  * @return Iterator pointing to first element equal to or greater
879  * than key, or end().
880  *
881  * This function returns the first element of a subsequence of elements
882  * that matches the given key. If unsuccessful it returns an iterator
883  * pointing to the first element that has a greater value than given key
884  * or end() if no such element exists.
885  */
886  iterator
887  lower_bound(const key_type& __x)
888  { return _M_t.lower_bound(__x); }
889 
890 #if __cplusplus > 201103L
891  template<typename _Kt>
892  auto
893  lower_bound(const _Kt& __x)
894  -> decltype(iterator(_M_t._M_lower_bound_tr(__x)))
895  { return iterator(_M_t._M_lower_bound_tr(__x)); }
896 #endif
897  //@}
898 
899  //@{
900  /**
901  * @brief Finds the beginning of a subsequence matching given key.
902  * @param __x Key of (key, value) pair to be located.
903  * @return Read-only (constant) iterator pointing to first element
904  * equal to or greater than key, or end().
905  *
906  * This function returns the first element of a subsequence of
907  * elements that matches the given key. If unsuccessful the
908  * iterator will point to the next greatest element or, if no
909  * such greater element exists, to end().
910  */
911  const_iterator
912  lower_bound(const key_type& __x) const
913  { return _M_t.lower_bound(__x); }
914 
915 #if __cplusplus > 201103L
916  template<typename _Kt>
917  auto
918  lower_bound(const _Kt& __x) const
919  -> decltype(const_iterator(_M_t._M_lower_bound_tr(__x)))
920  { return const_iterator(_M_t._M_lower_bound_tr(__x)); }
921 #endif
922  //@}
923 
924  //@{
925  /**
926  * @brief Finds the end of a subsequence matching given key.
927  * @param __x Key of (key, value) pair to be located.
928  * @return Iterator pointing to the first element
929  * greater than key, or end().
930  */
931  iterator
932  upper_bound(const key_type& __x)
933  { return _M_t.upper_bound(__x); }
934 
935 #if __cplusplus > 201103L
936  template<typename _Kt>
937  auto
938  upper_bound(const _Kt& __x)
939  -> decltype(iterator(_M_t._M_upper_bound_tr(__x)))
940  { return iterator(_M_t._M_upper_bound_tr(__x)); }
941 #endif
942  //@}
943 
944  //@{
945  /**
946  * @brief Finds the end of a subsequence matching given key.
947  * @param __x Key of (key, value) pair to be located.
948  * @return Read-only (constant) iterator pointing to first iterator
949  * greater than key, or end().
950  */
951  const_iterator
952  upper_bound(const key_type& __x) const
953  { return _M_t.upper_bound(__x); }
954 
955 #if __cplusplus > 201103L
956  template<typename _Kt>
957  auto
958  upper_bound(const _Kt& __x) const
959  -> decltype(const_iterator(_M_t._M_upper_bound_tr(__x)))
960  { return const_iterator(_M_t._M_upper_bound_tr(__x)); }
961 #endif
962  //@}
963 
964  //@{
965  /**
966  * @brief Finds a subsequence matching given key.
967  * @param __x Key of (key, value) pairs to be located.
968  * @return Pair of iterators that possibly points to the subsequence
969  * matching given key.
970  *
971  * This function is equivalent to
972  * @code
973  * std::make_pair(c.lower_bound(val),
974  * c.upper_bound(val))
975  * @endcode
976  * (but is faster than making the calls separately).
977  */
979  equal_range(const key_type& __x)
980  { return _M_t.equal_range(__x); }
981 
982 #if __cplusplus > 201103L
983  template<typename _Kt>
984  auto
985  equal_range(const _Kt& __x)
986  -> decltype(pair<iterator, iterator>(_M_t._M_equal_range_tr(__x)))
987  { return pair<iterator, iterator>(_M_t._M_equal_range_tr(__x)); }
988 #endif
989  //@}
990 
991  //@{
992  /**
993  * @brief Finds a subsequence matching given key.
994  * @param __x Key of (key, value) pairs to be located.
995  * @return Pair of read-only (constant) iterators that possibly points
996  * to the subsequence matching given key.
997  *
998  * This function is equivalent to
999  * @code
1000  * std::make_pair(c.lower_bound(val),
1001  * c.upper_bound(val))
1002  * @endcode
1003  * (but is faster than making the calls separately).
1004  */
1006  equal_range(const key_type& __x) const
1007  { return _M_t.equal_range(__x); }
1008 
1009 #if __cplusplus > 201103L
1010  template<typename _Kt>
1011  auto
1012  equal_range(const _Kt& __x) const
1014  _M_t._M_equal_range_tr(__x)))
1015  {
1017  _M_t._M_equal_range_tr(__x));
1018  }
1019 #endif
1020  //@}
1021 
1022  template<typename _K1, typename _T1, typename _C1, typename _A1>
1023  friend bool
1026 
1027  template<typename _K1, typename _T1, typename _C1, typename _A1>
1028  friend bool
1029  operator<(const multimap<_K1, _T1, _C1, _A1>&,
1031  };
1032 
1033  /**
1034  * @brief Multimap equality comparison.
1035  * @param __x A %multimap.
1036  * @param __y A %multimap of the same type as @a __x.
1037  * @return True iff the size and elements of the maps are equal.
1038  *
1039  * This is an equivalence relation. It is linear in the size of the
1040  * multimaps. Multimaps are considered equivalent if their sizes are equal,
1041  * and if corresponding elements compare equal.
1042  */
1043  template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
1044  inline bool
1047  { return __x._M_t == __y._M_t; }
1048 
1049  /**
1050  * @brief Multimap ordering relation.
1051  * @param __x A %multimap.
1052  * @param __y A %multimap of the same type as @a __x.
1053  * @return True iff @a x is lexicographically less than @a y.
1054  *
1055  * This is a total ordering relation. It is linear in the size of the
1056  * multimaps. The elements must be comparable with @c <.
1057  *
1058  * See std::lexicographical_compare() for how the determination is made.
1059  */
1060  template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
1061  inline bool
1062  operator<(const multimap<_Key, _Tp, _Compare, _Alloc>& __x,
1064  { return __x._M_t < __y._M_t; }
1065 
1066  /// Based on operator==
1067  template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
1068  inline bool
1071  { return !(__x == __y); }
1072 
1073  /// Based on operator<
1074  template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
1075  inline bool
1078  { return __y < __x; }
1079 
1080  /// Based on operator<
1081  template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
1082  inline bool
1083  operator<=(const multimap<_Key, _Tp, _Compare, _Alloc>& __x,
1085  { return !(__y < __x); }
1086 
1087  /// Based on operator<
1088  template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
1089  inline bool
1092  { return !(__x < __y); }
1093 
1094  /// See std::multimap::swap().
1095  template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
1096  inline void
1099  _GLIBCXX_NOEXCEPT_IF(noexcept(__x.swap(__y)))
1100  { __x.swap(__y); }
1101 
1102 _GLIBCXX_END_NAMESPACE_CONTAINER
1103 
1104 #if __cplusplus > 201402L
1105 _GLIBCXX_BEGIN_NAMESPACE_VERSION
1106  // Allow std::multimap access to internals of compatible maps.
1107  template<typename _Key, typename _Val, typename _Cmp1, typename _Alloc,
1108  typename _Cmp2>
1109  struct
1110  _Rb_tree_merge_helper<_GLIBCXX_STD_C::multimap<_Key, _Val, _Cmp1, _Alloc>,
1111  _Cmp2>
1112  {
1113  private:
1114  friend class _GLIBCXX_STD_C::multimap<_Key, _Val, _Cmp1, _Alloc>;
1115 
1116  static auto&
1117  _S_get_tree(_GLIBCXX_STD_C::map<_Key, _Val, _Cmp2, _Alloc>& __map)
1118  { return __map._M_t; }
1119 
1120  static auto&
1121  _S_get_tree(_GLIBCXX_STD_C::multimap<_Key, _Val, _Cmp2, _Alloc>& __map)
1122  { return __map._M_t; }
1123  };
1124 _GLIBCXX_END_NAMESPACE_VERSION
1125 #endif // C++17
1126 
1127 } // namespace std
1128 
1129 #endif /* _STL_MULTIMAP_H */
Uniform interface to C++98 and C++11 allocators.
void insert(_InputIterator __first, _InputIterator __last)
A template function that attempts to insert a range of elements.
Definition: stl_multimap.h:591
iterator emplace_hint(const_iterator __pos, _Args &&... __args)
Builds and inserts a std::pair into the multimap.
Definition: stl_multimap.h:510
iterator insert(const_iterator __position, const value_type &__x)
Inserts a std::pair into the multimap.
Definition: stl_multimap.h:564
iterator begin() noexcept
Definition: stl_multimap.h:343
The standard allocator, as per [20.4].
Definition: allocator.h:108
const_reverse_iterator rend() const noexcept
Definition: stl_multimap.h:406
bool operator!=(const multimap< _Key, _Tp, _Compare, _Alloc > &__x, const multimap< _Key, _Tp, _Compare, _Alloc > &__y)
Based on operator==.
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:217
auto find(const _Kt &__x) -> decltype(_M_t._M_find_tr(__x))
Tries to locate an element in a multimap.
Definition: stl_multimap.h:827
const_iterator end() const noexcept
Definition: stl_multimap.h:370
const_reverse_iterator crbegin() const noexcept
Definition: stl_multimap.h:434
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:893
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:869
void clear() noexcept
Definition: stl_multimap.h:786
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:958
iterator erase(const_iterator __position)
Erases an element from a multimap.
Definition: stl_multimap.h:680
initializer_list
iterator find(const key_type &__x)
Tries to locate an element in a multimap.
Definition: stl_multimap.h:821
multimap & operator=(initializer_list< value_type > __l)
Multimap list assignment operator.
Definition: stl_multimap.h:324
ISO C++ entities toplevel namespace is std.
multimap()=default
Default constructor creates no elements.
const_iterator upper_bound(const key_type &__x) const
Finds the end of a subsequence matching given key.
Definition: stl_multimap.h:952
multimap(initializer_list< value_type > __l, const allocator_type &__a)
Allocator-extended initialier-list constructor.
Definition: stl_multimap.h:239
reverse_iterator rend() noexcept
Definition: stl_multimap.h:397
size_type max_size() const noexcept
Definition: stl_multimap.h:460
~multimap()=default
iterator end() noexcept
Definition: stl_multimap.h:361
std::pair< iterator, iterator > equal_range(const key_type &__x)
Finds a subsequence matching given key.
Definition: stl_multimap.h:979
multimap & operator=(const multimap &)=default
Multimap assignment operator.
bool empty() const noexcept
Definition: stl_multimap.h:450
A standard container made up of (key,value) pairs, which can be retrieved based on a key...
Definition: stl_map.h:99
multimap(_InputIterator __first, _InputIterator __last, const allocator_type &__a)
Allocator-extended range constructor.
Definition: stl_multimap.h:245
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:918
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:851
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:233
bool operator>(const multimap< _Key, _Tp, _Compare, _Alloc > &__x, const multimap< _Key, _Tp, _Compare, _Alloc > &__y)
Based on operator<.
size_type erase(const key_type &__x)
Erases elements according to the provided key.
Definition: stl_multimap.h:717
multimap(const _Compare &__comp, const allocator_type &__a=allocator_type())
Creates a multimap with no elements.
Definition: stl_multimap.h:183
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.
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:938
void insert(initializer_list< value_type > __l)
Attempts to insert a list of std::pairs into the multimap.
Definition: stl_multimap.h:603
std::pair< const_iterator, const_iterator > equal_range(const key_type &__x) const
Finds a subsequence matching given key.
iterator insert(const value_type &__x)
Inserts a std::pair into the multimap.
Definition: stl_multimap.h:530
_GLIBCXX_ABI_TAG_CXX11 iterator erase(iterator __position)
Erases an element from a multimap.
Definition: stl_multimap.h:686
multimap(const multimap &__m, const allocator_type &__a)
Allocator-extended copy constructor.
Definition: stl_multimap.h:229
multimap(_InputIterator __first, _InputIterator __last)
Builds a multimap from a range.
Definition: stl_multimap.h:261
multimap(const allocator_type &__a)
Allocator-extended default constructor.
Definition: stl_multimap.h:225
One of the comparison functors.
Definition: stl_function.h:340
iterator erase(const_iterator __first, const_iterator __last)
Erases a [first,last) range of elements from a multimap.
Definition: stl_multimap.h:738
const_iterator begin() const noexcept
Definition: stl_multimap.h:352
allocator_type get_allocator() const noexcept
Get a copy of the memory allocation object.
Definition: stl_multimap.h:333
bool operator==(const multimap< _Key, _Tp, _Compare, _Alloc > &__x, const multimap< _Key, _Tp, _Compare, _Alloc > &__y)
Multimap equality comparison.
bool operator>=(const multimap< _Key, _Tp, _Compare, _Alloc > &__x, const multimap< _Key, _Tp, _Compare, _Alloc > &__y)
Based on operator<.
size_type count(const key_type &__x) const
Finds the number of elements with given key.
Definition: stl_multimap.h:863
reverse_iterator rbegin() noexcept
Definition: stl_multimap.h:379
A standard container made up of (key,value) pairs, which can be retrieved based on a key...
Definition: stl_map.h:71
const_reverse_iterator crend() const noexcept
Definition: stl_multimap.h:443
const_iterator lower_bound(const key_type &__x) const
Finds the beginning of a subsequence matching given key.
Definition: stl_multimap.h:912
const_iterator cend() const noexcept
Definition: stl_multimap.h:425
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:985
void swap(multimap &__x) noexcept(/*conditional */)
Swaps data with another multimap.
Definition: stl_multimap.h:775
const_reverse_iterator rbegin() const noexcept
Definition: stl_multimap.h:388
size_type size() const noexcept
Definition: stl_multimap.h:455
iterator emplace(_Args &&... __args)
Build and insert a std::pair into the multimap.
Definition: stl_multimap.h:483
key_compare key_comp() const
Definition: stl_multimap.h:795
multimap(_InputIterator __first, _InputIterator __last, const _Compare &__comp, const allocator_type &__a=allocator_type())
Builds a multimap from a range.
Definition: stl_multimap.h:277
iterator upper_bound(const key_type &__x)
Finds the end of a subsequence matching given key.
Definition: stl_multimap.h:932
iterator lower_bound(const key_type &__x)
Finds the beginning of a subsequence matching given key.
Definition: stl_multimap.h:887
Struct holding two objects of arbitrary type.
Definition: stl_pair.h:198
value_compare value_comp() const
Definition: stl_multimap.h:803
const_iterator find(const key_type &__x) const
Tries to locate an element in a multimap.
Definition: stl_multimap.h:845
const_iterator cbegin() const noexcept
Definition: stl_multimap.h:416