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#ifdef __glibcxx_associative_heterogeneous_insertion // C++26
552 template <__heterogeneous_tree_key<set> _Kt>
554 insert(_Kt&& __k)
555 {
556 auto [__left, __node] =_M_t._M_get_insert_unique_pos_tr(__k);
557 if (__node)
558 {
559 iterator __i = _M_t._M_emplace_here(
560 (__left == __node), __node, std::forward<_Kt>(__k));
561 return { __i, true };
562 }
563 return { iterator(__left), false };
564 }
565#endif
566
567 /**
568 * @brief Attempts to insert an element into the %set.
569 * @param __position An iterator that serves as a hint as to where the
570 * element should be inserted.
571 * @param __x Element to be inserted.
572 * @return An iterator that points to the element with key of
573 * @a __x (may or may not be the element passed in).
574 *
575 * This function is not concerned about whether the insertion took place,
576 * and thus does not return a boolean like the single-argument insert()
577 * does. Note that the first parameter is only a hint and can
578 * potentially improve the performance of the insertion process. A bad
579 * hint would cause no gains in efficiency.
580 *
581 * For more on @a hinting, see:
582 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
583 *
584 * If a heterogeneous key __k matches a range of elements, an iterator
585 * to the first is returned.
586 *
587 * Insertion requires logarithmic time (if the hint is not taken).
588 */
590 insert(const_iterator __position, const value_type& __x)
591 { return _M_t._M_insert_unique_(__position, __x); }
592
593#if __cplusplus >= 201103L
595 insert(const_iterator __position, value_type&& __x)
596 { return _M_t._M_insert_unique_(__position, std::move(__x)); }
597#endif
598
599#ifdef __glibcxx_associative_heterogeneous_insertion // C++26
600 template <__heterogeneous_tree_key<set> _Kt>
601 iterator
602 insert(const_iterator __position, _Kt&& __k)
603 {
604 auto [__left, __node] =
605 _M_t._M_get_insert_hint_unique_pos_tr(__position, __k);
606 if (__node)
607 return _M_t._M_emplace_here(
608 (__left == __node), __node, std::forward<_Kt>(__k));
609 else
610 return iterator(__left);
611 }
612#endif
613 ///@}
614
615 /**
616 * @brief A template function that attempts to insert a range
617 * of elements.
618 * @param __first Iterator pointing to the start of the range to be
619 * inserted.
620 * @param __last Iterator pointing to the end of the range.
621 *
622 * Complexity similar to that of the range constructor.
623 */
624 template<typename _InputIterator>
625 void
626 insert(_InputIterator __first, _InputIterator __last)
627 { _M_t._M_insert_range_unique(__first, __last); }
628
629#if __cplusplus >= 201103L
630 /**
631 * @brief Attempts to insert a list of elements into the %set.
632 * @param __l A std::initializer_list<value_type> of elements
633 * to be inserted.
634 *
635 * Complexity similar to that of the range constructor.
636 */
637 void
639 { this->insert(__l.begin(), __l.end()); }
640#endif
641
642#if __glibcxx_containers_ranges // C++ >= 23
643 /**
644 * @brief Inserts a range of elements.
645 * @since C++23
646 * @param __rg An input range of elements that can be converted to
647 * the set's value type.
648 */
649 template<__detail::__container_compatible_range<_Key> _Rg>
650 void
651 insert_range(_Rg&& __rg)
652 {
653 auto __first = ranges::begin(__rg);
654 const auto __last = ranges::end(__rg);
655 using _Rv = remove_cvref_t<ranges::range_reference_t<_Rg>>;
656 for (; __first != __last; ++__first)
657 if constexpr (is_same_v<_Rv, _Key>)
658 _M_t._M_insert_unique(*__first);
659 else
660 _M_t._M_emplace_unique(*__first);
661 }
662#endif
663
664#ifdef __glibcxx_node_extract // >= C++17
665 /// Extract a node.
666 node_type
667 extract(const_iterator __pos)
668 {
669 __glibcxx_assert(__pos != end());
670 return _M_t.extract(__pos);
671 }
672
673 /// Extract a node.
674 node_type
675 extract(const key_type& __x)
676 { return _M_t.extract(__x); }
677
678#ifdef __glibcxx_associative_heterogeneous_erasure // C++23
679 template <__heterogeneous_tree_key<set> _Kt>
680 node_type
681 extract(_Kt&& __key)
682 { return _M_t._M_extract_tr(__key); }
683#endif
684
685 /// Re-insert an extracted node.
686 insert_return_type
687 insert(node_type&& __nh)
688 { return _M_t._M_reinsert_node_unique(std::move(__nh)); }
689
690 /// Re-insert an extracted node.
692 insert(const_iterator __hint, node_type&& __nh)
693 { return _M_t._M_reinsert_node_hint_unique(__hint, std::move(__nh)); }
694
695 template<typename, typename>
696 friend struct std::_Rb_tree_merge_helper;
697
698 template<typename _Compare1>
699 void
700 merge(set<_Key, _Compare1, _Alloc>& __source)
701 {
702 using _Merge_helper = _Rb_tree_merge_helper<set, _Compare1>;
703 _M_t._M_merge_unique(_Merge_helper::_S_get_tree(__source));
704 }
705
706 template<typename _Compare1>
707 void
708 merge(set<_Key, _Compare1, _Alloc>&& __source)
709 { merge(__source); }
710
711 template<typename _Compare1>
712 void
713 merge(multiset<_Key, _Compare1, _Alloc>& __source)
714 {
715 using _Merge_helper = _Rb_tree_merge_helper<set, _Compare1>;
716 _M_t._M_merge_unique(_Merge_helper::_S_get_tree(__source));
717 }
718
719 template<typename _Compare1>
720 void
721 merge(multiset<_Key, _Compare1, _Alloc>&& __source)
722 { merge(__source); }
723#endif // C++17
724
725#if __cplusplus >= 201103L
726 // _GLIBCXX_RESOLVE_LIB_DEFECTS
727 // DR 130. Associative erase should return an iterator.
728 /**
729 * @brief Erases an element from a %set.
730 * @param __position An iterator pointing to the element to be erased.
731 * @return An iterator pointing to the element immediately following
732 * @a __position prior to the element being erased. If no such
733 * element exists, end() is returned.
734 *
735 * This function erases an element, pointed to by the given iterator,
736 * from a %set. Note that this function only erases the element, and
737 * that if the element is itself a pointer, the pointed-to memory is not
738 * touched in any way. Managing the pointer is the user's
739 * responsibility.
740 */
741 _GLIBCXX_ABI_TAG_CXX11
744 { return _M_t.erase(__position); }
745#else
746 /**
747 * @brief Erases an element from a %set.
748 * @param position An iterator pointing to the element to be erased.
749 *
750 * This function erases an element, pointed to by the given iterator,
751 * from a %set. Note that this function only erases the element, and
752 * that if the element is itself a pointer, the pointed-to memory is not
753 * touched in any way. Managing the pointer is the user's
754 * responsibility.
755 */
756 void
757 erase(iterator __position)
758 { _M_t.erase(__position); }
759#endif
760
761 /**
762 * @brief Erases elements according to the provided key.
763 * @param __x Key of element to be erased.
764 * @return The number of elements erased.
765 *
766 * This function erases all the elements located by the given key from
767 * a %set.
768 * Note that this function only erases the element, and that if
769 * the element is itself a pointer, the pointed-to memory is not touched
770 * in any way. Managing the pointer is the user's responsibility.
771 */
772 size_type
773 erase(const key_type& __x)
774 { return _M_t._M_erase_unique(__x); }
775
776#ifdef __glibcxx_associative_heterogeneous_erasure // C++23
777 // Note that for some types _Kt this may erase more than
778 // one element, such as if _Kt::operator< checks only part
779 // of the key.
780 template <__heterogeneous_tree_key<set> _Kt>
782 erase(_Kt&& __key)
783 { return _M_t._M_erase_tr(__key); }
784#endif
785
786#if __cplusplus >= 201103L
787 // _GLIBCXX_RESOLVE_LIB_DEFECTS
788 // DR 130. Associative erase should return an iterator.
789 /**
790 * @brief Erases a [__first,__last) range of elements from a %set.
791 * @param __first Iterator pointing to the start of the range to be
792 * erased.
793
794 * @param __last Iterator pointing to the end of the range to
795 * be erased.
796 * @return The iterator @a __last.
797 *
798 * This function erases a sequence of elements from a %set.
799 * Note that this function only erases the element, and that if
800 * the element is itself a pointer, the pointed-to memory is not touched
801 * in any way. Managing the pointer is the user's responsibility.
802 */
803 _GLIBCXX_ABI_TAG_CXX11
804 iterator
806 { return _M_t.erase(__first, __last); }
807#else
808 /**
809 * @brief Erases a [first,last) range of elements from a %set.
810 * @param __first Iterator pointing to the start of the range to be
811 * erased.
812 * @param __last Iterator pointing to the end of the range to
813 * be erased.
814 *
815 * This function erases a sequence of elements from a %set.
816 * Note that this function only erases the element, and that if
817 * the element is itself a pointer, the pointed-to memory is not touched
818 * in any way. Managing the pointer is the user's responsibility.
819 */
820 void
821 erase(iterator __first, iterator __last)
822 { _M_t.erase(__first, __last); }
823#endif
824
825 /**
826 * Erases all elements in a %set. Note that this function only erases
827 * the elements, and that if the elements themselves are pointers, the
828 * pointed-to memory is not touched in any way. Managing the pointer is
829 * the user's responsibility.
830 */
831 void
832 clear() _GLIBCXX_NOEXCEPT
833 { _M_t.clear(); }
834
835 // set operations:
836
837 ///@{
838 /**
839 * @brief Finds the number of elements.
840 * @param __x Element to located.
841 * @return Number of elements with specified key.
842 *
843 * This function only makes sense for multisets; for set the result will
844 * either be 0 (not present) or 1 (present).
845 */
847 count(const key_type& __x) const
848 { return _M_t.find(__x) == _M_t.end() ? 0 : 1; }
849
850#ifdef __glibcxx_generic_associative_lookup // C++ >= 14
851 template<typename _Kt>
852 auto
853 count(const _Kt& __x) const
854 -> decltype(_M_t._M_count_tr(__x))
855 { return _M_t._M_count_tr(__x); }
856#endif
857 ///@}
858
859#if __cplusplus > 201703L
860 ///@{
861 /**
862 * @brief Finds whether an element with the given key exists.
863 * @param __x Key of elements to be located.
864 * @return True if there is an element with the specified key.
865 */
866 bool
867 contains(const key_type& __x) const
868 { return _M_t.find(__x) != _M_t.end(); }
869
870 template<typename _Kt>
871 auto
872 contains(const _Kt& __x) const
873 -> decltype(_M_t._M_find_tr(__x), void(), true)
874 { return _M_t._M_find_tr(__x) != _M_t.end(); }
875 ///@}
876#endif
877
878 // _GLIBCXX_RESOLVE_LIB_DEFECTS
879 // 214. set::find() missing const overload
880 ///@{
881 /**
882 * @brief Tries to locate an element in a %set.
883 * @param __x Element to be located.
884 * @return Iterator pointing to sought-after element, or end() if not
885 * found.
886 *
887 * This function takes a key and tries to locate the element with which
888 * the key matches. If successful the function returns an iterator
889 * pointing to the sought after element. If unsuccessful it returns the
890 * past-the-end ( @c end() ) iterator.
891 */
893 find(const key_type& __x)
894 { return _M_t.find(__x); }
895
896 const_iterator
897 find(const key_type& __x) const
898 { return _M_t.find(__x); }
899
900#ifdef __glibcxx_generic_associative_lookup // C++ >= 14
901 template<typename _Kt>
902 auto
903 find(const _Kt& __x)
904 -> decltype(iterator{_M_t._M_find_tr(__x)})
905 { return iterator{_M_t._M_find_tr(__x)}; }
906
907 template<typename _Kt>
908 auto
909 find(const _Kt& __x) const
910 -> decltype(const_iterator{_M_t._M_find_tr(__x)})
911 { return const_iterator{_M_t._M_find_tr(__x)}; }
912#endif
913 ///@}
914
915 ///@{
916 /**
917 * @brief Finds the beginning of a subsequence matching given key.
918 * @param __x Key to be located.
919 * @return Iterator pointing to first element equal to or greater
920 * than key, or end().
921 *
922 * This function returns the first element of a subsequence of elements
923 * that matches the given key. If unsuccessful it returns an iterator
924 * pointing to the first element that has a greater value than given key
925 * or end() if no such element exists.
926 */
929 { return _M_t.lower_bound(__x); }
930
931 const_iterator
932 lower_bound(const key_type& __x) const
933 { return _M_t.lower_bound(__x); }
934
935#ifdef __glibcxx_generic_associative_lookup // C++ >= 14
936 template<typename _Kt>
937 auto
938 lower_bound(const _Kt& __x)
939 -> decltype(iterator(_M_t._M_lower_bound_tr(__x)))
940 { return iterator(_M_t._M_lower_bound_tr(__x)); }
941
942 template<typename _Kt>
943 auto
944 lower_bound(const _Kt& __x) const
945 -> decltype(const_iterator(_M_t._M_lower_bound_tr(__x)))
946 { return const_iterator(_M_t._M_lower_bound_tr(__x)); }
947#endif
948 ///@}
949
950 ///@{
951 /**
952 * @brief Finds the end of a subsequence matching given key.
953 * @param __x Key to be located.
954 * @return Iterator pointing to the first element
955 * greater than key, or end().
956 */
959 { return _M_t.upper_bound(__x); }
960
961 const_iterator
962 upper_bound(const key_type& __x) const
963 { return _M_t.upper_bound(__x); }
964
965#ifdef __glibcxx_generic_associative_lookup // C++ >= 14
966 template<typename _Kt>
967 auto
968 upper_bound(const _Kt& __x)
969 -> decltype(iterator(_M_t._M_upper_bound_tr(__x)))
970 { return iterator(_M_t._M_upper_bound_tr(__x)); }
971
972 template<typename _Kt>
973 auto
974 upper_bound(const _Kt& __x) const
975 -> decltype(const_iterator(_M_t._M_upper_bound_tr(__x)))
976 { return const_iterator(_M_t._M_upper_bound_tr(__x)); }
977#endif
978 ///@}
979
980 ///@{
981 /**
982 * @brief Finds a subsequence matching given key.
983 * @param __x Key to be located.
984 * @return Pair of iterators that possibly points to the subsequence
985 * matching given key.
986 *
987 * This function is equivalent to
988 * @code
989 * std::make_pair(c.lower_bound(val),
990 * c.upper_bound(val))
991 * @endcode
992 * (but is faster than making the calls separately).
993 *
994 * This function probably only makes sense for multisets.
995 */
998 { return _M_t.equal_range(__x); }
999
1001 equal_range(const key_type& __x) const
1002 { return _M_t.equal_range(__x); }
1003
1004#ifdef __glibcxx_generic_associative_lookup // C++ >= 14
1005 template<typename _Kt>
1006 auto
1007 equal_range(const _Kt& __x)
1008 -> decltype(pair<iterator, iterator>(_M_t._M_equal_range_tr(__x)))
1009 { return pair<iterator, iterator>(_M_t._M_equal_range_tr(__x)); }
1010
1011 template<typename _Kt>
1012 auto
1013 equal_range(const _Kt& __x) const
1014 -> decltype(pair<iterator, iterator>(_M_t._M_equal_range_tr(__x)))
1015 { return pair<iterator, iterator>(_M_t._M_equal_range_tr(__x)); }
1016#endif
1017 ///@}
1018
1019 template<typename _K1, typename _C1, typename _A1>
1020 friend bool
1021 operator==(const set<_K1, _C1, _A1>&, const set<_K1, _C1, _A1>&);
1022
1023#if __cpp_lib_three_way_comparison
1024 template<typename _K1, typename _C1, typename _A1>
1025 friend __detail::__synth3way_t<_K1>
1026 operator<=>(const set<_K1, _C1, _A1>&, const set<_K1, _C1, _A1>&);
1027#else
1028 template<typename _K1, typename _C1, typename _A1>
1029 friend bool
1030 operator<(const set<_K1, _C1, _A1>&, const set<_K1, _C1, _A1>&);
1031#endif
1032 };
1033
1034#if __cpp_deduction_guides >= 201606
1035
1036 template<typename _InputIterator,
1037 typename _Compare =
1039 typename _Allocator =
1041 typename = _RequireInputIter<_InputIterator>,
1042 typename = _RequireNotAllocator<_Compare>,
1043 typename = _RequireAllocator<_Allocator>>
1044 set(_InputIterator, _InputIterator,
1045 _Compare = _Compare(), _Allocator = _Allocator())
1047 _Compare, _Allocator>;
1048
1049 template<typename _Key, typename _Compare = less<_Key>,
1050 typename _Allocator = allocator<_Key>,
1051 typename = _RequireNotAllocator<_Compare>,
1052 typename = _RequireAllocator<_Allocator>>
1054 _Compare = _Compare(), _Allocator = _Allocator())
1056
1057 template<typename _InputIterator, typename _Allocator,
1058 typename = _RequireInputIter<_InputIterator>,
1059 typename = _RequireAllocator<_Allocator>>
1060 set(_InputIterator, _InputIterator, _Allocator)
1063 _Allocator>;
1064
1065 template<typename _Key, typename _Allocator,
1066 typename = _RequireAllocator<_Allocator>>
1067 set(initializer_list<_Key>, _Allocator)
1068 -> set<_Key, less<_Key>, _Allocator>;
1069
1070#if __glibcxx_containers_ranges // C++ >= 23
1071 template<ranges::input_range _Rg,
1072 __not_allocator_like _Compare = less<ranges::range_value_t<_Rg>>,
1073 __allocator_like _Alloc = std::allocator<ranges::range_value_t<_Rg>>>
1074 set(from_range_t, _Rg&&, _Compare = _Compare(), _Alloc = _Alloc())
1075 -> set<ranges::range_value_t<_Rg>, _Compare, _Alloc>;
1076
1077 template<ranges::input_range _Rg, __allocator_like _Alloc>
1078 set(from_range_t, _Rg&&, _Alloc)
1080#endif
1081#endif // deduction guides
1082
1083 /**
1084 * @brief Set equality comparison.
1085 * @param __x A %set.
1086 * @param __y A %set of the same type as @a x.
1087 * @return True iff the size and elements of the sets are equal.
1088 *
1089 * This is an equivalence relation. It is linear in the size of the sets.
1090 * Sets are considered equivalent if their sizes are equal, and if
1091 * corresponding elements compare equal.
1092 */
1093 template<typename _Key, typename _Compare, typename _Alloc>
1094 inline bool
1095 operator==(const set<_Key, _Compare, _Alloc>& __x,
1096 const set<_Key, _Compare, _Alloc>& __y)
1097 { return __x._M_t == __y._M_t; }
1098
1099#if __cpp_lib_three_way_comparison
1100 /**
1101 * @brief Set ordering relation.
1102 * @param __x A `set`.
1103 * @param __y A `set` of the same type as `x`.
1104 * @return A value indicating whether `__x` is less than, equal to,
1105 * greater than, or incomparable with `__y`.
1106 *
1107 * This is a total ordering relation. It is linear in the size of the
1108 * maps. The elements must be comparable with @c <.
1109 *
1110 * See `std::lexicographical_compare_three_way()` for how the determination
1111 * is made. This operator is used to synthesize relational operators like
1112 * `<` and `>=` etc.
1113 */
1114 template<typename _Key, typename _Compare, typename _Alloc>
1115 inline __detail::__synth3way_t<_Key>
1116 operator<=>(const set<_Key, _Compare, _Alloc>& __x,
1117 const set<_Key, _Compare, _Alloc>& __y)
1118 { return __x._M_t <=> __y._M_t; }
1119#else
1120 /**
1121 * @brief Set ordering relation.
1122 * @param __x A %set.
1123 * @param __y A %set of the same type as @a x.
1124 * @return True iff @a __x is lexicographically less than @a __y.
1125 *
1126 * This is a total ordering relation. It is linear in the size of the
1127 * sets. The elements must be comparable with @c <.
1128 *
1129 * See std::lexicographical_compare() for how the determination is made.
1130 */
1131 template<typename _Key, typename _Compare, typename _Alloc>
1132 inline bool
1133 operator<(const set<_Key, _Compare, _Alloc>& __x,
1134 const set<_Key, _Compare, _Alloc>& __y)
1135 { return __x._M_t < __y._M_t; }
1136
1137 /// Returns !(x == y).
1138 template<typename _Key, typename _Compare, typename _Alloc>
1139 inline bool
1140 operator!=(const set<_Key, _Compare, _Alloc>& __x,
1141 const set<_Key, _Compare, _Alloc>& __y)
1142 { return !(__x == __y); }
1143
1144 /// Returns y < x.
1145 template<typename _Key, typename _Compare, typename _Alloc>
1146 inline bool
1147 operator>(const set<_Key, _Compare, _Alloc>& __x,
1148 const set<_Key, _Compare, _Alloc>& __y)
1149 { return __y < __x; }
1150
1151 /// Returns !(y < x)
1152 template<typename _Key, typename _Compare, typename _Alloc>
1153 inline bool
1154 operator<=(const set<_Key, _Compare, _Alloc>& __x,
1155 const set<_Key, _Compare, _Alloc>& __y)
1156 { return !(__y < __x); }
1157
1158 /// Returns !(x < y)
1159 template<typename _Key, typename _Compare, typename _Alloc>
1160 inline bool
1161 operator>=(const set<_Key, _Compare, _Alloc>& __x,
1162 const set<_Key, _Compare, _Alloc>& __y)
1163 { return !(__x < __y); }
1164#endif // three-way comparison
1165
1166 /// See std::set::swap().
1167 template<typename _Key, typename _Compare, typename _Alloc>
1168 inline void
1170 _GLIBCXX_NOEXCEPT_IF(noexcept(__x.swap(__y)))
1171 { __x.swap(__y); }
1172
1173_GLIBCXX_END_NAMESPACE_CONTAINER
1174
1175#ifdef __glibcxx_node_extract // >= C++17 && HOSTED
1176 // Allow std::set access to internals of compatible sets.
1177 template<typename _Val, typename _Cmp1, typename _Alloc, typename _Cmp2>
1178 struct
1179 _Rb_tree_merge_helper<_GLIBCXX_STD_C::set<_Val, _Cmp1, _Alloc>, _Cmp2>
1180 {
1181 private:
1182 friend class _GLIBCXX_STD_C::set<_Val, _Cmp1, _Alloc>;
1183
1184 static auto&
1185 _S_get_tree(_GLIBCXX_STD_C::set<_Val, _Cmp2, _Alloc>& __set)
1186 { return __set._M_t; }
1187
1188 static auto&
1189 _S_get_tree(_GLIBCXX_STD_C::multiset<_Val, _Cmp2, _Alloc>& __set)
1190 { return __set._M_t; }
1191 };
1192#endif // C++17
1193
1194_GLIBCXX_END_NAMESPACE_VERSION
1195} //namespace std
1196#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:867
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:847
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:743
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:872
std::pair< const_iterator, const_iterator > equal_range(const key_type &__x) const
Finds a subsequence matching given key.
Definition stl_set.h:1001
const_iterator upper_bound(const key_type &__x) const
Finds the end of a subsequence matching given key.
Definition stl_set.h:962
void insert(initializer_list< value_type > __l)
Attempts to insert a list of elements into the set.
Definition stl_set.h:638
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:997
_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:626
_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:805
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:590
const_iterator lower_bound(const key_type &__x) const
Finds the beginning of a subsequence matching given key.
Definition stl_set.h:932
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:832
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:958
iterator lower_bound(const key_type &__x)
Finds the beginning of a subsequence matching given key.
Definition stl_set.h:928
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:893
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:773
const_iterator find(const key_type &__x) const
Tries to locate an element in a set.
Definition stl_set.h:897
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.