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