1 // Multimap implementation -*- C++ -*-
3 // Copyright (C) 2001-2024 Free Software Foundation, Inc.
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)
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.
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.
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/>.
28 * Hewlett-Packard Company
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.
39 * Copyright (c) 1996,1997
40 * Silicon Graphics Computer Systems, Inc.
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.
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}
56 #ifndef _STL_MULTIMAP_H
57 #define _STL_MULTIMAP_H 1
59 #include <bits/concept_check.h>
60 #if __cplusplus >= 201103L
61 #include <initializer_list>
64 namespace std
_GLIBCXX_VISIBILITY(default)
66 _GLIBCXX_BEGIN_NAMESPACE_VERSION
67 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
69 template <typename _Key
, typename _Tp
, typename _Compare
, typename _Alloc
>
73 * @brief A standard container made up of (key,value) pairs, which can be
74 * retrieved based on a key, in logarithmic time.
76 * @ingroup associative_containers
80 * @tparam _Key Type of key objects.
81 * @tparam _Tp Type of mapped objects.
82 * @tparam _Compare Comparison function object type, defaults to less<_Key>.
83 * @tparam _Alloc Allocator type, defaults to
84 * allocator<pair<const _Key, _Tp>.
86 * Meets the requirements of a <a href="tables.html#65">container</a>, a
87 * <a href="tables.html#66">reversible container</a>, and an
88 * <a href="tables.html#69">associative container</a> (using equivalent
89 * keys). For a @c multimap<Key,T> the key_type is Key, the mapped_type
90 * is T, and the value_type is std::pair<const Key,T>.
92 * Multimaps support bidirectional iterators.
94 * The private tree data is declared exactly the same way for map and
95 * multimap; the distinction is made entirely in how the tree functions are
96 * called (*_unique versus *_equal, same as the standard).
98 template <typename _Key
, typename _Tp
,
99 typename _Compare
= std::less
<_Key
>,
100 typename _Alloc
= std::allocator
<std::pair
<const _Key
, _Tp
> > >
104 typedef _Key key_type
;
105 typedef _Tp mapped_type
;
106 typedef std::pair
<const _Key
, _Tp
> value_type
;
107 typedef _Compare key_compare
;
108 typedef _Alloc allocator_type
;
111 #ifdef _GLIBCXX_CONCEPT_CHECKS
112 // concept requirements
113 typedef typename
_Alloc::value_type _Alloc_value_type
;
114 # if __cplusplus < 201103L
115 __glibcxx_class_requires(_Tp
, _SGIAssignableConcept
)
117 __glibcxx_class_requires4(_Compare
, bool, _Key
, _Key
,
118 _BinaryFunctionConcept
)
119 __glibcxx_class_requires2(value_type
, _Alloc_value_type
, _SameTypeConcept
)
122 #if __cplusplus >= 201103L
123 #if __cplusplus > 201703L || defined __STRICT_ANSI__
124 static_assert(is_same
<typename
_Alloc::value_type
, value_type
>::value
,
125 "std::multimap must have the same value_type as its allocator");
130 #pragma GCC diagnostic push
131 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
133 : public std::binary_function
<value_type
, value_type
, bool>
135 friend class multimap
<_Key
, _Tp
, _Compare
, _Alloc
>;
139 value_compare(_Compare __c
)
143 bool operator()(const value_type
& __x
, const value_type
& __y
) const
144 { return comp(__x
.first
, __y
.first
); }
146 #pragma GCC diagnostic pop
149 /// This turns a red-black tree into a [multi]map.
150 typedef typename
__gnu_cxx::__alloc_traits
<_Alloc
>::template
151 rebind
<value_type
>::other _Pair_alloc_type
;
153 typedef _Rb_tree
<key_type
, value_type
, _Select1st
<value_type
>,
154 key_compare
, _Pair_alloc_type
> _Rep_type
;
155 /// The actual tree structure.
158 typedef __gnu_cxx::__alloc_traits
<_Pair_alloc_type
> _Alloc_traits
;
161 // many of these are specified differently in ISO, but the following are
162 // "functionally equivalent"
163 typedef typename
_Alloc_traits::pointer pointer
;
164 typedef typename
_Alloc_traits::const_pointer const_pointer
;
165 typedef typename
_Alloc_traits::reference reference
;
166 typedef typename
_Alloc_traits::const_reference const_reference
;
167 typedef typename
_Rep_type::iterator iterator
;
168 typedef typename
_Rep_type::const_iterator const_iterator
;
169 typedef typename
_Rep_type::size_type size_type
;
170 typedef typename
_Rep_type::difference_type difference_type
;
171 typedef typename
_Rep_type::reverse_iterator reverse_iterator
;
172 typedef typename
_Rep_type::const_reverse_iterator const_reverse_iterator
;
174 #if __cplusplus > 201402L
175 using node_type
= typename
_Rep_type::node_type
;
178 // [23.3.2] construct/copy/destroy
179 // (get_allocator() is also listed in this section)
182 * @brief Default constructor creates no elements.
184 #if __cplusplus < 201103L
185 multimap() : _M_t() { }
187 multimap() = default;
191 * @brief Creates a %multimap with no elements.
192 * @param __comp A comparison object.
193 * @param __a An allocator object.
196 multimap(const _Compare
& __comp
,
197 const allocator_type
& __a
= allocator_type())
198 : _M_t(__comp
, _Pair_alloc_type(__a
)) { }
201 * @brief %Multimap copy constructor.
203 * Whether the allocator is copied depends on the allocator traits.
205 #if __cplusplus < 201103L
206 multimap(const multimap
& __x
)
209 multimap(const multimap
&) = default;
212 * @brief %Multimap move constructor.
214 * The newly-created %multimap contains the exact contents of the
215 * moved instance. The moved instance is a valid, but unspecified
218 multimap(multimap
&&) = default;
221 * @brief Builds a %multimap from an initializer_list.
222 * @param __l An initializer_list.
223 * @param __comp A comparison functor.
224 * @param __a An allocator object.
226 * Create a %multimap consisting of copies of the elements from
227 * the initializer_list. This is linear in N if the list is already
228 * sorted, and NlogN otherwise (where N is @a __l.size()).
230 multimap(initializer_list
<value_type
> __l
,
231 const _Compare
& __comp
= _Compare(),
232 const allocator_type
& __a
= allocator_type())
233 : _M_t(__comp
, _Pair_alloc_type(__a
))
234 { _M_t
._M_insert_range_equal(__l
.begin(), __l
.end()); }
236 /// Allocator-extended default constructor.
238 multimap(const allocator_type
& __a
)
239 : _M_t(_Pair_alloc_type(__a
)) { }
241 /// Allocator-extended copy constructor.
242 multimap(const multimap
& __m
,
243 const __type_identity_t
<allocator_type
>& __a
)
244 : _M_t(__m
._M_t
, _Pair_alloc_type(__a
)) { }
246 /// Allocator-extended move constructor.
247 multimap(multimap
&& __m
, const __type_identity_t
<allocator_type
>& __a
)
248 noexcept(is_nothrow_copy_constructible
<_Compare
>::value
249 && _Alloc_traits::_S_always_equal())
250 : _M_t(std::move(__m
._M_t
), _Pair_alloc_type(__a
)) { }
252 /// Allocator-extended initialier-list constructor.
253 multimap(initializer_list
<value_type
> __l
, const allocator_type
& __a
)
254 : _M_t(_Pair_alloc_type(__a
))
255 { _M_t
._M_insert_range_equal(__l
.begin(), __l
.end()); }
257 /// Allocator-extended range constructor.
258 template<typename _InputIterator
>
259 multimap(_InputIterator __first
, _InputIterator __last
,
260 const allocator_type
& __a
)
261 : _M_t(_Pair_alloc_type(__a
))
262 { _M_t
._M_insert_range_equal(__first
, __last
); }
266 * @brief Builds a %multimap from a range.
267 * @param __first An input iterator.
268 * @param __last An input iterator.
270 * Create a %multimap consisting of copies of the elements from
271 * [__first,__last). This is linear in N if the range is already sorted,
272 * and NlogN otherwise (where N is distance(__first,__last)).
274 template<typename _InputIterator
>
275 multimap(_InputIterator __first
, _InputIterator __last
)
277 { _M_t
._M_insert_range_equal(__first
, __last
); }
280 * @brief Builds a %multimap from a range.
281 * @param __first An input iterator.
282 * @param __last An input iterator.
283 * @param __comp A comparison functor.
284 * @param __a An allocator object.
286 * Create a %multimap consisting of copies of the elements from
287 * [__first,__last). This is linear in N if the range is already sorted,
288 * and NlogN otherwise (where N is distance(__first,__last)).
290 template<typename _InputIterator
>
291 multimap(_InputIterator __first
, _InputIterator __last
,
292 const _Compare
& __comp
,
293 const allocator_type
& __a
= allocator_type())
294 : _M_t(__comp
, _Pair_alloc_type(__a
))
295 { _M_t
._M_insert_range_equal(__first
, __last
); }
297 #if __cplusplus >= 201103L
299 * The dtor only erases the elements, and note that if the elements
300 * themselves are pointers, the pointed-to memory is not touched in any
301 * way. Managing the pointer is the user's responsibility.
303 ~multimap() = default;
307 * @brief %Multimap assignment operator.
309 * Whether the allocator is copied depends on the allocator traits.
311 #if __cplusplus < 201103L
313 operator=(const multimap
& __x
)
320 operator=(const multimap
&) = default;
322 /// Move assignment operator.
324 operator=(multimap
&&) = default;
327 * @brief %Multimap list assignment operator.
328 * @param __l An initializer_list.
330 * This function fills a %multimap with copies of the elements
331 * in the initializer list @a __l.
333 * Note that the assignment completely changes the %multimap and
334 * that the resulting %multimap's size is the same as the number
335 * of elements assigned.
338 operator=(initializer_list
<value_type
> __l
)
340 _M_t
._M_assign_equal(__l
.begin(), __l
.end());
345 /// Get a copy of the memory allocation object.
347 get_allocator() const _GLIBCXX_NOEXCEPT
348 { return allocator_type(_M_t
.get_allocator()); }
352 * Returns a read/write iterator that points to the first pair in the
353 * %multimap. Iteration is done in ascending order according to the
357 begin() _GLIBCXX_NOEXCEPT
358 { return _M_t
.begin(); }
361 * Returns a read-only (constant) iterator that points to the first pair
362 * in the %multimap. Iteration is done in ascending order according to
366 begin() const _GLIBCXX_NOEXCEPT
367 { return _M_t
.begin(); }
370 * Returns a read/write iterator that points one past the last pair in
371 * the %multimap. Iteration is done in ascending order according to the
375 end() _GLIBCXX_NOEXCEPT
376 { return _M_t
.end(); }
379 * Returns a read-only (constant) iterator that points one past the last
380 * pair in the %multimap. Iteration is done in ascending order according
384 end() const _GLIBCXX_NOEXCEPT
385 { return _M_t
.end(); }
388 * Returns a read/write reverse iterator that points to the last pair in
389 * the %multimap. Iteration is done in descending order according to the
393 rbegin() _GLIBCXX_NOEXCEPT
394 { return _M_t
.rbegin(); }
397 * Returns a read-only (constant) reverse iterator that points to the
398 * last pair in the %multimap. Iteration is done in descending order
399 * according to the keys.
401 const_reverse_iterator
402 rbegin() const _GLIBCXX_NOEXCEPT
403 { return _M_t
.rbegin(); }
406 * Returns a read/write reverse iterator that points to one before the
407 * first pair in the %multimap. Iteration is done in descending order
408 * according to the keys.
411 rend() _GLIBCXX_NOEXCEPT
412 { return _M_t
.rend(); }
415 * Returns a read-only (constant) reverse iterator that points to one
416 * before the first pair in the %multimap. Iteration is done in
417 * descending order according to the keys.
419 const_reverse_iterator
420 rend() const _GLIBCXX_NOEXCEPT
421 { return _M_t
.rend(); }
423 #if __cplusplus >= 201103L
425 * Returns a read-only (constant) iterator that points to the first pair
426 * in the %multimap. Iteration is done in ascending order according to
430 cbegin() const noexcept
431 { return _M_t
.begin(); }
434 * Returns a read-only (constant) iterator that points one past the last
435 * pair in the %multimap. Iteration is done in ascending order according
439 cend() const noexcept
440 { return _M_t
.end(); }
443 * Returns a read-only (constant) reverse iterator that points to the
444 * last pair in the %multimap. Iteration is done in descending order
445 * according to the keys.
447 const_reverse_iterator
448 crbegin() const noexcept
449 { return _M_t
.rbegin(); }
452 * Returns a read-only (constant) reverse iterator that points to one
453 * before the first pair in the %multimap. Iteration is done in
454 * descending order according to the keys.
456 const_reverse_iterator
457 crend() const noexcept
458 { return _M_t
.rend(); }
462 /** Returns true if the %multimap is empty. */
463 _GLIBCXX_NODISCARD
bool
464 empty() const _GLIBCXX_NOEXCEPT
465 { return _M_t
.empty(); }
467 /** Returns the size of the %multimap. */
469 size() const _GLIBCXX_NOEXCEPT
470 { return _M_t
.size(); }
472 /** Returns the maximum size of the %multimap. */
474 max_size() const _GLIBCXX_NOEXCEPT
475 { return _M_t
.max_size(); }
478 #if __cplusplus >= 201103L
480 * @brief Build and insert a std::pair into the %multimap.
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).
486 * @return An iterator that points to the inserted (key,value) pair.
488 * This function builds and inserts a (key, value) %pair into the
490 * Contrary to a std::map the %multimap does not rely on unique keys and
491 * thus multiple pairs with the same key can be inserted.
493 * Insertion requires logarithmic time.
495 template<typename
... _Args
>
497 emplace(_Args
&&... __args
)
498 { return _M_t
._M_emplace_equal(std::forward
<_Args
>(__args
)...); }
501 * @brief Builds and inserts a std::pair into the %multimap.
503 * @param __pos An iterator that serves as a hint as to where the pair
504 * should be inserted.
505 * @param __args Arguments used to generate a new pair instance (see
506 * std::piecewise_contruct for passing arguments to each
507 * part of the pair constructor).
508 * @return An iterator that points to the inserted (key,value) pair.
510 * This function inserts a (key, value) pair into the %multimap.
511 * Contrary to a std::map the %multimap does not rely on unique keys and
512 * thus multiple pairs with the same key can be inserted.
513 * Note that the first parameter is only a hint and can potentially
514 * improve the performance of the insertion process. A bad hint would
515 * cause no gains in efficiency.
517 * For more on @a hinting, see:
518 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
520 * Insertion requires logarithmic time (if the hint is not taken).
522 template<typename
... _Args
>
524 emplace_hint(const_iterator __pos
, _Args
&&... __args
)
526 return _M_t
._M_emplace_hint_equal(__pos
,
527 std::forward
<_Args
>(__args
)...);
532 * @brief Inserts a std::pair into the %multimap.
533 * @param __x Pair to be inserted (see std::make_pair for easy creation
535 * @return An iterator that points to the inserted (key,value) pair.
537 * This function inserts a (key, value) pair into the %multimap.
538 * Contrary to a std::map the %multimap does not rely on unique keys and
539 * thus multiple pairs with the same key can be inserted.
541 * Insertion requires logarithmic time.
545 insert(const value_type
& __x
)
546 { return _M_t
._M_insert_equal(__x
); }
548 #if __cplusplus >= 201103L
549 // _GLIBCXX_RESOLVE_LIB_DEFECTS
550 // 2354. Unnecessary copying when inserting into maps with braced-init
552 insert(value_type
&& __x
)
553 { return _M_t
._M_insert_equal(std::move(__x
)); }
555 template<typename _Pair
>
556 __enable_if_t
<is_constructible
<value_type
, _Pair
>::value
, iterator
>
558 { return _M_t
._M_emplace_equal(std::forward
<_Pair
>(__x
)); }
563 * @brief Inserts a std::pair into the %multimap.
564 * @param __position An iterator that serves as a hint as to where the
565 * pair should be inserted.
566 * @param __x Pair to be inserted (see std::make_pair for easy creation
568 * @return An iterator that points to the inserted (key,value) pair.
570 * This function inserts a (key, value) pair into the %multimap.
571 * Contrary to a std::map the %multimap does not rely on unique keys and
572 * thus multiple pairs with the same key can be inserted.
573 * Note that the first parameter is only a hint and can potentially
574 * improve the performance of the insertion process. A bad hint would
575 * cause no gains in efficiency.
577 * For more on @a hinting, see:
578 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
580 * Insertion requires logarithmic time (if the hint is not taken).
584 #if __cplusplus >= 201103L
585 insert(const_iterator __position
, const value_type
& __x
)
587 insert(iterator __position
, const value_type
& __x
)
589 { return _M_t
._M_insert_equal_(__position
, __x
); }
591 #if __cplusplus >= 201103L
592 // _GLIBCXX_RESOLVE_LIB_DEFECTS
593 // 2354. Unnecessary copying when inserting into maps with braced-init
595 insert(const_iterator __position
, value_type
&& __x
)
596 { return _M_t
._M_insert_equal_(__position
, std::move(__x
)); }
598 template<typename _Pair
>
599 __enable_if_t
<is_constructible
<value_type
, _Pair
&&>::value
, iterator
>
600 insert(const_iterator __position
, _Pair
&& __x
)
602 return _M_t
._M_emplace_hint_equal(__position
,
603 std::forward
<_Pair
>(__x
));
609 * @brief A template function that attempts to insert a range
611 * @param __first Iterator pointing to the start of the range to be
613 * @param __last Iterator pointing to the end of the range.
615 * Complexity similar to that of the range constructor.
617 template<typename _InputIterator
>
619 insert(_InputIterator __first
, _InputIterator __last
)
620 { _M_t
._M_insert_range_equal(__first
, __last
); }
622 #if __cplusplus >= 201103L
624 * @brief Attempts to insert a list of std::pairs into the %multimap.
625 * @param __l A std::initializer_list<value_type> of pairs to be
628 * Complexity similar to that of the range constructor.
631 insert(initializer_list
<value_type
> __l
)
632 { this->insert(__l
.begin(), __l
.end()); }
635 #if __cplusplus > 201402L
638 extract(const_iterator __pos
)
640 __glibcxx_assert(__pos
!= end());
641 return _M_t
.extract(__pos
);
646 extract(const key_type
& __x
)
647 { return _M_t
.extract(__x
); }
649 /// Re-insert an extracted node.
651 insert(node_type
&& __nh
)
652 { return _M_t
._M_reinsert_node_equal(std::move(__nh
)); }
654 /// Re-insert an extracted node.
656 insert(const_iterator __hint
, node_type
&& __nh
)
657 { return _M_t
._M_reinsert_node_hint_equal(__hint
, std::move(__nh
)); }
659 template<typename
, typename
>
660 friend struct std::_Rb_tree_merge_helper
;
662 template<typename _Cmp2
>
664 merge(multimap
<_Key
, _Tp
, _Cmp2
, _Alloc
>& __source
)
666 using _Merge_helper
= _Rb_tree_merge_helper
<multimap
, _Cmp2
>;
667 _M_t
._M_merge_equal(_Merge_helper::_S_get_tree(__source
));
670 template<typename _Cmp2
>
672 merge(multimap
<_Key
, _Tp
, _Cmp2
, _Alloc
>&& __source
)
675 template<typename _Cmp2
>
677 merge(map
<_Key
, _Tp
, _Cmp2
, _Alloc
>& __source
)
679 using _Merge_helper
= _Rb_tree_merge_helper
<multimap
, _Cmp2
>;
680 _M_t
._M_merge_equal(_Merge_helper::_S_get_tree(__source
));
683 template<typename _Cmp2
>
685 merge(map
<_Key
, _Tp
, _Cmp2
, _Alloc
>&& __source
)
689 #if __cplusplus >= 201103L
690 // _GLIBCXX_RESOLVE_LIB_DEFECTS
691 // DR 130. Associative erase should return an iterator.
693 * @brief Erases an element from a %multimap.
694 * @param __position An iterator pointing to the element to be erased.
695 * @return An iterator pointing to the element immediately following
696 * @a position prior to the element being erased. If no such
697 * element exists, end() is returned.
699 * This function erases an element, pointed to by the given iterator,
700 * from a %multimap. Note that this function only erases the element,
701 * and that if the element is itself a pointer, the pointed-to memory is
702 * not touched in any way. Managing the pointer is the user's
708 erase(const_iterator __position
)
709 { return _M_t
.erase(__position
); }
712 _GLIBCXX_ABI_TAG_CXX11
714 erase(iterator __position
)
715 { return _M_t
.erase(__position
); }
719 * @brief Erases an element from a %multimap.
720 * @param __position An iterator pointing to the element to be erased.
722 * This function erases an element, pointed to by the given iterator,
723 * from a %multimap. Note that this function only erases the element,
724 * and that if the element is itself a pointer, the pointed-to memory is
725 * not touched in any way. Managing the pointer is the user's
729 erase(iterator __position
)
730 { _M_t
.erase(__position
); }
734 * @brief Erases elements according to the provided key.
735 * @param __x Key of element to be erased.
736 * @return The number of elements erased.
738 * This function erases all elements located by the given key from a
740 * Note that this function only erases the element, and that if
741 * the element is itself a pointer, the pointed-to memory is not touched
742 * in any way. Managing the pointer is the user's responsibility.
745 erase(const key_type
& __x
)
746 { return _M_t
.erase(__x
); }
748 #if __cplusplus >= 201103L
749 // _GLIBCXX_RESOLVE_LIB_DEFECTS
750 // DR 130. Associative erase should return an iterator.
752 * @brief Erases a [first,last) range of elements from a %multimap.
753 * @param __first Iterator pointing to the start of the range to be
755 * @param __last Iterator pointing to the end of the range to be
757 * @return The iterator @a __last.
759 * This function erases a sequence of elements from a %multimap.
760 * Note that this function only erases the elements, and that if
761 * the elements themselves are pointers, the pointed-to memory is not
762 * touched in any way. Managing the pointer is the user's
766 erase(const_iterator __first
, const_iterator __last
)
767 { return _M_t
.erase(__first
, __last
); }
769 // _GLIBCXX_RESOLVE_LIB_DEFECTS
770 // DR 130. Associative erase should return an iterator.
772 * @brief Erases a [first,last) range of elements from a %multimap.
773 * @param __first Iterator pointing to the start of the range to be
775 * @param __last Iterator pointing to the end of the range to
778 * This function erases a sequence of elements from a %multimap.
779 * Note that this function only erases the elements, and that if
780 * the elements themselves are pointers, the pointed-to memory is not
781 * touched in any way. Managing the pointer is the user's
785 erase(iterator __first
, iterator __last
)
786 { _M_t
.erase(__first
, __last
); }
790 * @brief Swaps data with another %multimap.
791 * @param __x A %multimap of the same element and allocator types.
793 * This exchanges the elements between two multimaps in constant time.
794 * (It is only swapping a pointer, an integer, and an instance of
795 * the @c Compare type (which itself is often stateless and empty), so it
796 * should be quite fast.)
797 * Note that the global std::swap() function is specialized such that
798 * std::swap(m1,m2) will feed to this function.
800 * Whether the allocators are swapped depends on the allocator traits.
804 _GLIBCXX_NOEXCEPT_IF(__is_nothrow_swappable
<_Compare
>::value
)
805 { _M_t
.swap(__x
._M_t
); }
808 * Erases all elements in a %multimap. Note that this function only
809 * erases the elements, and that if the elements themselves are pointers,
810 * the pointed-to memory is not touched in any way. Managing the pointer
811 * is the user's responsibility.
814 clear() _GLIBCXX_NOEXCEPT
819 * Returns the key comparison object out of which the %multimap
824 { return _M_t
.key_comp(); }
827 * Returns a value comparison object, built from the key comparison
828 * object out of which the %multimap was constructed.
832 { return value_compare(_M_t
.key_comp()); }
834 // multimap operations
838 * @brief Tries to locate an element in a %multimap.
839 * @param __x Key of (key, value) pair to be located.
840 * @return Iterator pointing to sought-after element,
841 * or end() if not found.
843 * This function takes a key and tries to locate the element with which
844 * the key matches. If successful the function returns an iterator
845 * pointing to the sought after %pair. If unsuccessful it returns the
846 * past-the-end ( @c end() ) iterator.
849 find(const key_type
& __x
)
850 { return _M_t
.find(__x
); }
852 #if __cplusplus > 201103L
853 template<typename _Kt
>
855 find(const _Kt
& __x
) -> decltype(_M_t
._M_find_tr(__x
))
856 { return _M_t
._M_find_tr(__x
); }
862 * @brief Tries to locate an element in a %multimap.
863 * @param __x Key of (key, value) pair to be located.
864 * @return Read-only (constant) iterator pointing to sought-after
865 * element, or end() if not found.
867 * This function takes a key and tries to locate the element with which
868 * the key matches. If successful the function returns a constant
869 * iterator pointing to the sought after %pair. If unsuccessful it
870 * returns the past-the-end ( @c end() ) iterator.
873 find(const key_type
& __x
) const
874 { return _M_t
.find(__x
); }
876 #if __cplusplus > 201103L
877 template<typename _Kt
>
879 find(const _Kt
& __x
) const -> decltype(_M_t
._M_find_tr(__x
))
880 { return _M_t
._M_find_tr(__x
); }
886 * @brief Finds the number of elements with given key.
887 * @param __x Key of (key, value) pairs to be located.
888 * @return Number of elements with specified key.
891 count(const key_type
& __x
) const
892 { return _M_t
.count(__x
); }
894 #if __cplusplus > 201103L
895 template<typename _Kt
>
897 count(const _Kt
& __x
) const -> decltype(_M_t
._M_count_tr(__x
))
898 { return _M_t
._M_count_tr(__x
); }
902 #if __cplusplus > 201703L
905 * @brief Finds whether an element with the given key exists.
906 * @param __x Key of (key, value) pairs to be located.
907 * @return True if there is any element with the specified key.
910 contains(const key_type
& __x
) const
911 { return _M_t
.find(__x
) != _M_t
.end(); }
913 template<typename _Kt
>
915 contains(const _Kt
& __x
) const
916 -> decltype(_M_t
._M_find_tr(__x
), void(), true)
917 { return _M_t
._M_find_tr(__x
) != _M_t
.end(); }
923 * @brief Finds the beginning of a subsequence matching given key.
924 * @param __x Key of (key, value) pair to be located.
925 * @return Iterator pointing to first element equal to or greater
926 * than key, or end().
928 * This function returns the first element of a subsequence of elements
929 * that matches the given key. If unsuccessful it returns an iterator
930 * pointing to the first element that has a greater value than given key
931 * or end() if no such element exists.
934 lower_bound(const key_type
& __x
)
935 { return _M_t
.lower_bound(__x
); }
937 #if __cplusplus > 201103L
938 template<typename _Kt
>
940 lower_bound(const _Kt
& __x
)
941 -> decltype(iterator(_M_t
._M_lower_bound_tr(__x
)))
942 { return iterator(_M_t
._M_lower_bound_tr(__x
)); }
948 * @brief Finds the beginning of a subsequence matching given key.
949 * @param __x Key of (key, value) pair to be located.
950 * @return Read-only (constant) iterator pointing to first element
951 * equal to or greater than key, or end().
953 * This function returns the first element of a subsequence of
954 * elements that matches the given key. If unsuccessful the
955 * iterator will point to the next greatest element or, if no
956 * such greater element exists, to end().
959 lower_bound(const key_type
& __x
) const
960 { return _M_t
.lower_bound(__x
); }
962 #if __cplusplus > 201103L
963 template<typename _Kt
>
965 lower_bound(const _Kt
& __x
) const
966 -> decltype(const_iterator(_M_t
._M_lower_bound_tr(__x
)))
967 { return const_iterator(_M_t
._M_lower_bound_tr(__x
)); }
973 * @brief Finds the end of a subsequence matching given key.
974 * @param __x Key of (key, value) pair to be located.
975 * @return Iterator pointing to the first element
976 * greater than key, or end().
979 upper_bound(const key_type
& __x
)
980 { return _M_t
.upper_bound(__x
); }
982 #if __cplusplus > 201103L
983 template<typename _Kt
>
985 upper_bound(const _Kt
& __x
)
986 -> decltype(iterator(_M_t
._M_upper_bound_tr(__x
)))
987 { return iterator(_M_t
._M_upper_bound_tr(__x
)); }
993 * @brief Finds the end of a subsequence matching given key.
994 * @param __x Key of (key, value) pair to be located.
995 * @return Read-only (constant) iterator pointing to first iterator
996 * greater than key, or end().
999 upper_bound(const key_type
& __x
) const
1000 { return _M_t
.upper_bound(__x
); }
1002 #if __cplusplus > 201103L
1003 template<typename _Kt
>
1005 upper_bound(const _Kt
& __x
) const
1006 -> decltype(const_iterator(_M_t
._M_upper_bound_tr(__x
)))
1007 { return const_iterator(_M_t
._M_upper_bound_tr(__x
)); }
1013 * @brief Finds a subsequence matching given key.
1014 * @param __x Key of (key, value) pairs to be located.
1015 * @return Pair of iterators that possibly points to the subsequence
1016 * matching given key.
1018 * This function is equivalent to
1020 * std::make_pair(c.lower_bound(val),
1021 * c.upper_bound(val))
1023 * (but is faster than making the calls separately).
1025 std::pair
<iterator
, iterator
>
1026 equal_range(const key_type
& __x
)
1027 { return _M_t
.equal_range(__x
); }
1029 #if __cplusplus > 201103L
1030 template<typename _Kt
>
1032 equal_range(const _Kt
& __x
)
1033 -> decltype(pair
<iterator
, iterator
>(_M_t
._M_equal_range_tr(__x
)))
1034 { return pair
<iterator
, iterator
>(_M_t
._M_equal_range_tr(__x
)); }
1040 * @brief Finds a subsequence matching given key.
1041 * @param __x Key of (key, value) pairs to be located.
1042 * @return Pair of read-only (constant) iterators that possibly points
1043 * to the subsequence matching given key.
1045 * This function is equivalent to
1047 * std::make_pair(c.lower_bound(val),
1048 * c.upper_bound(val))
1050 * (but is faster than making the calls separately).
1052 std::pair
<const_iterator
, const_iterator
>
1053 equal_range(const key_type
& __x
) const
1054 { return _M_t
.equal_range(__x
); }
1056 #if __cplusplus > 201103L
1057 template<typename _Kt
>
1059 equal_range(const _Kt
& __x
) const
1060 -> decltype(pair
<const_iterator
, const_iterator
>(
1061 _M_t
._M_equal_range_tr(__x
)))
1063 return pair
<const_iterator
, const_iterator
>(
1064 _M_t
._M_equal_range_tr(__x
));
1069 template<typename _K1
, typename _T1
, typename _C1
, typename _A1
>
1071 operator==(const multimap
<_K1
, _T1
, _C1
, _A1
>&,
1072 const multimap
<_K1
, _T1
, _C1
, _A1
>&);
1074 #if __cpp_lib_three_way_comparison
1075 template<typename _K1
, typename _T1
, typename _C1
, typename _A1
>
1076 friend __detail::__synth3way_t
<pair
<const _K1
, _T1
>>
1077 operator<=>(const multimap
<_K1
, _T1
, _C1
, _A1
>&,
1078 const multimap
<_K1
, _T1
, _C1
, _A1
>&);
1080 template<typename _K1
, typename _T1
, typename _C1
, typename _A1
>
1082 operator<(const multimap
<_K1
, _T1
, _C1
, _A1
>&,
1083 const multimap
<_K1
, _T1
, _C1
, _A1
>&);
1087 #if __cpp_deduction_guides >= 201606
1089 template<typename _InputIterator
,
1090 typename _Compare
= less
<__iter_key_t
<_InputIterator
>>,
1091 typename _Allocator
= allocator
<__iter_to_alloc_t
<_InputIterator
>>,
1092 typename
= _RequireInputIter
<_InputIterator
>,
1093 typename
= _RequireNotAllocator
<_Compare
>,
1094 typename
= _RequireAllocator
<_Allocator
>>
1095 multimap(_InputIterator
, _InputIterator
,
1096 _Compare
= _Compare(), _Allocator
= _Allocator())
1097 -> multimap
<__iter_key_t
<_InputIterator
>, __iter_val_t
<_InputIterator
>,
1098 _Compare
, _Allocator
>;
1100 template<typename _Key
, typename _Tp
, typename _Compare
= less
<_Key
>,
1101 typename _Allocator
= allocator
<pair
<const _Key
, _Tp
>>,
1102 typename
= _RequireNotAllocator
<_Compare
>,
1103 typename
= _RequireAllocator
<_Allocator
>>
1104 multimap(initializer_list
<pair
<_Key
, _Tp
>>,
1105 _Compare
= _Compare(), _Allocator
= _Allocator())
1106 -> multimap
<_Key
, _Tp
, _Compare
, _Allocator
>;
1108 template<typename _InputIterator
, typename _Allocator
,
1109 typename
= _RequireInputIter
<_InputIterator
>,
1110 typename
= _RequireAllocator
<_Allocator
>>
1111 multimap(_InputIterator
, _InputIterator
, _Allocator
)
1112 -> multimap
<__iter_key_t
<_InputIterator
>, __iter_val_t
<_InputIterator
>,
1113 less
<__iter_key_t
<_InputIterator
>>, _Allocator
>;
1115 template<typename _Key
, typename _Tp
, typename _Allocator
,
1116 typename
= _RequireAllocator
<_Allocator
>>
1117 multimap(initializer_list
<pair
<_Key
, _Tp
>>, _Allocator
)
1118 -> multimap
<_Key
, _Tp
, less
<_Key
>, _Allocator
>;
1120 #endif // deduction guides
1123 * @brief Multimap equality comparison.
1124 * @param __x A %multimap.
1125 * @param __y A %multimap of the same type as @a __x.
1126 * @return True iff the size and elements of the maps are equal.
1128 * This is an equivalence relation. It is linear in the size of the
1129 * multimaps. Multimaps are considered equivalent if their sizes are equal,
1130 * and if corresponding elements compare equal.
1132 template<typename _Key
, typename _Tp
, typename _Compare
, typename _Alloc
>
1134 operator==(const multimap
<_Key
, _Tp
, _Compare
, _Alloc
>& __x
,
1135 const multimap
<_Key
, _Tp
, _Compare
, _Alloc
>& __y
)
1136 { return __x
._M_t
== __y
._M_t
; }
1138 #if __cpp_lib_three_way_comparison
1140 * @brief Multimap ordering relation.
1141 * @param __x A `multimap`.
1142 * @param __y A `multimap` of the same type as `x`.
1143 * @return A value indicating whether `__x` is less than, equal to,
1144 * greater than, or incomparable with `__y`.
1146 * This is a total ordering relation. It is linear in the size of the
1147 * maps. The elements must be comparable with @c <.
1149 * See `std::lexicographical_compare_three_way()` for how the determination
1150 * is made. This operator is used to synthesize relational operators like
1153 template<typename _Key
, typename _Tp
, typename _Compare
, typename _Alloc
>
1154 inline __detail::__synth3way_t
<pair
<const _Key
, _Tp
>>
1155 operator<=>(const multimap
<_Key
, _Tp
, _Compare
, _Alloc
>& __x
,
1156 const multimap
<_Key
, _Tp
, _Compare
, _Alloc
>& __y
)
1157 { return __x
._M_t
<=> __y
._M_t
; }
1160 * @brief Multimap ordering relation.
1161 * @param __x A %multimap.
1162 * @param __y A %multimap of the same type as @a __x.
1163 * @return True iff @a x is lexicographically less than @a y.
1165 * This is a total ordering relation. It is linear in the size of the
1166 * multimaps. The elements must be comparable with @c <.
1168 * See std::lexicographical_compare() for how the determination is made.
1170 template<typename _Key
, typename _Tp
, typename _Compare
, typename _Alloc
>
1172 operator<(const multimap
<_Key
, _Tp
, _Compare
, _Alloc
>& __x
,
1173 const multimap
<_Key
, _Tp
, _Compare
, _Alloc
>& __y
)
1174 { return __x
._M_t
< __y
._M_t
; }
1176 /// Based on operator==
1177 template<typename _Key
, typename _Tp
, typename _Compare
, typename _Alloc
>
1179 operator!=(const multimap
<_Key
, _Tp
, _Compare
, _Alloc
>& __x
,
1180 const multimap
<_Key
, _Tp
, _Compare
, _Alloc
>& __y
)
1181 { return !(__x
== __y
); }
1183 /// Based on operator<
1184 template<typename _Key
, typename _Tp
, typename _Compare
, typename _Alloc
>
1186 operator>(const multimap
<_Key
, _Tp
, _Compare
, _Alloc
>& __x
,
1187 const multimap
<_Key
, _Tp
, _Compare
, _Alloc
>& __y
)
1188 { return __y
< __x
; }
1190 /// Based on operator<
1191 template<typename _Key
, typename _Tp
, typename _Compare
, typename _Alloc
>
1193 operator<=(const multimap
<_Key
, _Tp
, _Compare
, _Alloc
>& __x
,
1194 const multimap
<_Key
, _Tp
, _Compare
, _Alloc
>& __y
)
1195 { return !(__y
< __x
); }
1197 /// Based on operator<
1198 template<typename _Key
, typename _Tp
, typename _Compare
, typename _Alloc
>
1200 operator>=(const multimap
<_Key
, _Tp
, _Compare
, _Alloc
>& __x
,
1201 const multimap
<_Key
, _Tp
, _Compare
, _Alloc
>& __y
)
1202 { return !(__x
< __y
); }
1203 #endif // three-way comparison
1205 /// See std::multimap::swap().
1206 template<typename _Key
, typename _Tp
, typename _Compare
, typename _Alloc
>
1208 swap(multimap
<_Key
, _Tp
, _Compare
, _Alloc
>& __x
,
1209 multimap
<_Key
, _Tp
, _Compare
, _Alloc
>& __y
)
1210 _GLIBCXX_NOEXCEPT_IF(noexcept(__x
.swap(__y
)))
1213 _GLIBCXX_END_NAMESPACE_CONTAINER
1215 #if __cplusplus > 201402L
1216 // Allow std::multimap access to internals of compatible maps.
1217 template<typename _Key
, typename _Val
, typename _Cmp1
, typename _Alloc
,
1220 _Rb_tree_merge_helper
<_GLIBCXX_STD_C::multimap
<_Key
, _Val
, _Cmp1
, _Alloc
>,
1224 friend class _GLIBCXX_STD_C::multimap
<_Key
, _Val
, _Cmp1
, _Alloc
>;
1227 _S_get_tree(_GLIBCXX_STD_C::map
<_Key
, _Val
, _Cmp2
, _Alloc
>& __map
)
1228 { return __map
._M_t
; }
1231 _S_get_tree(_GLIBCXX_STD_C::multimap
<_Key
, _Val
, _Cmp2
, _Alloc
>& __map
)
1232 { return __map
._M_t
; }
1236 _GLIBCXX_END_NAMESPACE_VERSION
1239 #endif /* _STL_MULTIMAP_H */