1 // Multiset 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.
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_multiset.h
52 * This is an internal header file, included by other library headers.
53 * Do not attempt to use it directly. @headername{set}
56 #ifndef _STL_MULTISET_H
57 #define _STL_MULTISET_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 _Compare
, typename _Alloc
>
73 * @brief A standard container made up of elements, which can be retrieved
74 * in logarithmic time.
76 * @ingroup associative_containers
80 * @tparam _Key Type of key objects.
81 * @tparam _Compare Comparison function object type, defaults to less<_Key>.
82 * @tparam _Alloc Allocator type, defaults to allocator<_Key>.
84 * Meets the requirements of a <a href="tables.html#65">container</a>, a
85 * <a href="tables.html#66">reversible container</a>, and an
86 * <a href="tables.html#69">associative container</a> (using equivalent
87 * keys). For a @c multiset<Key> the key_type and value_type are Key.
89 * Multisets support bidirectional iterators.
91 * The private tree data is declared exactly the same way for set and
92 * multiset; the distinction is made entirely in how the tree functions are
93 * called (*_unique versus *_equal, same as the standard).
95 template <typename _Key
, typename _Compare
= std::less
<_Key
>,
96 typename _Alloc
= std::allocator
<_Key
> >
99 #ifdef _GLIBCXX_CONCEPT_CHECKS
100 // concept requirements
101 typedef typename
_Alloc::value_type _Alloc_value_type
;
102 # if __cplusplus < 201103L
103 __glibcxx_class_requires(_Key
, _SGIAssignableConcept
)
105 __glibcxx_class_requires4(_Compare
, bool, _Key
, _Key
,
106 _BinaryFunctionConcept
)
107 __glibcxx_class_requires2(_Key
, _Alloc_value_type
, _SameTypeConcept
)
110 #if __cplusplus >= 201103L
111 static_assert(is_same
<typename remove_cv
<_Key
>::type
, _Key
>::value
,
112 "std::multiset must have a non-const, non-volatile value_type");
113 # if __cplusplus > 201703L || defined __STRICT_ANSI__
114 static_assert(is_same
<typename
_Alloc::value_type
, _Key
>::value
,
115 "std::multiset must have the same value_type as its allocator");
121 typedef _Key key_type
;
122 typedef _Key value_type
;
123 typedef _Compare key_compare
;
124 typedef _Compare value_compare
;
125 typedef _Alloc allocator_type
;
128 /// This turns a red-black tree into a [multi]set.
129 typedef typename
__gnu_cxx::__alloc_traits
<_Alloc
>::template
130 rebind
<_Key
>::other _Key_alloc_type
;
132 typedef _Rb_tree
<key_type
, value_type
, _Identity
<value_type
>,
133 key_compare
, _Key_alloc_type
> _Rep_type
;
134 /// The actual tree structure.
137 typedef __gnu_cxx::__alloc_traits
<_Key_alloc_type
> _Alloc_traits
;
140 typedef typename
_Alloc_traits::pointer pointer
;
141 typedef typename
_Alloc_traits::const_pointer const_pointer
;
142 typedef typename
_Alloc_traits::reference reference
;
143 typedef typename
_Alloc_traits::const_reference const_reference
;
144 // _GLIBCXX_RESOLVE_LIB_DEFECTS
145 // DR 103. set::iterator is required to be modifiable,
146 // but this allows modification of keys.
147 typedef typename
_Rep_type::const_iterator iterator
;
148 typedef typename
_Rep_type::const_iterator const_iterator
;
149 typedef typename
_Rep_type::const_reverse_iterator reverse_iterator
;
150 typedef typename
_Rep_type::const_reverse_iterator const_reverse_iterator
;
151 typedef typename
_Rep_type::size_type size_type
;
152 typedef typename
_Rep_type::difference_type difference_type
;
154 #if __cplusplus > 201402L
155 using node_type
= typename
_Rep_type::node_type
;
158 // allocation/deallocation
160 * @brief Default constructor creates no elements.
162 #if __cplusplus < 201103L
163 multiset() : _M_t() { }
165 multiset() = default;
169 * @brief Creates a %multiset with no elements.
170 * @param __comp Comparator to use.
171 * @param __a An allocator object.
174 multiset(const _Compare
& __comp
,
175 const allocator_type
& __a
= allocator_type())
176 : _M_t(__comp
, _Key_alloc_type(__a
)) { }
179 * @brief Builds a %multiset from a range.
180 * @param __first An input iterator.
181 * @param __last An input iterator.
183 * Create a %multiset consisting of copies of the elements from
184 * [first,last). This is linear in N if the range is already sorted,
185 * and NlogN otherwise (where N is distance(__first,__last)).
187 template<typename _InputIterator
>
188 multiset(_InputIterator __first
, _InputIterator __last
)
190 { _M_t
._M_insert_range_equal(__first
, __last
); }
193 * @brief Builds a %multiset from a range.
194 * @param __first An input iterator.
195 * @param __last An input iterator.
196 * @param __comp A comparison functor.
197 * @param __a An allocator object.
199 * Create a %multiset consisting of copies of the elements from
200 * [__first,__last). This is linear in N if the range is already sorted,
201 * and NlogN otherwise (where N is distance(__first,__last)).
203 template<typename _InputIterator
>
204 multiset(_InputIterator __first
, _InputIterator __last
,
205 const _Compare
& __comp
,
206 const allocator_type
& __a
= allocator_type())
207 : _M_t(__comp
, _Key_alloc_type(__a
))
208 { _M_t
._M_insert_range_equal(__first
, __last
); }
211 * @brief %Multiset copy constructor.
213 * Whether the allocator is copied depends on the allocator traits.
215 #if __cplusplus < 201103L
216 multiset(const multiset
& __x
)
219 multiset(const multiset
&) = default;
222 * @brief %Multiset move constructor.
224 * The newly-created %multiset contains the exact contents of the
225 * moved instance. The moved instance is a valid, but unspecified
228 multiset(multiset
&&) = default;
231 * @brief Builds a %multiset from an initializer_list.
232 * @param __l An initializer_list.
233 * @param __comp A comparison functor.
234 * @param __a An allocator object.
236 * Create a %multiset consisting of copies of the elements from
237 * the list. This is linear in N if the list is already sorted,
238 * and NlogN otherwise (where N is @a __l.size()).
240 multiset(initializer_list
<value_type
> __l
,
241 const _Compare
& __comp
= _Compare(),
242 const allocator_type
& __a
= allocator_type())
243 : _M_t(__comp
, _Key_alloc_type(__a
))
244 { _M_t
._M_insert_range_equal(__l
.begin(), __l
.end()); }
246 /// Allocator-extended default constructor.
248 multiset(const allocator_type
& __a
)
249 : _M_t(_Key_alloc_type(__a
)) { }
251 /// Allocator-extended copy constructor.
252 multiset(const multiset
& __m
,
253 const __type_identity_t
<allocator_type
>& __a
)
254 : _M_t(__m
._M_t
, _Key_alloc_type(__a
)) { }
256 /// Allocator-extended move constructor.
257 multiset(multiset
&& __m
, const __type_identity_t
<allocator_type
>& __a
)
258 noexcept(is_nothrow_copy_constructible
<_Compare
>::value
259 && _Alloc_traits::_S_always_equal())
260 : _M_t(std::move(__m
._M_t
), _Key_alloc_type(__a
)) { }
262 /// Allocator-extended initialier-list constructor.
263 multiset(initializer_list
<value_type
> __l
, const allocator_type
& __a
)
264 : _M_t(_Key_alloc_type(__a
))
265 { _M_t
._M_insert_range_equal(__l
.begin(), __l
.end()); }
267 /// Allocator-extended range constructor.
268 template<typename _InputIterator
>
269 multiset(_InputIterator __first
, _InputIterator __last
,
270 const allocator_type
& __a
)
271 : _M_t(_Key_alloc_type(__a
))
272 { _M_t
._M_insert_range_equal(__first
, __last
); }
275 * The dtor only erases the elements, and note that if the elements
276 * themselves are pointers, the pointed-to memory is not touched in any
277 * way. Managing the pointer is the user's responsibility.
279 ~multiset() = default;
283 * @brief %Multiset assignment operator.
285 * Whether the allocator is copied depends on the allocator traits.
287 #if __cplusplus < 201103L
289 operator=(const multiset
& __x
)
296 operator=(const multiset
&) = default;
298 /// Move assignment operator.
300 operator=(multiset
&&) = default;
303 * @brief %Multiset list assignment operator.
304 * @param __l An initializer_list.
306 * This function fills a %multiset with copies of the elements in the
307 * initializer list @a __l.
309 * Note that the assignment completely changes the %multiset and
310 * that the resulting %multiset's size is the same as the number
311 * of elements assigned.
314 operator=(initializer_list
<value_type
> __l
)
316 _M_t
._M_assign_equal(__l
.begin(), __l
.end());
323 /// Returns the comparison object.
326 { return _M_t
.key_comp(); }
327 /// Returns the comparison object.
330 { return _M_t
.key_comp(); }
331 /// Returns the memory allocation object.
333 get_allocator() const _GLIBCXX_NOEXCEPT
334 { return allocator_type(_M_t
.get_allocator()); }
337 * Returns a read-only (constant) iterator that points to the first
338 * element in the %multiset. Iteration is done in ascending order
339 * according to the keys.
342 begin() const _GLIBCXX_NOEXCEPT
343 { return _M_t
.begin(); }
346 * Returns a read-only (constant) iterator that points one past the last
347 * element in the %multiset. Iteration is done in ascending order
348 * according to the keys.
351 end() const _GLIBCXX_NOEXCEPT
352 { return _M_t
.end(); }
355 * Returns a read-only (constant) reverse iterator that points to the
356 * last element in the %multiset. Iteration is done in descending order
357 * according to the keys.
360 rbegin() const _GLIBCXX_NOEXCEPT
361 { return _M_t
.rbegin(); }
364 * Returns a read-only (constant) reverse iterator that points to the
365 * last element in the %multiset. Iteration is done in descending order
366 * according to the keys.
369 rend() const _GLIBCXX_NOEXCEPT
370 { return _M_t
.rend(); }
372 #if __cplusplus >= 201103L
374 * Returns a read-only (constant) iterator that points to the first
375 * element in the %multiset. Iteration is done in ascending order
376 * according to the keys.
379 cbegin() const noexcept
380 { return _M_t
.begin(); }
383 * Returns a read-only (constant) iterator that points one past the last
384 * element in the %multiset. Iteration is done in ascending order
385 * according to the keys.
388 cend() const noexcept
389 { return _M_t
.end(); }
392 * Returns a read-only (constant) reverse iterator that points to the
393 * last element in the %multiset. Iteration is done in descending order
394 * according to the keys.
397 crbegin() const noexcept
398 { return _M_t
.rbegin(); }
401 * Returns a read-only (constant) reverse iterator that points to the
402 * last element in the %multiset. Iteration is done in descending order
403 * according to the keys.
406 crend() const noexcept
407 { return _M_t
.rend(); }
410 /// Returns true if the %set is empty.
411 _GLIBCXX_NODISCARD
bool
412 empty() const _GLIBCXX_NOEXCEPT
413 { return _M_t
.empty(); }
415 /// Returns the size of the %set.
417 size() const _GLIBCXX_NOEXCEPT
418 { return _M_t
.size(); }
420 /// Returns the maximum size of the %set.
422 max_size() const _GLIBCXX_NOEXCEPT
423 { return _M_t
.max_size(); }
426 * @brief Swaps data with another %multiset.
427 * @param __x A %multiset of the same element and allocator types.
429 * This exchanges the elements between two multisets in constant time.
430 * (It is only swapping a pointer, an integer, and an instance of the @c
431 * Compare type (which itself is often stateless and empty), so it should
433 * Note that the global std::swap() function is specialized such that
434 * std::swap(s1,s2) will feed to this function.
436 * Whether the allocators are swapped depends on the allocator traits.
440 _GLIBCXX_NOEXCEPT_IF(__is_nothrow_swappable
<_Compare
>::value
)
441 { _M_t
.swap(__x
._M_t
); }
444 #if __cplusplus >= 201103L
446 * @brief Builds and inserts an element into the %multiset.
447 * @param __args Arguments used to generate the element instance to be
449 * @return An iterator that points to the inserted element.
451 * This function inserts an element into the %multiset. Contrary
452 * to a std::set the %multiset does not rely on unique keys and thus
453 * multiple copies of the same element can be inserted.
455 * Insertion requires logarithmic time.
457 template<typename
... _Args
>
459 emplace(_Args
&&... __args
)
460 { return _M_t
._M_emplace_equal(std::forward
<_Args
>(__args
)...); }
463 * @brief Builds and inserts an element into the %multiset.
464 * @param __pos An iterator that serves as a hint as to where the
465 * element should be inserted.
466 * @param __args Arguments used to generate the element instance to be
468 * @return An iterator that points to the inserted element.
470 * This function inserts an element into the %multiset. Contrary
471 * to a std::set the %multiset does not rely on unique keys and thus
472 * multiple copies of the same element can be inserted.
474 * Note that the first parameter is only a hint and can potentially
475 * improve the performance of the insertion process. A bad hint would
476 * cause no gains in efficiency.
478 * See https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
479 * for more on @a hinting.
481 * Insertion requires logarithmic time (if the hint is not taken).
483 template<typename
... _Args
>
485 emplace_hint(const_iterator __pos
, _Args
&&... __args
)
487 return _M_t
._M_emplace_hint_equal(__pos
,
488 std::forward
<_Args
>(__args
)...);
493 * @brief Inserts an element into the %multiset.
494 * @param __x Element to be inserted.
495 * @return An iterator that points to the inserted element.
497 * This function inserts an element into the %multiset. Contrary
498 * to a std::set the %multiset does not rely on unique keys and thus
499 * multiple copies of the same element can be inserted.
501 * Insertion requires logarithmic time.
504 insert(const value_type
& __x
)
505 { return _M_t
._M_insert_equal(__x
); }
507 #if __cplusplus >= 201103L
509 insert(value_type
&& __x
)
510 { return _M_t
._M_insert_equal(std::move(__x
)); }
514 * @brief Inserts an element into the %multiset.
515 * @param __position An iterator that serves as a hint as to where the
516 * element should be inserted.
517 * @param __x Element to be inserted.
518 * @return An iterator that points to the inserted element.
520 * This function inserts an element into the %multiset. Contrary
521 * to a std::set the %multiset does not rely on unique keys and thus
522 * multiple copies of the same element can be inserted.
524 * Note that the first parameter is only a hint and can potentially
525 * improve the performance of the insertion process. A bad hint would
526 * cause no gains in efficiency.
528 * See https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
529 * for more on @a hinting.
531 * Insertion requires logarithmic time (if the hint is not taken).
534 insert(const_iterator __position
, const value_type
& __x
)
535 { return _M_t
._M_insert_equal_(__position
, __x
); }
537 #if __cplusplus >= 201103L
539 insert(const_iterator __position
, value_type
&& __x
)
540 { return _M_t
._M_insert_equal_(__position
, std::move(__x
)); }
544 * @brief A template function that tries to insert a range of elements.
545 * @param __first Iterator pointing to the start of the range to be
547 * @param __last Iterator pointing to the end of the range.
549 * Complexity similar to that of the range constructor.
551 template<typename _InputIterator
>
553 insert(_InputIterator __first
, _InputIterator __last
)
554 { _M_t
._M_insert_range_equal(__first
, __last
); }
556 #if __cplusplus >= 201103L
558 * @brief Attempts to insert a list of elements into the %multiset.
559 * @param __l A std::initializer_list<value_type> of elements
562 * Complexity similar to that of the range constructor.
565 insert(initializer_list
<value_type
> __l
)
566 { this->insert(__l
.begin(), __l
.end()); }
569 #if __cplusplus > 201402L
572 extract(const_iterator __pos
)
574 __glibcxx_assert(__pos
!= end());
575 return _M_t
.extract(__pos
);
580 extract(const key_type
& __x
)
581 { return _M_t
.extract(__x
); }
583 /// Re-insert an extracted node.
585 insert(node_type
&& __nh
)
586 { return _M_t
._M_reinsert_node_equal(std::move(__nh
)); }
588 /// Re-insert an extracted node.
590 insert(const_iterator __hint
, node_type
&& __nh
)
591 { return _M_t
._M_reinsert_node_hint_equal(__hint
, std::move(__nh
)); }
593 template<typename
, typename
>
594 friend struct std::_Rb_tree_merge_helper
;
596 template<typename _Compare1
>
598 merge(multiset
<_Key
, _Compare1
, _Alloc
>& __source
)
600 using _Merge_helper
= _Rb_tree_merge_helper
<multiset
, _Compare1
>;
601 _M_t
._M_merge_equal(_Merge_helper::_S_get_tree(__source
));
604 template<typename _Compare1
>
606 merge(multiset
<_Key
, _Compare1
, _Alloc
>&& __source
)
609 template<typename _Compare1
>
611 merge(set
<_Key
, _Compare1
, _Alloc
>& __source
)
613 using _Merge_helper
= _Rb_tree_merge_helper
<multiset
, _Compare1
>;
614 _M_t
._M_merge_equal(_Merge_helper::_S_get_tree(__source
));
617 template<typename _Compare1
>
619 merge(set
<_Key
, _Compare1
, _Alloc
>&& __source
)
623 #if __cplusplus >= 201103L
624 // _GLIBCXX_RESOLVE_LIB_DEFECTS
625 // DR 130. Associative erase should return an iterator.
627 * @brief Erases an element from a %multiset.
628 * @param __position An iterator pointing to the element to be erased.
629 * @return An iterator pointing to the element immediately following
630 * @a position prior to the element being erased. If no such
631 * element exists, end() is returned.
633 * This function erases an element, pointed to by the given iterator,
634 * from a %multiset. Note that this function only erases the element,
635 * and that if the element is itself a pointer, the pointed-to memory is
636 * not touched in any way. Managing the pointer is the user's
639 _GLIBCXX_ABI_TAG_CXX11
641 erase(const_iterator __position
)
642 { return _M_t
.erase(__position
); }
645 * @brief Erases an element from a %multiset.
646 * @param __position An iterator pointing to the element to be erased.
648 * This function erases an element, pointed to by the given iterator,
649 * from a %multiset. Note that this function only erases the element,
650 * and that if the element is itself a pointer, the pointed-to memory is
651 * not touched in any way. Managing the pointer is the user's
655 erase(iterator __position
)
656 { _M_t
.erase(__position
); }
660 * @brief Erases elements according to the provided key.
661 * @param __x Key of element to be erased.
662 * @return The number of elements erased.
664 * This function erases all elements located by the given key from a
666 * Note that this function only erases the element, and that if
667 * the element is itself a pointer, the pointed-to memory is not touched
668 * in any way. Managing the pointer is the user's responsibility.
671 erase(const key_type
& __x
)
672 { return _M_t
.erase(__x
); }
674 #if __cplusplus >= 201103L
675 // _GLIBCXX_RESOLVE_LIB_DEFECTS
676 // DR 130. Associative erase should return an iterator.
678 * @brief Erases a [first,last) range of elements from a %multiset.
679 * @param __first Iterator pointing to the start of the range to be
681 * @param __last Iterator pointing to the end of the range to
683 * @return The iterator @a last.
685 * This function erases a sequence of elements from a %multiset.
686 * Note that this function only erases the elements, and that if
687 * the elements themselves are pointers, the pointed-to memory is not
688 * touched in any way. Managing the pointer is the user's
691 _GLIBCXX_ABI_TAG_CXX11
693 erase(const_iterator __first
, const_iterator __last
)
694 { return _M_t
.erase(__first
, __last
); }
697 * @brief Erases a [first,last) range of elements from a %multiset.
698 * @param first Iterator pointing to the start of the range to be
700 * @param last Iterator pointing to the end of the range to be erased.
702 * This function erases a sequence of elements from a %multiset.
703 * Note that this function only erases the elements, and that if
704 * the elements themselves are pointers, the pointed-to memory is not
705 * touched in any way. Managing the pointer is the user's
709 erase(iterator __first
, iterator __last
)
710 { _M_t
.erase(__first
, __last
); }
714 * Erases all elements in a %multiset. 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.
720 clear() _GLIBCXX_NOEXCEPT
723 // multiset operations:
727 * @brief Finds the number of elements with given key.
728 * @param __x Key of elements to be located.
729 * @return Number of elements with specified key.
732 count(const key_type
& __x
) const
733 { return _M_t
.count(__x
); }
735 #if __cplusplus > 201103L
736 template<typename _Kt
>
738 count(const _Kt
& __x
) const -> decltype(_M_t
._M_count_tr(__x
))
739 { return _M_t
._M_count_tr(__x
); }
743 #if __cplusplus > 201703L
746 * @brief Finds whether an element with the given key exists.
747 * @param __x Key of elements to be located.
748 * @return True if there is any element with the specified key.
751 contains(const key_type
& __x
) const
752 { return _M_t
.find(__x
) != _M_t
.end(); }
754 template<typename _Kt
>
756 contains(const _Kt
& __x
) const
757 -> decltype(_M_t
._M_find_tr(__x
), void(), true)
758 { return _M_t
._M_find_tr(__x
) != _M_t
.end(); }
762 // _GLIBCXX_RESOLVE_LIB_DEFECTS
763 // 214. set::find() missing const overload
766 * @brief Tries to locate an element in a %set.
767 * @param __x Element to be located.
768 * @return Iterator pointing to sought-after element, or end() if not
771 * This function takes a key and tries to locate the element with which
772 * the key matches. If successful the function returns an iterator
773 * pointing to the sought after element. If unsuccessful it returns the
774 * past-the-end ( @c end() ) iterator.
777 find(const key_type
& __x
)
778 { return _M_t
.find(__x
); }
781 find(const key_type
& __x
) const
782 { return _M_t
.find(__x
); }
784 #if __cplusplus > 201103L
785 template<typename _Kt
>
788 -> decltype(iterator
{_M_t
._M_find_tr(__x
)})
789 { return iterator
{_M_t
._M_find_tr(__x
)}; }
791 template<typename _Kt
>
793 find(const _Kt
& __x
) const
794 -> decltype(const_iterator
{_M_t
._M_find_tr(__x
)})
795 { return const_iterator
{_M_t
._M_find_tr(__x
)}; }
801 * @brief Finds the beginning of a subsequence matching given key.
802 * @param __x Key to be located.
803 * @return Iterator pointing to first element equal to or greater
804 * than key, or end().
806 * This function returns the first element of a subsequence of elements
807 * that matches the given key. If unsuccessful it returns an iterator
808 * pointing to the first element that has a greater value than given key
809 * or end() if no such element exists.
812 lower_bound(const key_type
& __x
)
813 { return _M_t
.lower_bound(__x
); }
816 lower_bound(const key_type
& __x
) const
817 { return _M_t
.lower_bound(__x
); }
819 #if __cplusplus > 201103L
820 template<typename _Kt
>
822 lower_bound(const _Kt
& __x
)
823 -> decltype(iterator(_M_t
._M_lower_bound_tr(__x
)))
824 { return iterator(_M_t
._M_lower_bound_tr(__x
)); }
826 template<typename _Kt
>
828 lower_bound(const _Kt
& __x
) const
829 -> decltype(iterator(_M_t
._M_lower_bound_tr(__x
)))
830 { return iterator(_M_t
._M_lower_bound_tr(__x
)); }
836 * @brief Finds the end of a subsequence matching given key.
837 * @param __x Key to be located.
838 * @return Iterator pointing to the first element
839 * greater than key, or end().
842 upper_bound(const key_type
& __x
)
843 { return _M_t
.upper_bound(__x
); }
846 upper_bound(const key_type
& __x
) const
847 { return _M_t
.upper_bound(__x
); }
849 #if __cplusplus > 201103L
850 template<typename _Kt
>
852 upper_bound(const _Kt
& __x
)
853 -> decltype(iterator(_M_t
._M_upper_bound_tr(__x
)))
854 { return iterator(_M_t
._M_upper_bound_tr(__x
)); }
856 template<typename _Kt
>
858 upper_bound(const _Kt
& __x
) const
859 -> decltype(iterator(_M_t
._M_upper_bound_tr(__x
)))
860 { return iterator(_M_t
._M_upper_bound_tr(__x
)); }
866 * @brief Finds a subsequence matching given key.
867 * @param __x Key to be located.
868 * @return Pair of iterators that possibly points to the subsequence
869 * matching given key.
871 * This function is equivalent to
873 * std::make_pair(c.lower_bound(val),
874 * c.upper_bound(val))
876 * (but is faster than making the calls separately).
878 * This function probably only makes sense for multisets.
880 std::pair
<iterator
, iterator
>
881 equal_range(const key_type
& __x
)
882 { return _M_t
.equal_range(__x
); }
884 std::pair
<const_iterator
, const_iterator
>
885 equal_range(const key_type
& __x
) const
886 { return _M_t
.equal_range(__x
); }
888 #if __cplusplus > 201103L
889 template<typename _Kt
>
891 equal_range(const _Kt
& __x
)
892 -> decltype(pair
<iterator
, iterator
>(_M_t
._M_equal_range_tr(__x
)))
893 { return pair
<iterator
, iterator
>(_M_t
._M_equal_range_tr(__x
)); }
895 template<typename _Kt
>
897 equal_range(const _Kt
& __x
) const
898 -> decltype(pair
<iterator
, iterator
>(_M_t
._M_equal_range_tr(__x
)))
899 { return pair
<iterator
, iterator
>(_M_t
._M_equal_range_tr(__x
)); }
903 template<typename _K1
, typename _C1
, typename _A1
>
905 operator==(const multiset
<_K1
, _C1
, _A1
>&,
906 const multiset
<_K1
, _C1
, _A1
>&);
908 #if __cpp_lib_three_way_comparison
909 template<typename _K1
, typename _C1
, typename _A1
>
910 friend __detail::__synth3way_t
<_K1
>
911 operator<=>(const multiset
<_K1
, _C1
, _A1
>&,
912 const multiset
<_K1
, _C1
, _A1
>&);
914 template<typename _K1
, typename _C1
, typename _A1
>
916 operator< (const multiset
<_K1
, _C1
, _A1
>&,
917 const multiset
<_K1
, _C1
, _A1
>&);
921 #if __cpp_deduction_guides >= 201606
923 template<typename _InputIterator
,
925 less
<typename iterator_traits
<_InputIterator
>::value_type
>,
926 typename _Allocator
=
927 allocator
<typename iterator_traits
<_InputIterator
>::value_type
>,
928 typename
= _RequireInputIter
<_InputIterator
>,
929 typename
= _RequireNotAllocator
<_Compare
>,
930 typename
= _RequireAllocator
<_Allocator
>>
931 multiset(_InputIterator
, _InputIterator
,
932 _Compare
= _Compare(), _Allocator
= _Allocator())
933 -> multiset
<typename iterator_traits
<_InputIterator
>::value_type
,
934 _Compare
, _Allocator
>;
936 template<typename _Key
,
937 typename _Compare
= less
<_Key
>,
938 typename _Allocator
= allocator
<_Key
>,
939 typename
= _RequireNotAllocator
<_Compare
>,
940 typename
= _RequireAllocator
<_Allocator
>>
941 multiset(initializer_list
<_Key
>,
942 _Compare
= _Compare(), _Allocator
= _Allocator())
943 -> multiset
<_Key
, _Compare
, _Allocator
>;
945 template<typename _InputIterator
, typename _Allocator
,
946 typename
= _RequireInputIter
<_InputIterator
>,
947 typename
= _RequireAllocator
<_Allocator
>>
948 multiset(_InputIterator
, _InputIterator
, _Allocator
)
949 -> multiset
<typename iterator_traits
<_InputIterator
>::value_type
,
950 less
<typename iterator_traits
<_InputIterator
>::value_type
>,
953 template<typename _Key
, typename _Allocator
,
954 typename
= _RequireAllocator
<_Allocator
>>
955 multiset(initializer_list
<_Key
>, _Allocator
)
956 -> multiset
<_Key
, less
<_Key
>, _Allocator
>;
958 #endif // deduction guides
961 * @brief Multiset equality comparison.
962 * @param __x A %multiset.
963 * @param __y A %multiset of the same type as @a __x.
964 * @return True iff the size and elements of the multisets are equal.
966 * This is an equivalence relation. It is linear in the size of the
968 * Multisets are considered equivalent if their sizes are equal, and if
969 * corresponding elements compare equal.
971 template<typename _Key
, typename _Compare
, typename _Alloc
>
973 operator==(const multiset
<_Key
, _Compare
, _Alloc
>& __x
,
974 const multiset
<_Key
, _Compare
, _Alloc
>& __y
)
975 { return __x
._M_t
== __y
._M_t
; }
977 #if __cpp_lib_three_way_comparison
979 * @brief Multiset ordering relation.
980 * @param __x A `multiset`.
981 * @param __y A `multiset` of the same type as `x`.
982 * @return A value indicating whether `__x` is less than, equal to,
983 * greater than, or incomparable with `__y`.
985 * This is a total ordering relation. It is linear in the size of the
986 * maps. The elements must be comparable with @c <.
988 * See `std::lexicographical_compare_three_way()` for how the determination
989 * is made. This operator is used to synthesize relational operators like
992 template<typename _Key
, typename _Compare
, typename _Alloc
>
993 inline __detail::__synth3way_t
<_Key
>
994 operator<=>(const multiset
<_Key
, _Compare
, _Alloc
>& __x
,
995 const multiset
<_Key
, _Compare
, _Alloc
>& __y
)
996 { return __x
._M_t
<=> __y
._M_t
; }
999 * @brief Multiset ordering relation.
1000 * @param __x A %multiset.
1001 * @param __y A %multiset of the same type as @a __x.
1002 * @return True iff @a __x is lexicographically less than @a __y.
1004 * This is a total ordering relation. It is linear in the size of the
1005 * sets. The elements must be comparable with @c <.
1007 * See std::lexicographical_compare() for how the determination is made.
1009 template<typename _Key
, typename _Compare
, typename _Alloc
>
1011 operator<(const multiset
<_Key
, _Compare
, _Alloc
>& __x
,
1012 const multiset
<_Key
, _Compare
, _Alloc
>& __y
)
1013 { return __x
._M_t
< __y
._M_t
; }
1015 /// Returns !(x == y).
1016 template<typename _Key
, typename _Compare
, typename _Alloc
>
1018 operator!=(const multiset
<_Key
, _Compare
, _Alloc
>& __x
,
1019 const multiset
<_Key
, _Compare
, _Alloc
>& __y
)
1020 { return !(__x
== __y
); }
1023 template<typename _Key
, typename _Compare
, typename _Alloc
>
1025 operator>(const multiset
<_Key
,_Compare
,_Alloc
>& __x
,
1026 const multiset
<_Key
,_Compare
,_Alloc
>& __y
)
1027 { return __y
< __x
; }
1029 /// Returns !(y < x)
1030 template<typename _Key
, typename _Compare
, typename _Alloc
>
1032 operator<=(const multiset
<_Key
, _Compare
, _Alloc
>& __x
,
1033 const multiset
<_Key
, _Compare
, _Alloc
>& __y
)
1034 { return !(__y
< __x
); }
1036 /// Returns !(x < y)
1037 template<typename _Key
, typename _Compare
, typename _Alloc
>
1039 operator>=(const multiset
<_Key
, _Compare
, _Alloc
>& __x
,
1040 const multiset
<_Key
, _Compare
, _Alloc
>& __y
)
1041 { return !(__x
< __y
); }
1042 #endif // three-way comparison
1044 /// See std::multiset::swap().
1045 template<typename _Key
, typename _Compare
, typename _Alloc
>
1047 swap(multiset
<_Key
, _Compare
, _Alloc
>& __x
,
1048 multiset
<_Key
, _Compare
, _Alloc
>& __y
)
1049 _GLIBCXX_NOEXCEPT_IF(noexcept(__x
.swap(__y
)))
1052 _GLIBCXX_END_NAMESPACE_CONTAINER
1054 #if __cplusplus > 201402L
1055 // Allow std::multiset access to internals of compatible sets.
1056 template<typename _Val
, typename _Cmp1
, typename _Alloc
, typename _Cmp2
>
1058 _Rb_tree_merge_helper
<_GLIBCXX_STD_C::multiset
<_Val
, _Cmp1
, _Alloc
>,
1062 friend class _GLIBCXX_STD_C::multiset
<_Val
, _Cmp1
, _Alloc
>;
1065 _S_get_tree(_GLIBCXX_STD_C::set
<_Val
, _Cmp2
, _Alloc
>& __set
)
1066 { return __set
._M_t
; }
1069 _S_get_tree(_GLIBCXX_STD_C::multiset
<_Val
, _Cmp2
, _Alloc
>& __set
)
1070 { return __set
._M_t
; }
1074 _GLIBCXX_END_NAMESPACE_VERSION
1077 #endif /* _STL_MULTISET_H */