libstdc++
stl_set.h
Go to the documentation of this file.
1// Set implementation -*- C++ -*-
2
3// Copyright (C) 2001-2026 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_set.h
52 * This is an internal header file, included by other library headers.
53 * Do not attempt to use it directly. @headername{set}
54 */
55
56#ifndef _STL_SET_H
57#define _STL_SET_H 1
58
59#include <bits/concept_check.h>
60#if __cplusplus >= 201103L
61#include <initializer_list>
62#endif
63#if __glibcxx_containers_ranges // C++ >= 23
64# include <bits/ranges_base.h> // ranges::begin, ranges::distance etc.
65#endif
66// #include <bits/stl_tree.h> // done in std/set
67
68namespace std _GLIBCXX_VISIBILITY(default)
69{
70_GLIBCXX_BEGIN_NAMESPACE_VERSION
71_GLIBCXX_BEGIN_NAMESPACE_CONTAINER
72
73 template<typename _Key, typename _Compare, typename _Alloc>
74 class multiset;
75
76 /**
77 * @brief A standard container made up of unique keys, which can be
78 * retrieved in logarithmic time.
79 *
80 * @ingroup associative_containers
81 * @headerfile set
82 * @since C++98
83 *
84 * @tparam _Key Type of key objects.
85 * @tparam _Compare Comparison function object type, defaults to less<_Key>.
86 * @tparam _Alloc Allocator type, defaults to allocator<_Key>.
87 *
88 * Meets the requirements of a <a href="tables.html#65">container</a>, a
89 * <a href="tables.html#66">reversible container</a>, and an
90 * <a href="tables.html#69">associative container</a> (using unique keys).
91 *
92 * Sets support bidirectional iterators.
93 *
94 * The private tree data is declared exactly the same way for set and
95 * multiset; the distinction is made entirely in how the tree functions are
96 * called (*_unique versus *_equal, same as the standard).
97 */
98 template<typename _Key, typename _Compare = std::less<_Key>,
99 typename _Alloc = std::allocator<_Key> >
100 class set
101 {
102#ifdef _GLIBCXX_CONCEPT_CHECKS
103 // concept requirements
104 typedef typename _Alloc::value_type _Alloc_value_type;
105# if __cplusplus < 201103L
106 __glibcxx_class_requires(_Key, _SGIAssignableConcept)
107# endif
108 __glibcxx_class_requires4(_Compare, bool, _Key, _Key,
109 _BinaryFunctionConcept)
110 __glibcxx_class_requires2(_Key, _Alloc_value_type, _SameTypeConcept)
111#endif
112
113#if __cplusplus >= 201103L
114 static_assert(is_same<typename remove_cv<_Key>::type, _Key>::value,
115 "std::set must have a non-const, non-volatile value_type");
116# if __cplusplus > 201703L || defined __STRICT_ANSI__
118 "std::set must have the same value_type as its allocator");
119# endif
120#endif
121
122 public:
123 // typedefs:
124 ///@{
125 /// Public typedefs.
126 typedef _Key key_type;
127 typedef _Key value_type;
128 typedef _Compare key_compare;
129 typedef _Compare value_compare;
130 typedef _Alloc allocator_type;
131 ///@}
132
133 private:
135 rebind<_Key>::other _Key_alloc_type;
136
137 typedef _Rb_tree<key_type, value_type, _Identity<value_type>,
138 key_compare, _Key_alloc_type> _Rep_type;
139 _Rep_type _M_t; // Red-black tree representing set.
140
141 typedef __gnu_cxx::__alloc_traits<_Key_alloc_type> _Alloc_traits;
142
143 public:
144 ///@{
145 /// Iterator-related typedefs.
146 typedef typename _Alloc_traits::pointer pointer;
147 typedef typename _Alloc_traits::const_pointer const_pointer;
148 typedef typename _Alloc_traits::reference reference;
149 typedef typename _Alloc_traits::const_reference const_reference;
150 // _GLIBCXX_RESOLVE_LIB_DEFECTS
151 // DR 103. set::iterator is required to be modifiable,
152 // but this allows modification of keys.
153 typedef typename _Rep_type::const_iterator iterator;
154 typedef typename _Rep_type::const_iterator const_iterator;
155 typedef typename _Rep_type::const_reverse_iterator reverse_iterator;
156 typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator;
157 typedef typename _Rep_type::size_type size_type;
158 typedef typename _Rep_type::difference_type difference_type;
159 ///@}
160
161#ifdef __glibcxx_node_extract // >= C++17
162 using node_type = typename _Rep_type::node_type;
163 using insert_return_type = typename _Rep_type::insert_return_type;
164#endif
165
166 // allocation/deallocation
167 /**
168 * @brief Default constructor creates no elements.
169 */
170#if __cplusplus < 201103L
171 set() : _M_t() { }
172#else
173 set() = default;
174#endif
175
176 /**
177 * @brief Creates a %set with no elements.
178 * @param __comp Comparator to use.
179 * @param __a An allocator object.
180 */
181 explicit
182 set(const _Compare& __comp,
183 const allocator_type& __a = allocator_type())
184 : _M_t(__comp, _Key_alloc_type(__a)) { }
185
186 /**
187 * @brief Builds a %set from a range.
188 * @param __first An input iterator.
189 * @param __last An input iterator.
190 *
191 * Create a %set consisting of copies of the elements from
192 * [__first,__last). This is linear in N if the range is
193 * already sorted, and NlogN otherwise (where N is
194 * distance(__first,__last)).
195 */
196 template<typename _InputIterator>
197 set(_InputIterator __first, _InputIterator __last)
198 : _M_t()
199 { _M_t._M_insert_range_unique(__first, __last); }
200
201 /**
202 * @brief Builds a %set from a range.
203 * @param __first An input iterator.
204 * @param __last An input iterator.
205 * @param __comp A comparison functor.
206 * @param __a An allocator object.
207 *
208 * Create a %set consisting of copies of the elements from
209 * [__first,__last). This is linear in N if the range is
210 * already sorted, and NlogN otherwise (where N is
211 * distance(__first,__last)).
212 */
213 template<typename _InputIterator>
214 set(_InputIterator __first, _InputIterator __last,
215 const _Compare& __comp,
216 const allocator_type& __a = allocator_type())
217 : _M_t(__comp, _Key_alloc_type(__a))
218 { _M_t._M_insert_range_unique(__first, __last); }
219
220 /**
221 * @brief %Set copy constructor.
222 *
223 * Whether the allocator is copied depends on the allocator traits.
224 */
225#if __cplusplus < 201103L
226 set(const set& __x)
227 : _M_t(__x._M_t) { }
228#else
229 set(const set&) = default;
230
231 /**
232 * @brief %Set move constructor
233 *
234 * The newly-created %set contains the exact contents of the moved
235 * instance. The moved instance is a valid, but unspecified, %set.
236 */
237 set(set&&) = default;
238
239 /**
240 * @brief Builds a %set from an initializer_list.
241 * @param __l An initializer_list.
242 * @param __comp A comparison functor.
243 * @param __a An allocator object.
244 *
245 * Create a %set consisting of copies of the elements in the list.
246 * This is linear in N if the list is already sorted, and NlogN
247 * otherwise (where N is @a __l.size()).
248 */
250 const _Compare& __comp = _Compare(),
251 const allocator_type& __a = allocator_type())
252 : _M_t(__comp, _Key_alloc_type(__a))
253 { _M_t._M_insert_range_unique(__l.begin(), __l.end()); }
254
255 /// Allocator-extended default constructor.
256 explicit
257 set(const allocator_type& __a)
258 : _M_t(_Key_alloc_type(__a)) { }
259
260 /// Allocator-extended copy constructor.
261 set(const set& __x, const __type_identity_t<allocator_type>& __a)
262 : _M_t(__x._M_t, _Key_alloc_type(__a)) { }
263
264 /// Allocator-extended move constructor.
265 set(set&& __x, const __type_identity_t<allocator_type>& __a)
267 && _Alloc_traits::_S_always_equal())
268 : _M_t(std::move(__x._M_t), _Key_alloc_type(__a)) { }
269
270 /// Allocator-extended initialier-list constructor.
272 : _M_t(_Key_alloc_type(__a))
273 { _M_t._M_insert_range_unique(__l.begin(), __l.end()); }
274
275 /// Allocator-extended range constructor.
276 template<typename _InputIterator>
277 set(_InputIterator __first, _InputIterator __last,
278 const allocator_type& __a)
279 : _M_t(_Key_alloc_type(__a))
280 { _M_t._M_insert_range_unique(__first, __last); }
281
282#if __glibcxx_containers_ranges // C++ >= 23
283 /**
284 * @brief Builds a %set from a range.
285 * @since C++23
286 */
287 template<__detail::__container_compatible_range<_Key> _Rg>
288 set(from_range_t, _Rg&& __rg,
289 const _Compare& __comp,
290 const _Alloc& __a = _Alloc())
291 : _M_t(__comp, _Key_alloc_type(__a))
292 { insert_range(std::forward<_Rg>(__rg)); }
293
294 /// Allocator-extended range constructor.
295 template<__detail::__container_compatible_range<_Key> _Rg>
296 set(from_range_t, _Rg&& __rg, const _Alloc& __a = _Alloc())
297 : _M_t(_Key_alloc_type(__a))
298 { insert_range(std::forward<_Rg>(__rg)); }
299#endif
300
301 /**
302 * The dtor only erases the elements, and note that if the elements
303 * themselves are pointers, the pointed-to memory is not touched in any
304 * way. Managing the pointer is the user's responsibility.
305 */
306 ~set() = default;
307#endif
308
309 /**
310 * @brief %Set assignment operator.
311 *
312 * Whether the allocator is copied depends on the allocator traits.
313 */
314#if __cplusplus < 201103L
315 set&
316 operator=(const set& __x)
317 {
318 _M_t = __x._M_t;
319 return *this;
320 }
321#else
322 set&
323 operator=(const set&) = default;
324
325 /// Move assignment operator.
326 set&
327 operator=(set&&) = default;
328
329 /**
330 * @brief %Set list assignment operator.
331 * @param __l An initializer_list.
332 *
333 * This function fills a %set with copies of the elements in the
334 * initializer list @a __l.
335 *
336 * Note that the assignment completely changes the %set and
337 * that the resulting %set's size is the same as the number
338 * of elements assigned.
339 */
340 set&
342 {
343 _M_t._M_assign_unique(__l.begin(), __l.end());
344 return *this;
345 }
346#endif
347
348 // accessors:
349
350 /// Returns the comparison object with which the %set was constructed.
352 key_comp() const
353 { return _M_t.key_comp(); }
354 /// Returns the comparison object with which the %set was constructed.
357 { return _M_t.key_comp(); }
358 /// Returns the allocator object with which the %set was constructed.
359 allocator_type
360 get_allocator() const _GLIBCXX_NOEXCEPT
361 { return allocator_type(_M_t.get_allocator()); }
362
363 /**
364 * Returns a read-only (constant) iterator that points to the first
365 * element in the %set. Iteration is done in ascending order according
366 * to the keys.
367 */
369 begin() const _GLIBCXX_NOEXCEPT
370 { return _M_t.begin(); }
371
372 /**
373 * Returns a read-only (constant) iterator that points one past the last
374 * element in the %set. Iteration is done in ascending order according
375 * to the keys.
376 */
378 end() const _GLIBCXX_NOEXCEPT
379 { return _M_t.end(); }
380
381 /**
382 * Returns a read-only (constant) iterator that points to the last
383 * element in the %set. Iteration is done in descending order according
384 * to the keys.
385 */
387 rbegin() const _GLIBCXX_NOEXCEPT
388 { return _M_t.rbegin(); }
389
390 /**
391 * Returns a read-only (constant) reverse iterator that points to the
392 * last pair in the %set. Iteration is done in descending order
393 * according to the keys.
394 */
396 rend() const _GLIBCXX_NOEXCEPT
397 { return _M_t.rend(); }
398
399#if __cplusplus >= 201103L
400 /**
401 * Returns a read-only (constant) iterator that points to the first
402 * element in the %set. Iteration is done in ascending order according
403 * to the keys.
404 */
406 cbegin() const noexcept
407 { return _M_t.begin(); }
408
409 /**
410 * Returns a read-only (constant) iterator that points one past the last
411 * element in the %set. Iteration is done in ascending order according
412 * to the keys.
413 */
415 cend() const noexcept
416 { return _M_t.end(); }
417
418 /**
419 * Returns a read-only (constant) iterator that points to the last
420 * element in the %set. Iteration is done in descending order according
421 * to the keys.
422 */
424 crbegin() const noexcept
425 { return _M_t.rbegin(); }
426
427 /**
428 * Returns a read-only (constant) reverse iterator that points to the
429 * last pair in the %set. Iteration is done in descending order
430 * according to the keys.
431 */
433 crend() const noexcept
434 { return _M_t.rend(); }
435#endif
436
437 /// Returns true if the %set is empty.
438 _GLIBCXX_NODISCARD bool
439 empty() const _GLIBCXX_NOEXCEPT
440 { return _M_t.empty(); }
441
442 /// Returns the size of the %set.
444 size() const _GLIBCXX_NOEXCEPT
445 { return _M_t.size(); }
446
447 /// Returns the maximum size of the %set.
449 max_size() const _GLIBCXX_NOEXCEPT
450 { return _M_t.max_size(); }
451
452 /**
453 * @brief Swaps data with another %set.
454 * @param __x A %set of the same element and allocator types.
455 *
456 * This exchanges the elements between two sets in constant
457 * time. (It is only swapping a pointer, an integer, and an
458 * instance of the @c Compare type (which itself is often
459 * stateless and empty), so it should be quite fast.) Note
460 * that the global std::swap() function is specialized such
461 * that std::swap(s1,s2) will feed to this function.
462 *
463 * Whether the allocators are swapped depends on the allocator traits.
464 */
465 void
466 swap(set& __x)
467 _GLIBCXX_NOEXCEPT_IF(__is_nothrow_swappable<_Compare>::value)
468 { _M_t.swap(__x._M_t); }
469
470 // insert/erase
471#if __cplusplus >= 201103L
472 /**
473 * @brief Attempts to build and insert an element into the %set.
474 * @param __args Arguments used to generate an element.
475 * @return A pair, of which the first element is an iterator that points
476 * to the possibly inserted element, and the second is a bool
477 * that is true if the element was actually inserted.
478 *
479 * This function attempts to build and insert an element into the %set.
480 * A %set relies on unique keys and thus an element is only inserted if
481 * it is not already present in the %set.
482 *
483 * Insertion requires logarithmic time.
484 */
485 template<typename... _Args>
487 emplace(_Args&&... __args)
488 { return _M_t._M_emplace_unique(std::forward<_Args>(__args)...); }
489
490 /**
491 * @brief Attempts to insert an element into the %set.
492 * @param __pos An iterator that serves as a hint as to where the
493 * element should be inserted.
494 * @param __args Arguments used to generate the element to be
495 * inserted.
496 * @return An iterator that points to the element with key equivalent to
497 * the one generated from @a __args (may or may not be the
498 * element itself).
499 *
500 * This function is not concerned about whether the insertion took place,
501 * and thus does not return a boolean like the single-argument emplace()
502 * does. Note that the first parameter is only a hint and can
503 * potentially improve the performance of the insertion process. A bad
504 * hint would cause no gains in efficiency.
505 *
506 * For more on @a hinting, see:
507 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
508 *
509 * Insertion requires logarithmic time (if the hint is not taken).
510 */
511 template<typename... _Args>
513 emplace_hint(const_iterator __pos, _Args&&... __args)
514 {
515 return _M_t._M_emplace_hint_unique(__pos,
516 std::forward<_Args>(__args)...);
517 }
518#endif
519
520 /**
521 * @brief Attempts to insert an element into the %set.
522 * @param __x Element to be inserted.
523 * @return A pair, of which the first element is an iterator that points
524 * to the possibly inserted element, and the second is a bool
525 * that is true if the element was actually inserted.
526 *
527 * This function attempts to insert an element into the %set. A %set
528 * relies on unique keys and thus an element is only inserted if it is
529 * not already present in the %set.
530 *
531 * Insertion requires logarithmic time.
532 */
534 insert(const value_type& __x)
535 {
537 _M_t._M_insert_unique(__x);
538 return std::pair<iterator, bool>(__p.first, __p.second);
539 }
540
541#if __cplusplus >= 201103L
543 insert(value_type&& __x)
544 {
546 _M_t._M_insert_unique(std::move(__x));
547 return std::pair<iterator, bool>(__p.first, __p.second);
548 }
549#endif
550
551 /**
552 * @brief Attempts to insert an element into the %set.
553 * @param __position An iterator that serves as a hint as to where the
554 * element should be inserted.
555 * @param __x Element to be inserted.
556 * @return An iterator that points to the element with key of
557 * @a __x (may or may not be the element passed in).
558 *
559 * This function is not concerned about whether the insertion took place,
560 * and thus does not return a boolean like the single-argument insert()
561 * does. Note that the first parameter is only a hint and can
562 * potentially improve the performance of the insertion process. A bad
563 * hint would cause no gains in efficiency.
564 *
565 * For more on @a hinting, see:
566 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
567 *
568 * Insertion requires logarithmic time (if the hint is not taken).
569 */
570 iterator
571 insert(const_iterator __position, const value_type& __x)
572 { return _M_t._M_insert_unique_(__position, __x); }
573
574#if __cplusplus >= 201103L
576 insert(const_iterator __position, value_type&& __x)
577 { return _M_t._M_insert_unique_(__position, std::move(__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_range_unique(__first, __last); }
593
594#if __cplusplus >= 201103L
595 /**
596 * @brief Attempts to insert a list of elements into the %set.
597 * @param __l A std::initializer_list<value_type> of elements
598 * to be 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 __glibcxx_containers_ranges // C++ >= 23
608 /**
609 * @brief Inserts a range of elements.
610 * @since C++23
611 * @param __rg An input range of elements that can be converted to
612 * the set's value type.
613 */
614 template<__detail::__container_compatible_range<_Key> _Rg>
615 void
616 insert_range(_Rg&& __rg)
617 {
618 auto __first = ranges::begin(__rg);
619 const auto __last = ranges::end(__rg);
620 using _Rv = remove_cvref_t<ranges::range_reference_t<_Rg>>;
621 for (; __first != __last; ++__first)
622 if constexpr (is_same_v<_Rv, _Key>)
623 _M_t._M_insert_unique(*__first);
624 else
625 _M_t._M_emplace_unique(*__first);
626 }
627#endif
628
629#ifdef __glibcxx_node_extract // >= C++17
630 /// Extract a node.
631 node_type
632 extract(const_iterator __pos)
633 {
634 __glibcxx_assert(__pos != end());
635 return _M_t.extract(__pos);
636 }
637
638 /// Extract a node.
639 node_type
640 extract(const key_type& __x)
641 { return _M_t.extract(__x); }
642
643#ifdef __glibcxx_associative_heterogeneous_erasure // C++23
644 template <__heterogeneous_tree_key<set> _Kt>
645 node_type
646 extract(_Kt&& __key)
647 { return _M_t._M_extract_tr(__key); }
648#endif
649
650 /// Re-insert an extracted node.
651 insert_return_type
652 insert(node_type&& __nh)
653 { return _M_t._M_reinsert_node_unique(std::move(__nh)); }
654
655 /// Re-insert an extracted node.
657 insert(const_iterator __hint, node_type&& __nh)
658 { return _M_t._M_reinsert_node_hint_unique(__hint, std::move(__nh)); }
659
660 template<typename, typename>
661 friend struct std::_Rb_tree_merge_helper;
662
663 template<typename _Compare1>
664 void
665 merge(set<_Key, _Compare1, _Alloc>& __source)
666 {
667 using _Merge_helper = _Rb_tree_merge_helper<set, _Compare1>;
668 _M_t._M_merge_unique(_Merge_helper::_S_get_tree(__source));
669 }
670
671 template<typename _Compare1>
672 void
673 merge(set<_Key, _Compare1, _Alloc>&& __source)
674 { merge(__source); }
675
676 template<typename _Compare1>
677 void
678 merge(multiset<_Key, _Compare1, _Alloc>& __source)
679 {
680 using _Merge_helper = _Rb_tree_merge_helper<set, _Compare1>;
681 _M_t._M_merge_unique(_Merge_helper::_S_get_tree(__source));
682 }
683
684 template<typename _Compare1>
685 void
686 merge(multiset<_Key, _Compare1, _Alloc>&& __source)
687 { merge(__source); }
688#endif // C++17
689
690#if __cplusplus >= 201103L
691 // _GLIBCXX_RESOLVE_LIB_DEFECTS
692 // DR 130. Associative erase should return an iterator.
693 /**
694 * @brief Erases an element from a %set.
695 * @param __position An iterator pointing to the element to be erased.
696 * @return An iterator pointing to the element immediately following
697 * @a __position prior to the element being erased. If no such
698 * element exists, end() is returned.
699 *
700 * This function erases an element, pointed to by the given iterator,
701 * from a %set. Note that this function only erases the element, and
702 * that if the element is itself a pointer, the pointed-to memory is not
703 * touched in any way. Managing the pointer is the user's
704 * responsibility.
705 */
706 _GLIBCXX_ABI_TAG_CXX11
709 { return _M_t.erase(__position); }
710#else
711 /**
712 * @brief Erases an element from a %set.
713 * @param position An iterator pointing to the element to be erased.
714 *
715 * This function erases an element, pointed to by the given iterator,
716 * from a %set. Note that this function only erases the element, and
717 * that if the element is itself a pointer, the pointed-to memory is not
718 * touched in any way. Managing the pointer is the user's
719 * responsibility.
720 */
721 void
722 erase(iterator __position)
723 { _M_t.erase(__position); }
724#endif
725
726 /**
727 * @brief Erases elements according to the provided key.
728 * @param __x Key of element to be erased.
729 * @return The number of elements erased.
730 *
731 * This function erases all the elements located by the given key from
732 * a %set.
733 * Note that this function only erases the element, and that if
734 * the element is itself a pointer, the pointed-to memory is not touched
735 * in any way. Managing the pointer is the user's responsibility.
736 */
737 size_type
738 erase(const key_type& __x)
739 { return _M_t._M_erase_unique(__x); }
740
741#ifdef __glibcxx_associative_heterogeneous_erasure // C++23
742 // Note that for some types _Kt this may erase more than
743 // one element, such as if _Kt::operator< checks only part
744 // of the key.
745 template <__heterogeneous_tree_key<set> _Kt>
747 erase(_Kt&& __key)
748 { return _M_t._M_erase_tr(__key); }
749#endif
750
751#if __cplusplus >= 201103L
752 // _GLIBCXX_RESOLVE_LIB_DEFECTS
753 // DR 130. Associative erase should return an iterator.
754 /**
755 * @brief Erases a [__first,__last) range of elements from a %set.
756 * @param __first Iterator pointing to the start of the range to be
757 * erased.
758
759 * @param __last Iterator pointing to the end of the range to
760 * be erased.
761 * @return The iterator @a __last.
762 *
763 * This function erases a sequence of elements from a %set.
764 * Note that this function only erases the element, and that if
765 * the element is itself a pointer, the pointed-to memory is not touched
766 * in any way. Managing the pointer is the user's responsibility.
767 */
768 _GLIBCXX_ABI_TAG_CXX11
769 iterator
771 { return _M_t.erase(__first, __last); }
772#else
773 /**
774 * @brief Erases a [first,last) range of elements from a %set.
775 * @param __first Iterator pointing to the start of the range to be
776 * erased.
777 * @param __last Iterator pointing to the end of the range to
778 * be erased.
779 *
780 * This function erases a sequence of elements from a %set.
781 * Note that this function only erases the element, and that if
782 * the element is itself a pointer, the pointed-to memory is not touched
783 * in any way. Managing the pointer is the user's responsibility.
784 */
785 void
786 erase(iterator __first, iterator __last)
787 { _M_t.erase(__first, __last); }
788#endif
789
790 /**
791 * Erases all elements in a %set. Note that this function only erases
792 * the elements, and that if the elements themselves are pointers, the
793 * pointed-to memory is not touched in any way. Managing the pointer is
794 * the user's responsibility.
795 */
796 void
797 clear() _GLIBCXX_NOEXCEPT
798 { _M_t.clear(); }
799
800 // set operations:
801
802 ///@{
803 /**
804 * @brief Finds the number of elements.
805 * @param __x Element to located.
806 * @return Number of elements with specified key.
807 *
808 * This function only makes sense for multisets; for set the result will
809 * either be 0 (not present) or 1 (present).
810 */
812 count(const key_type& __x) const
813 { return _M_t.find(__x) == _M_t.end() ? 0 : 1; }
814
815#ifdef __glibcxx_generic_associative_lookup // C++ >= 14
816 template<typename _Kt>
817 auto
818 count(const _Kt& __x) const
819 -> decltype(_M_t._M_count_tr(__x))
820 { return _M_t._M_count_tr(__x); }
821#endif
822 ///@}
823
824#if __cplusplus > 201703L
825 ///@{
826 /**
827 * @brief Finds whether an element with the given key exists.
828 * @param __x Key of elements to be located.
829 * @return True if there is an element with the specified key.
830 */
831 bool
832 contains(const key_type& __x) const
833 { return _M_t.find(__x) != _M_t.end(); }
834
835 template<typename _Kt>
836 auto
837 contains(const _Kt& __x) const
838 -> decltype(_M_t._M_find_tr(__x), void(), true)
839 { return _M_t._M_find_tr(__x) != _M_t.end(); }
840 ///@}
841#endif
842
843 // _GLIBCXX_RESOLVE_LIB_DEFECTS
844 // 214. set::find() missing const overload
845 ///@{
846 /**
847 * @brief Tries to locate an element in a %set.
848 * @param __x Element to be located.
849 * @return Iterator pointing to sought-after element, or end() if not
850 * found.
851 *
852 * This function takes a key and tries to locate the element with which
853 * the key matches. If successful the function returns an iterator
854 * pointing to the sought after element. If unsuccessful it returns the
855 * past-the-end ( @c end() ) iterator.
856 */
858 find(const key_type& __x)
859 { return _M_t.find(__x); }
860
861 const_iterator
862 find(const key_type& __x) const
863 { return _M_t.find(__x); }
864
865#ifdef __glibcxx_generic_associative_lookup // C++ >= 14
866 template<typename _Kt>
867 auto
868 find(const _Kt& __x)
869 -> decltype(iterator{_M_t._M_find_tr(__x)})
870 { return iterator{_M_t._M_find_tr(__x)}; }
871
872 template<typename _Kt>
873 auto
874 find(const _Kt& __x) const
875 -> decltype(const_iterator{_M_t._M_find_tr(__x)})
876 { return const_iterator{_M_t._M_find_tr(__x)}; }
877#endif
878 ///@}
879
880 ///@{
881 /**
882 * @brief Finds the beginning of a subsequence matching given key.
883 * @param __x Key to be located.
884 * @return Iterator pointing to first element equal to or greater
885 * than key, or end().
886 *
887 * This function returns the first element of a subsequence of elements
888 * that matches the given key. If unsuccessful it returns an iterator
889 * pointing to the first element that has a greater value than given key
890 * or end() if no such element exists.
891 */
894 { return _M_t.lower_bound(__x); }
895
896 const_iterator
897 lower_bound(const key_type& __x) const
898 { return _M_t.lower_bound(__x); }
899
900#ifdef __glibcxx_generic_associative_lookup // C++ >= 14
901 template<typename _Kt>
902 auto
903 lower_bound(const _Kt& __x)
904 -> decltype(iterator(_M_t._M_lower_bound_tr(__x)))
905 { return iterator(_M_t._M_lower_bound_tr(__x)); }
906
907 template<typename _Kt>
908 auto
909 lower_bound(const _Kt& __x) const
910 -> decltype(const_iterator(_M_t._M_lower_bound_tr(__x)))
911 { return const_iterator(_M_t._M_lower_bound_tr(__x)); }
912#endif
913 ///@}
914
915 ///@{
916 /**
917 * @brief Finds the end of a subsequence matching given key.
918 * @param __x Key to be located.
919 * @return Iterator pointing to the first element
920 * greater than key, or end().
921 */
924 { return _M_t.upper_bound(__x); }
925
926 const_iterator
927 upper_bound(const key_type& __x) const
928 { return _M_t.upper_bound(__x); }
929
930#ifdef __glibcxx_generic_associative_lookup // C++ >= 14
931 template<typename _Kt>
932 auto
933 upper_bound(const _Kt& __x)
934 -> decltype(iterator(_M_t._M_upper_bound_tr(__x)))
935 { return iterator(_M_t._M_upper_bound_tr(__x)); }
936
937 template<typename _Kt>
938 auto
939 upper_bound(const _Kt& __x) const
940 -> decltype(const_iterator(_M_t._M_upper_bound_tr(__x)))
941 { return const_iterator(_M_t._M_upper_bound_tr(__x)); }
942#endif
943 ///@}
944
945 ///@{
946 /**
947 * @brief Finds a subsequence matching given key.
948 * @param __x Key to be located.
949 * @return Pair of iterators that possibly points to the subsequence
950 * matching given key.
951 *
952 * This function is equivalent to
953 * @code
954 * std::make_pair(c.lower_bound(val),
955 * c.upper_bound(val))
956 * @endcode
957 * (but is faster than making the calls separately).
958 *
959 * This function probably only makes sense for multisets.
960 */
963 { return _M_t.equal_range(__x); }
964
966 equal_range(const key_type& __x) const
967 { return _M_t.equal_range(__x); }
968
969#ifdef __glibcxx_generic_associative_lookup // C++ >= 14
970 template<typename _Kt>
971 auto
972 equal_range(const _Kt& __x)
973 -> decltype(pair<iterator, iterator>(_M_t._M_equal_range_tr(__x)))
974 { return pair<iterator, iterator>(_M_t._M_equal_range_tr(__x)); }
975
976 template<typename _Kt>
977 auto
978 equal_range(const _Kt& __x) const
979 -> decltype(pair<iterator, iterator>(_M_t._M_equal_range_tr(__x)))
980 { return pair<iterator, iterator>(_M_t._M_equal_range_tr(__x)); }
981#endif
982 ///@}
983
984 template<typename _K1, typename _C1, typename _A1>
985 friend bool
986 operator==(const set<_K1, _C1, _A1>&, const set<_K1, _C1, _A1>&);
987
988#if __cpp_lib_three_way_comparison
989 template<typename _K1, typename _C1, typename _A1>
990 friend __detail::__synth3way_t<_K1>
991 operator<=>(const set<_K1, _C1, _A1>&, const set<_K1, _C1, _A1>&);
992#else
993 template<typename _K1, typename _C1, typename _A1>
994 friend bool
995 operator<(const set<_K1, _C1, _A1>&, const set<_K1, _C1, _A1>&);
996#endif
997 };
998
999#if __cpp_deduction_guides >= 201606
1000
1001 template<typename _InputIterator,
1002 typename _Compare =
1004 typename _Allocator =
1006 typename = _RequireInputIter<_InputIterator>,
1007 typename = _RequireNotAllocator<_Compare>,
1008 typename = _RequireAllocator<_Allocator>>
1009 set(_InputIterator, _InputIterator,
1010 _Compare = _Compare(), _Allocator = _Allocator())
1012 _Compare, _Allocator>;
1013
1014 template<typename _Key, typename _Compare = less<_Key>,
1015 typename _Allocator = allocator<_Key>,
1016 typename = _RequireNotAllocator<_Compare>,
1017 typename = _RequireAllocator<_Allocator>>
1019 _Compare = _Compare(), _Allocator = _Allocator())
1021
1022 template<typename _InputIterator, typename _Allocator,
1023 typename = _RequireInputIter<_InputIterator>,
1024 typename = _RequireAllocator<_Allocator>>
1025 set(_InputIterator, _InputIterator, _Allocator)
1028 _Allocator>;
1029
1030 template<typename _Key, typename _Allocator,
1031 typename = _RequireAllocator<_Allocator>>
1032 set(initializer_list<_Key>, _Allocator)
1033 -> set<_Key, less<_Key>, _Allocator>;
1034
1035#if __glibcxx_containers_ranges // C++ >= 23
1036 template<ranges::input_range _Rg,
1037 __not_allocator_like _Compare = less<ranges::range_value_t<_Rg>>,
1038 __allocator_like _Alloc = std::allocator<ranges::range_value_t<_Rg>>>
1039 set(from_range_t, _Rg&&, _Compare = _Compare(), _Alloc = _Alloc())
1040 -> set<ranges::range_value_t<_Rg>, _Compare, _Alloc>;
1041
1042 template<ranges::input_range _Rg, __allocator_like _Alloc>
1043 set(from_range_t, _Rg&&, _Alloc)
1045#endif
1046#endif // deduction guides
1047
1048 /**
1049 * @brief Set equality comparison.
1050 * @param __x A %set.
1051 * @param __y A %set of the same type as @a x.
1052 * @return True iff the size and elements of the sets are equal.
1053 *
1054 * This is an equivalence relation. It is linear in the size of the sets.
1055 * Sets are considered equivalent if their sizes are equal, and if
1056 * corresponding elements compare equal.
1057 */
1058 template<typename _Key, typename _Compare, typename _Alloc>
1059 inline bool
1060 operator==(const set<_Key, _Compare, _Alloc>& __x,
1061 const set<_Key, _Compare, _Alloc>& __y)
1062 { return __x._M_t == __y._M_t; }
1063
1064#if __cpp_lib_three_way_comparison
1065 /**
1066 * @brief Set ordering relation.
1067 * @param __x A `set`.
1068 * @param __y A `set` of the same type as `x`.
1069 * @return A value indicating whether `__x` is less than, equal to,
1070 * greater than, or incomparable with `__y`.
1071 *
1072 * This is a total ordering relation. It is linear in the size of the
1073 * maps. The elements must be comparable with @c <.
1074 *
1075 * See `std::lexicographical_compare_three_way()` for how the determination
1076 * is made. This operator is used to synthesize relational operators like
1077 * `<` and `>=` etc.
1078 */
1079 template<typename _Key, typename _Compare, typename _Alloc>
1080 inline __detail::__synth3way_t<_Key>
1081 operator<=>(const set<_Key, _Compare, _Alloc>& __x,
1082 const set<_Key, _Compare, _Alloc>& __y)
1083 { return __x._M_t <=> __y._M_t; }
1084#else
1085 /**
1086 * @brief Set ordering relation.
1087 * @param __x A %set.
1088 * @param __y A %set of the same type as @a x.
1089 * @return True iff @a __x is lexicographically less than @a __y.
1090 *
1091 * This is a total ordering relation. It is linear in the size of the
1092 * sets. The elements must be comparable with @c <.
1093 *
1094 * See std::lexicographical_compare() for how the determination is made.
1095 */
1096 template<typename _Key, typename _Compare, typename _Alloc>
1097 inline bool
1098 operator<(const set<_Key, _Compare, _Alloc>& __x,
1099 const set<_Key, _Compare, _Alloc>& __y)
1100 { return __x._M_t < __y._M_t; }
1101
1102 /// Returns !(x == y).
1103 template<typename _Key, typename _Compare, typename _Alloc>
1104 inline bool
1105 operator!=(const set<_Key, _Compare, _Alloc>& __x,
1106 const set<_Key, _Compare, _Alloc>& __y)
1107 { return !(__x == __y); }
1108
1109 /// Returns y < x.
1110 template<typename _Key, typename _Compare, typename _Alloc>
1111 inline bool
1112 operator>(const set<_Key, _Compare, _Alloc>& __x,
1113 const set<_Key, _Compare, _Alloc>& __y)
1114 { return __y < __x; }
1115
1116 /// Returns !(y < x)
1117 template<typename _Key, typename _Compare, typename _Alloc>
1118 inline bool
1119 operator<=(const set<_Key, _Compare, _Alloc>& __x,
1120 const set<_Key, _Compare, _Alloc>& __y)
1121 { return !(__y < __x); }
1122
1123 /// Returns !(x < y)
1124 template<typename _Key, typename _Compare, typename _Alloc>
1125 inline bool
1126 operator>=(const set<_Key, _Compare, _Alloc>& __x,
1127 const set<_Key, _Compare, _Alloc>& __y)
1128 { return !(__x < __y); }
1129#endif // three-way comparison
1130
1131 /// See std::set::swap().
1132 template<typename _Key, typename _Compare, typename _Alloc>
1133 inline void
1135 _GLIBCXX_NOEXCEPT_IF(noexcept(__x.swap(__y)))
1136 { __x.swap(__y); }
1137
1138_GLIBCXX_END_NAMESPACE_CONTAINER
1139
1140#ifdef __glibcxx_node_extract // >= C++17 && HOSTED
1141 // Allow std::set access to internals of compatible sets.
1142 template<typename _Val, typename _Cmp1, typename _Alloc, typename _Cmp2>
1143 struct
1144 _Rb_tree_merge_helper<_GLIBCXX_STD_C::set<_Val, _Cmp1, _Alloc>, _Cmp2>
1145 {
1146 private:
1147 friend class _GLIBCXX_STD_C::set<_Val, _Cmp1, _Alloc>;
1148
1149 static auto&
1150 _S_get_tree(_GLIBCXX_STD_C::set<_Val, _Cmp2, _Alloc>& __set)
1151 { return __set._M_t; }
1152
1153 static auto&
1154 _S_get_tree(_GLIBCXX_STD_C::multiset<_Val, _Cmp2, _Alloc>& __set)
1155 { return __set._M_t; }
1156 };
1157#endif // C++17
1158
1159_GLIBCXX_END_NAMESPACE_VERSION
1160} //namespace std
1161#endif /* _STL_SET_H */
pair(_T1, _T2) -> pair< _T1, _T2 >
Two pairs are equal iff their members are equal.
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition move.h:138
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
Definition move.h:72
ISO C++ entities toplevel namespace is std.
initializer_list
is_nothrow_copy_constructible
Definition type_traits:1334
The standard allocator, as per C++03 [20.4.1].
Definition allocator.h:134
One of the comparison functors.
Struct holding two objects of arbitrary type.
Definition stl_pair.h:304
_T1 first
The first member.
Definition stl_pair.h:308
_T2 second
The second member.
Definition stl_pair.h:309
Common iterator class.
A standard container made up of unique keys, which can be retrieved in logarithmic time.
Definition stl_set.h:101
set(set &&__x, const __type_identity_t< allocator_type > &__a) noexcept(is_nothrow_copy_constructible< _Compare >::value &&_Alloc_traits::_S_always_equal())
Allocator-extended move constructor.
Definition stl_set.h:265
bool contains(const key_type &__x) const
Finds whether an element with the given key exists.
Definition stl_set.h:832
set(_InputIterator __first, _InputIterator __last, const _Compare &__comp, const allocator_type &__a=allocator_type())
Builds a set from a range.
Definition stl_set.h:214
size_type count(const key_type &__x) const
Finds the number of elements.
Definition stl_set.h:812
set & operator=(initializer_list< value_type > __l)
Set list assignment operator.
Definition stl_set.h:341
_GLIBCXX_ABI_TAG_CXX11 iterator erase(const_iterator __position)
Erases an element from a set.
Definition stl_set.h:708
set(set &&)=default
Set move constructor
void swap(set &__x) noexcept(/*conditional */)
Swaps data with another set.
Definition stl_set.h:466
value_compare value_comp() const
Returns the comparison object with which the set was constructed.
Definition stl_set.h:356
iterator cbegin() const noexcept
Definition stl_set.h:406
polymorphic_allocator< _Key > allocator_type
Definition stl_set.h:130
auto contains(const _Kt &__x) const -> decltype(_M_t._M_find_tr(__x), void(), true)
Finds whether an element with the given key exists.
Definition stl_set.h:837
std::pair< const_iterator, const_iterator > equal_range(const key_type &__x) const
Finds a subsequence matching given key.
Definition stl_set.h:966
const_iterator upper_bound(const key_type &__x) const
Finds the end of a subsequence matching given key.
Definition stl_set.h:927
void insert(initializer_list< value_type > __l)
Attempts to insert a list of elements into the set.
Definition stl_set.h:603
set(_InputIterator __first, _InputIterator __last)
Builds a set from a range.
Definition stl_set.h:197
iterator cend() const noexcept
Definition stl_set.h:415
iterator end() const noexcept
Definition stl_set.h:378
size_type max_size() const noexcept
Returns the maximum size of the set.
Definition stl_set.h:449
std::pair< iterator, bool > insert(const value_type &__x)
Attempts to insert an element into the set.
Definition stl_set.h:534
std::pair< iterator, iterator > equal_range(const key_type &__x)
Finds a subsequence matching given key.
Definition stl_set.h:962
_Alloc_traits::const_reference const_reference
Definition stl_set.h:149
set()=default
Default constructor creates no elements.
set(const allocator_type &__a)
Allocator-extended default constructor.
Definition stl_set.h:257
std::pair< iterator, bool > emplace(_Args &&... __args)
Attempts to build and insert an element into the set.
Definition stl_set.h:487
key_compare key_comp() const
Returns the comparison object with which the set was constructed.
Definition stl_set.h:352
reverse_iterator rbegin() const noexcept
Definition stl_set.h:387
void insert(_InputIterator __first, _InputIterator __last)
A template function that attempts to insert a range of elements.
Definition stl_set.h:591
_GLIBCXX_ABI_TAG_CXX11 iterator erase(const_iterator __first, const_iterator __last)
Erases a [__first,__last) range of elements from a set.
Definition stl_set.h:770
reverse_iterator crbegin() const noexcept
Definition stl_set.h:424
set(initializer_list< value_type > __l, const allocator_type &__a)
Allocator-extended initialier-list constructor.
Definition stl_set.h:271
size_type size() const noexcept
Returns the size of the set.
Definition stl_set.h:444
_Rep_type::const_reverse_iterator const_reverse_iterator
Definition stl_set.h:156
_Rep_type::const_reverse_iterator reverse_iterator
Definition stl_set.h:155
reverse_iterator crend() const noexcept
Definition stl_set.h:433
iterator insert(const_iterator __position, const value_type &__x)
Attempts to insert an element into the set.
Definition stl_set.h:571
const_iterator lower_bound(const key_type &__x) const
Finds the beginning of a subsequence matching given key.
Definition stl_set.h:897
set(_InputIterator __first, _InputIterator __last, const allocator_type &__a)
Allocator-extended range constructor.
Definition stl_set.h:277
set(const set &__x, const __type_identity_t< allocator_type > &__a)
Allocator-extended copy constructor.
Definition stl_set.h:261
set(initializer_list< value_type > __l, const _Compare &__comp=_Compare(), const allocator_type &__a=allocator_type())
Builds a set from an initializer_list.
Definition stl_set.h:249
void clear() noexcept
Definition stl_set.h:797
allocator_type get_allocator() const noexcept
Returns the allocator object with which the set was constructed.
Definition stl_set.h:360
set(const set &)=default
Set copy constructor.
iterator upper_bound(const key_type &__x)
Finds the end of a subsequence matching given key.
Definition stl_set.h:923
iterator lower_bound(const key_type &__x)
Finds the beginning of a subsequence matching given key.
Definition stl_set.h:893
iterator emplace_hint(const_iterator __pos, _Args &&... __args)
Attempts to insert an element into the set.
Definition stl_set.h:513
set & operator=(const set &)=default
Set assignment operator.
iterator begin() const noexcept
Definition stl_set.h:369
set(const _Compare &__comp, const allocator_type &__a=allocator_type())
Creates a set with no elements.
Definition stl_set.h:182
~set()=default
iterator find(const key_type &__x)
Tries to locate an element in a set.
Definition stl_set.h:858
set & operator=(set &&)=default
Move assignment operator.
bool empty() const noexcept
Returns true if the set is empty.
Definition stl_set.h:439
size_type erase(const key_type &__x)
Erases elements according to the provided key.
Definition stl_set.h:738
const_iterator find(const key_type &__x) const
Tries to locate an element in a set.
Definition stl_set.h:862
reverse_iterator rend() const noexcept
Definition stl_set.h:396
A standard container made up of elements, which can be retrieved in logarithmic time.
Uniform interface to C++98 and C++11 allocators.
A range for which ranges::begin returns an input iterator.