STD::SET(3)

πŸ’– NAME πŸ’–

okay so, welcome to the manual page for std::set< ..., _Alloc >(3) from the Library Functions Manual πŸ’… and ngl bestie, she's iconic. The actual full name is std::set< _Key, _Compare, _Alloc > and she is a standard container made up of unique keys, which can be retrieved in logarithmic time ✨ no cap, that's the whole flex.


🌸 SYNOPSIS 🌸

#include <set>

πŸŽ€ Public Types

These are the public typedefs 🫢 (the little name tags every set carries around):

  • typedef _Key key_type β€” public typedef, it's literally just your _Key πŸ’œ
  • typedef _Key value_type β€” public typedef. yes bestie, it's _Key again. same girl, different name.
  • typedef _Compare key_compare β€” public typedef, your _Compare.
  • typedef _Compare value_compare β€” public typedef, also your _Compare.
  • typedef _Alloc allocator_type β€” public typedef, your _Alloc 😌

and then the iterator-related typedefs πŸ¦‹ (the ones that make the iteration girlies thrive):

  • typedef _Alloc_traits::pointer pointer β€” iterator-related typedef
  • typedef _Alloc_traits::const_pointer const_pointer β€” iterator-related typedef
  • typedef _Alloc_traits::reference reference β€” iterator-related typedef
  • typedef _Alloc_traits::const_reference const_reference β€” iterator-related typedef
  • typedef _Rep_type::const_iterator iterator β€” iterator-related typedef. yes, iterator is a const_iterator here. it's giving "you can't touch my keys" energy πŸ”₯
  • typedef _Rep_type::const_iterator const_iterator β€” iterator-related typedef
  • typedef _Rep_type::const_reverse_iterator reverse_iterator β€” iterator-related typedef
  • typedef _Rep_type::const_reverse_iterator const_reverse_iterator β€” iterator-related typedef
  • typedef _Rep_type::size_type size_type β€” iterator-related typedef
  • typedef _Rep_type::difference_type difference_type β€” iterator-related typedef

πŸ¦‹ Public Member Functions

buckle up, there's a LOT of them, and I'm not skipping a single one πŸ’€

  • set ()=default β€” default constructor, creates no elements. empty girl, clean slate 🌸
  • template<typename _InputIterator> set (_InputIterator __first, _InputIterator __last) β€” builds a set from a range 🫢
  • template<typename _InputIterator> set (_InputIterator __first, _InputIterator __last, const _Compare &__comp, const allocator_type &__a=allocator_type()) β€” also builds a set from a range, but now you're bringing your own __comp and allocator ✨
  • template<typename _InputIterator> set (_InputIterator __first, _InputIterator __last, const allocator_type &__a) β€” allocator-extended range constructor πŸ’œ
  • set (const _Compare &__comp, const allocator_type &__a=allocator_type()) β€” creates a set with no elements, but with your comparison object and allocator 😌
  • set (const allocator_type &__a) β€” allocator-extended default constructor πŸŽ€
  • set (const set &)=default β€” set copy constructor. copying is self-care πŸ’…
  • set (const set &__x, const __type_identity_t< allocator_type > &__a) β€” allocator-extended copy constructor πŸ¦‹
  • set (initializer_list< value_type > __l, const _Compare &__comp=_Compare(), const allocator_type &__a=allocator_type()) β€” builds a set from an initializer_list ⭐
  • set (initializer_list< value_type > __l, const allocator_type &__a) β€” allocator-extended initializer-list constructor 🌈
  • set (set &&)=default β€” set move constructor. she's moving out, and she's taking the vibes with her πŸ’¨
  • 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, with the noexcept condition copied exactly, no cap πŸ”₯
  • ~set ()=default β€” destructor πŸ’€
  • iterator begin () const noexcept
  • iterator cbegin () const noexcept
  • iterator cend () const noexcept
  • void clear () noexcept
  • reverse_iterator crbegin () const noexcept
  • reverse_iterator crend () const noexcept
  • template<typename... _Args> std::pair< iterator, bool > emplace (_Args &&... __args) β€” attempts to build and insert an element into the set 🫢
  • template<typename... _Args> iterator emplace_hint (const_iterator __pos, _Args &&... __args) β€” attempts to insert an element into the set, with a hint about where πŸ‘€
  • bool empty () const noexcept β€” returns true if the set is empty 🌸
  • iterator end () const noexcept
  • size_type erase (const key_type &__x) β€” erases elements according to the provided key. bye bestie πŸ’…
  • iterator erase (const_iterator __first, const_iterator __last) β€” erases a [__first,__last) range of elements from a set ✨
  • iterator erase (const_iterator __position) β€” erases an element from a set πŸ¦‹
  • allocator_type get_allocator () const noexcept β€” returns the allocator object with which the set was constructed πŸŽ€
  • template<typename _InputIterator> void insert (_InputIterator __first, _InputIterator __last) β€” a template function that attempts to insert a range of elements 🌈
  • std::pair< iterator, bool > insert (const value_type &__x) β€” attempts to insert an element into the set πŸ’œ
  • void insert (initializer_list< value_type > __l) β€” attempts to insert a list of elements into the set ⭐
  • std::pair< iterator, bool > insert (value_type &&__x) β€” (no description given, she's just here, being mysterious πŸ™ˆ)
  • key_compare key_comp () const β€” returns the comparison object with which the set was constructed πŸ‘‘
  • size_type max_size () const noexcept β€” returns the maximum size of the set πŸ”₯
  • set & operator= (const set &)=default β€” set assignment operator 😌
  • set & operator= (initializer_list< value_type > __l) β€” set list assignment operator 🫢
  • set & operator= (set &&)=default β€” move assignment operator πŸ’¨
  • reverse_iterator rbegin () const noexcept
  • reverse_iterator rend () const noexcept
  • size_type size () const noexcept β€” returns the size of the set 🌸
  • void swap (set &__x) noexcept(/*conditional */) β€” swaps data with another set. the noexcept is conditional, exactly as written, don't come for me πŸ’…
  • value_compare value_comp () const β€” returns the comparison object with which the set was constructed πŸŽ€

πŸ’« ...more Public Member Functions (yes there's a second batch, she's thorough)

  • iterator insert (const_iterator __position, const value_type &__x) β€” attempts to insert an element into the set πŸ¦‹
  • iterator insert (const_iterator __position, value_type &&__x) β€” attempts to insert an element into the set 🌈
  • size_type count (const key_type &__x) const β€” finds the number of elements πŸ‘€
  • bool contains (const key_type &__x) const β€” finds whether an element with the given key exists ✨
  • template<typename _Kt> auto contains (const _Kt &__x) const -> decltype(_M_t._M_find_tr(__x), void(), true) β€” also finds whether an element with the given key exists, but templated πŸ’œ
  • iterator find (const key_type &__x) β€” tries to locate an element in a set πŸ”
  • const_iterator find (const key_type &__x) const β€” tries to locate an element in a set πŸ”
  • iterator lower_bound (const key_type &__x) β€” finds the beginning of a subsequence matching given key ⬇️
  • const_iterator lower_bound (const key_type &__x) const β€” finds the beginning of a subsequence matching given key ⬇️
  • iterator upper_bound (const key_type &__x) β€” finds the end of a subsequence matching given key ⬆️
  • const_iterator upper_bound (const key_type &__x) const β€” finds the end of a subsequence matching given key ⬆️
  • std::pair< iterator, iterator > equal_range (const key_type &__x) β€” finds a subsequence matching given key 🫢
  • std::pair< const_iterator, const_iterator > equal_range (const key_type &__x) const β€” finds a subsequence matching given key 🫢

πŸ’œ Friends

  • template<typename _K1, typename _C1, typename _A1> bool operator< (const set< _K1, _C1, _A1 > &, const set< _K1, _C1, _A1 > &)
  • template<typename _K1, typename _C1, typename _A1> bool operator== (const set< _K1, _C1, _A1 > &, const set< _K1, _C1, _A1 > &)

πŸ“– Detailed Description πŸ“–

template<typename _Key, typename _Compare = std::less<_Key>, typename _Alloc = std::allocator<_Key>>
class std::set< _Key, _Compare, _Alloc >

so this is "a standard container made up of unique keys, which can be retrieved in logarithmic time." ✨ queen behaviour, honestly.

Since: C++98 πŸ‘‘ (she's been that girl since forever)

🌟 Template Parameters

  • _Key β€” type of key objects πŸŽ€
  • _Compare β€” comparison function object type, defaults to less<_Key> πŸ’…
  • _Alloc β€” allocator type, defaults to allocator<_Key> πŸ¦‹

Meets the requirements of a container, a reversible container, and an associative container (using unique keys) 🌈

Sets support bidirectional iterators πŸ”₯

🫢 heads up bestie: the private tree data is declared exactly the same way for set and multiset; the distinction is made entirely in how the tree functions are called (*_unique versus *_equal, same as the standard). so like, same tree, different attitude πŸ’…

✨ Member Typedef Documentation ✨

okay so these are the typedefs, bestie β€” the little aliases that make std::set go brrr. Every single one is here, no skips, periodt. πŸ’…

  • allocator_type β€” public typedef energy ✨
template<typename _Key, typename _Compare = std::less<_Key>, typename _Alloc = std::allocator<_Key>> typedef _Alloc std::set< _Key, _Compare, _Alloc >::allocator_type

Public typedefs. πŸŽ€

  • const_iterator β€” iterator-related, no cap πŸ¦‹
template<typename _Key, typename _Compare = std::less<_Key>, typename _Alloc = std::allocator<_Key>> typedef _Rep_type::const_iterator std::set< _Key, _Compare, _Alloc >::const_iterator

Iterator-related typedefs. πŸ’–

  • const_pointer β€” pointer but make it const πŸ’…
template<typename _Key, typename _Compare = std::less<_Key>, typename _Alloc = std::allocator<_Key>> typedef _Alloc_traits::const_pointer std::set< _Key, _Compare, _Alloc >::const_pointer

Iterator-related typedefs. ✨

  • const_reference β€” reference but const, iconic πŸ‘‘
template<typename _Key, typename _Compare = std::less<_Key>, typename _Alloc = std::allocator<_Key>> typedef _Alloc_traits::const_reference std::set< _Key, _Compare, _Alloc >::const_reference

Iterator-related typedefs. πŸ¦‹

  • const_reverse_iterator β€” reverse iterator but const πŸ’™
template<typename _Key, typename _Compare = std::less<_Key>, typename _Alloc = std::allocator<_Key>> typedef _Rep_type::const_reverse_iterator std::set< _Key, _Compare, _Alloc >::const_reverse_iterator

Iterator-related typedefs. πŸŽ€

  • difference_type β€” the difference type, fr fr πŸ”₯
template<typename _Key, typename _Compare = std::less<_Key>, typename _Alloc = std::allocator<_Key>> typedef _Rep_type::difference_type std::set< _Key, _Compare, _Alloc >::difference_type

Iterator-related typedefs. πŸ’–

  • iterator β€” the iterator, main character energy 🌈
template<typename _Key, typename _Compare = std::less<_Key>, typename _Alloc = std::allocator<_Key>> typedef _Rep_type::const_iterator std::set< _Key, _Compare, _Alloc >::iterator

Iterator-related typedefs. ✨

  • key_compare β€” public typedef, slay πŸ’…
template<typename _Key, typename _Compare = std::less<_Key>, typename _Alloc = std::allocator<_Key>> typedef _Compare std::set< _Key, _Compare, _Alloc >::key_compare

Public typedefs. πŸŽ€

  • key_type β€” the key type, periodt πŸ‘‘
template<typename _Key, typename _Compare = std::less<_Key>, typename _Alloc = std::allocator<_Key>> typedef _Key std::set< _Key, _Compare, _Alloc >::key_type

Public typedefs. πŸ’–

  • pointer β€” pointer vibes πŸ¦‹
template<typename _Key, typename _Compare = std::less<_Key>, typename _Alloc = std::allocator<_Key>> typedef _Alloc_traits::pointer std::set< _Key, _Compare, _Alloc >::pointer

Iterator-related typedefs. ✨

  • reference β€” reference energy πŸ’œ
template<typename _Key, typename _Compare = std::less<_Key>, typename _Alloc = std::allocator<_Key>> typedef _Alloc_traits::reference std::set< _Key, _Compare, _Alloc >::reference

Iterator-related typedefs. πŸŽ€

  • reverse_iterator β€” reverse iterator, no cap πŸ’™
template<typename _Key, typename _Compare = std::less<_Key>, typename _Alloc = std::allocator<_Key>> typedef _Rep_type::const_reverse_iterator std::set< _Key, _Compare, _Alloc >::reverse_iterator

Iterator-related typedefs. πŸ¦‹

  • size_type β€” size type, iconic ⭐
template<typename _Key, typename _Compare = std::less<_Key>, typename _Alloc = std::allocator<_Key>> typedef _Rep_type::size_type std::set< _Key, _Compare, _Alloc >::size_type

Iterator-related typedefs. πŸ’–

  • value_compare β€” public typedef, ate πŸ“
template<typename _Key, typename _Compare = std::less<_Key>, typename _Alloc = std::allocator<_Key>> typedef _Compare std::set< _Key, _Compare, _Alloc >::value_compare

Public typedefs. ✨

  • value_type β€” the value type, bestie 🌈
template<typename _Key, typename _Compare = std::less<_Key>, typename _Alloc = std::allocator<_Key>> typedef _Key std::set< _Key, _Compare, _Alloc >::value_type

Public typedefs. πŸŽ€


πŸ› οΈ Constructor & Destructor Documentation πŸ› οΈ

  • set() β€” the default constructor, bestie 🌸
template<typename _Key, typename _Compare = std::less<_Key>, typename _Alloc = std::allocator<_Key>> std::set< _Key, _Compare, _Alloc >::set ()

[default] β€” Default constructor creates no elements. πŸ’…

  • set(const _Compare & __comp, const allocator_type & __a = allocator_type()) β€” creates a set with no elements πŸŽ€
template<typename _Key, typename _Compare = std::less<_Key>, typename _Alloc = std::allocator<_Key>> std::set< _Key, _Compare, _Alloc >::set (const _Compare & __comp, const allocator_type & __a = allocator_type())

[inline], [explicit] β€” Creates a set with no elements.

Parameters

  • __comp β€” Comparator to use.
  • __a β€” An allocator object. ✨
  • set(_InputIterator __first, _InputIterator __last) β€” range constructor, iconic 🌈
template<typename _Key, typename _Compare = std::less<_Key>, typename _Alloc = std::allocator<_Key>> template<typename _InputIterator> std::set< _Key, _Compare, _Alloc >::set (_InputIterator __first, _InputIterator __last)

[inline] β€” Builds a set from a range.

Parameters

  • __first β€” An input iterator.
  • __last β€” An input iterator.

Create a set consisting of copies of the elements from [__first,__last). This is linear in N if the range is already sorted, and NlogN otherwise (where N is distance(__first,__last)). ✨

  • set(_InputIterator __first, _InputIterator __last, const _Compare & __comp, const allocator_type & __a = allocator_type()) β€” range constructor with extras πŸ’…
template<typename _Key, typename _Compare = std::less<_Key>, typename _Alloc = std::allocator<_Key>> template<typename _InputIterator> std::set< _Key, _Compare, _Alloc >::set (_InputIterator __first, _InputIterator __last, const _Compare & __comp, const allocator_type & __a = allocator_type())

[inline] β€” Builds a set from a range.

Parameters

  • __first β€” An input iterator.
  • __last β€” An input iterator.
  • __comp β€” A comparison functor.
  • __a β€” An allocator object.

Create a set consisting of copies of the elements from [__first,__last). This is linear in N if the range is already sorted, and NlogN otherwise (where N is distance(__first,__last)). πŸ¦‹

  • set(const set< _Key, _Compare, _Alloc > & ) β€” copy constructor, slay πŸ’™
template<typename _Key, typename _Compare = std::less<_Key>, typename _Alloc = std::allocator<_Key>> std::set< _Key, _Compare, _Alloc >::set (const set< _Key, _Compare, _Alloc > & )

[default] β€” Set copy constructor. Whether the allocator is copied depends on the allocator traits. πŸ¦‹

  • set(set< _Key, _Compare, _Alloc > && ) β€” move constructor, main character energy πŸ‘‘
template<typename _Key, typename _Compare = std::less<_Key>, typename _Alloc = std::allocator<_Key>> std::set< _Key, _Compare, _Alloc >::set (set< _Key, _Compare, _Alloc > && )

[default] β€” Set move constructor The newly-created set contains the exact contents of the moved instance. The moved instance is a valid, but unspecified, set. πŸ’…

  • set(initializer_list< value_type > __l, const _Compare & __comp = _Compare(), const allocator_type & __a = allocator_type()) β€” initializer_list constructor, ate πŸ“
template<typename _Key, typename _Compare = std::less<_Key>, typename _Alloc = std::allocator<_Key>> std::set< _Key, _Compare, _Alloc >::set (initializer_list< value_type > __l, const _Compare & __comp = _Compare(), const allocator_type & __a = allocator_type())

[inline] β€” Builds a set from an initializer_list.

Parameters

  • __l β€” An initializer_list.
  • __comp β€” A comparison functor.
  • __a β€” An allocator object.

Create a set consisting of copies of the elements in the list. This is linear in N if the list is already sorted, and NlogN otherwise (where N is __l.size()). ✨

  • set(const allocator_type & __a) β€” allocator-extended default constructor πŸ’›
template<typename _Key, typename _Compare = std::less<_Key>, typename _Alloc = std::allocator<_Key>> std::set< _Key, _Compare, _Alloc >::set (const allocator_type & __a)

[inline], [explicit] β€” Allocator-extended default constructor. πŸŽ€

  • set(const set< _Key, _Compare, _Alloc > & __x, const __type_identity_t< allocator_type > & __a) β€” allocator-extended copy constructor πŸ’™
template<typename _Key, typename _Compare = std::less<_Key>, typename _Alloc = std::allocator<_Key>> std::set< _Key, _Compare, _Alloc >::set (const set< _Key, _Compare, _Alloc > & __x, const __type_identity_t< allocator_type > & __a)

[inline] β€” Allocator-extended copy constructor. πŸ¦‹

  • set(set< _Key, _Compare, _Alloc > && __x, const __type_identity_t< allocator_type > & __a) β€” allocator-extended move constructor πŸ”₯
template<typename _Key, typename _Compare = std::less<_Key>, typename _Alloc = std::allocator<_Key>> std::set< _Key, _Compare, _Alloc >::set (set< _Key, _Compare, _Alloc > && __x, const __type_identity_t< allocator_type > & __a)

[inline], [noexcept] β€” Allocator-extended move constructor. ✨

  • set(initializer_list< value_type > __l, const allocator_type & __a) β€” allocator-extended initialier-list constructor 🌈
template<typename _Key, typename _Compare = std::less<_Key>, typename _Alloc = std::allocator<_Key>> std::set< _Key, _Compare, _Alloc >::set (initializer_list< value_type > __l, const allocator_type & __a)

[inline] β€” Allocator-extended initialier-list constructor. πŸ’…

  • set(_InputIterator __first, _InputIterator __last, const allocator_type & __a) β€” allocator-extended range constructor πŸ¦‹
template<typename _Key, typename _Compare = std::less<_Key>, typename _Alloc = std::allocator<_Key>> template<typename _InputIterator> std::set< _Key, _Compare, _Alloc >::set (_InputIterator __first, _InputIterator __last, const allocator_type & __a)

[inline] β€” Allocator-extended range constructor. πŸŽ€

  • ~set() β€” destructor, bye bestie πŸ’€
template<typename _Key, typename _Compare = std::less<_Key>, typename _Alloc = std::allocator<_Key>> std::set< _Key, _Compare, _Alloc >::~set ()

[default] β€” The dtor only erases the elements, and note that if the elements themselves are pointers, the pointed-to memory is not touched in any way. Managing the pointer is the user's responsibility. 🫢


πŸ’– Member Function Documentation πŸ’–

  • begin() const β€” first element energy ✨
template<typename _Key, typename _Compare = std::less<_Key>, typename _Alloc = std::allocator<_Key>> iterator std::set< _Key, _Compare, _Alloc >::begin () const

[inline], [noexcept] β€” Returns a read-only (constant) iterator that points to the first element in the set. Iteration is done in ascending order according to the keys. πŸ’…

  • cbegin() const β€” const first element, no cap πŸ¦‹
template<typename _Key, typename _Compare = std::less<_Key>, typename _Alloc = std::allocator<_Key>> iterator std::set< _Key, _Compare, _Alloc >::cbegin () const

[inline], [noexcept] β€” Returns a read-only (constant) iterator that points to the first element in the set. Iteration is done in ascending order according to the keys. ✨

  • cend() const β€” one past the last, iconic πŸ‘‘
template<typename _Key, typename _Compare = std::less<_Key>, typename _Alloc = std::allocator<_Key>> iterator std::set< _Key, _Compare, _Alloc >::cend () const

[inline], [noexcept] β€” Returns a read-only (constant) iterator that points one past the last element in the set. Iteration is done in ascending order according to the keys. πŸ’œ

  • clear() β€” erase everything, periodt πŸ’…
template<typename _Key, typename _Compare = std::less<_Key>, typename _Alloc = std::allocator<_Key>> void std::set< _Key, _Compare, _Alloc >::clear ()

[inline], [noexcept] β€” Erases all elements in a set. Note that this function only erases the elements, and that if the elements themselves are pointers, the pointed-to memory is not touched in any way. Managing the pointer is the user's responsibility. 🫢

  • contains(const _Kt & __x) const β€” does it exist? bestie check 🌈
template<typename _Key, typename _Compare = std::less<_Key>, typename _Alloc = std::allocator<_Key>> template<typename _Kt> auto std::set< _Key, _Compare, _Alloc >::contains (const _Kt & __x) const -> decltype(_M_t._M_find_tr(__x), void(), true)

[inline] β€” Finds whether an element with the given key exists.

Parameters

  • __x β€” Key of elements to be located.

Returns

  • True if there is an element with the specified key. ✨
  • contains(const key_type & __x) const β€” same vibe, different signature πŸ’…
template<typename _Key, typename _Compare = std::less<_Key>, typename _Alloc = std::allocator<_Key>> bool std::set< _Key, _Compare, _Alloc >::contains (const key_type & __x) const

[inline] β€” Finds whether an element with the given key exists.

Parameters

  • __x β€” Key of elements to be located.

Returns

  • True if there is an element with the specified key. πŸ¦‹
  • count(const key_type & __x) const β€” count 'em πŸ”₯
template<typename _Key, typename _Compare = std::less<_Key>, typename _Alloc = std::allocator<_Key>> size_type std::set< _Key, _Compare, _Alloc >::count (const key_type & __x) const

[inline] β€” Finds the number of elements.

Parameters

  • __x β€” Element to located.

Returns

  • Number of elements with specified key.

This function only makes sense for multisets; for set the result will either be 0 (not present) or 1 (present). πŸ’…

  • crbegin() const β€” last element reverse energy πŸ’™
template<typename _Key, typename _Compare = std::less<_Key>, typename _Alloc = std::allocator<_Key>> reverse_iterator std::set< _Key, _Compare, _Alloc >::crbegin () const

[inline], [noexcept] β€” Returns a read-only (constant) iterator that points to the last element in the set. Iteration is done in descending order according to the keys. πŸ‘‘

crend πŸ’–

   template<typename _Key, typename _Compare = std::less<_Key>, typename _Alloc
       = std::allocator<_Key>> reverse_iterator std::set< _Key, _Compare,
       _Alloc >::crend () const [inline],  [noexcept]

Okay so this one hands you a read-only (constant) reverse iterator that points to the last pair in the set. Iteration is done in descending order according to the keys. It's giving reverse main character energy, fr. ✨


emplace πŸ› οΈ

   template<typename _Key, typename _Compare = std::less<_Key>, typename _Alloc
       = std::allocator<_Key>> template<typename... _Args> std::pair< iterator,
       bool > std::set< _Key, _Compare, _Alloc >::emplace (_Args &&... __args)
       [inline]

Attempts to build and insert an element into the set. Like, it's trying its best.

Parameters

  • __args β€” Arguments used to generate an element.

Returns

  • A pair, of which the first element is an iterator that points to the possibly inserted element, and the second is a bool that is true if the element was actually inserted.

This function attempts to build and insert an element into the set. A set relies on unique keys and thus an element is only inserted if it is not already present in the set. No duplicates, periodt.

Insertion requires logarithmic time. 😭


emplace_hint πŸ“

   template<typename _Key, typename _Compare = std::less<_Key>, typename _Alloc
       = std::allocator<_Key>> template<typename... _Args> iterator std::set<
       _Key, _Compare, _Alloc >::emplace_hint (const_iterator __pos, _Args
       &&... __args) [inline]

Attempts to insert an element into the set.

Parameters

  • __pos β€” An iterator that serves as a hint as to where the element should be inserted.
  • __args β€” Arguments used to generate the element to be inserted.

Returns

  • An iterator that points to the element with key equivalent to the one generated from __args (may or may not be the element itself).

This function is not concerned about whether the insertion took place, and thus does not return a boolean like the single-argument emplace() does. Note that the first parameter is only a hint and can potentially improve the performance of the insertion process. A bad hint would cause no gains in efficiency. So don't just throw any old iterator in there, bestie. πŸ™ˆ

For more on hinting, see: https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints

Insertion requires logarithmic time (if the hint is not taken). ⏳


empty πŸ«™

   template<typename _Key, typename _Compare = std::less<_Key>, typename _Alloc
       = std::allocator<_Key>> bool std::set< _Key, _Compare, _Alloc >::empty
       () const [inline],  [nodiscard],  [noexcept]

Returns true if the set is empty. That's it. That's the vibe check. ✨


end 🏁

   template<typename _Key, typename _Compare = std::less<_Key>, typename _Alloc
       = std::allocator<_Key>> iterator std::set< _Key, _Compare, _Alloc >::end
       () const [inline],  [noexcept]

Returns a read-only (constant) iterator that points one past the last element in the set. Iteration is done in ascending order according to the keys. Classic past-the-end behavior, no cap.


equal_range (non-const) πŸ”

   template<typename _Key, typename _Compare = std::less<_Key>, typename _Alloc
       = std::allocator<_Key>> std::pair< iterator, iterator > std::set< _Key,
       _Compare, _Alloc >::equal_range (const key_type & __x) [inline]

Finds a subsequence matching given key.

Parameters

  • __x β€” Key to be located.

Returns

  • Pair of iterators that possibly points to the subsequence matching given key.

This function is equivalent to

       std::make_pair(c.lower_bound(val),
                      c.upper_bound(val))

(but is faster than making the calls separately). Speed demon behavior. πŸ’…

This function probably only makes sense for multisets. For a plain set? Eh, it's giving extra. 🀷


equal_range (const) πŸ”βœ¨

   template<typename _Key, typename _Compare = std::less<_Key>, typename _Alloc
       = std::allocator<_Key>> std::pair< const_iterator, const_iterator >
       std::set< _Key, _Compare, _Alloc >::equal_range (const key_type & __x)
       const [inline]

Finds a subsequence matching given key.

Parameters

  • __x β€” Key to be located.

Returns

  • Pair of iterators that possibly points to the subsequence matching given key.

This function is equivalent to

       std::make_pair(c.lower_bound(val),
                      c.upper_bound(val))

(but is faster than making the calls separately).

This function probably only makes sense for multisets. Again, just saying. 🫢


erase (by key) 🧹

   template<typename _Key, typename _Compare = std::less<_Key>, typename _Alloc
       = std::allocator<_Key>> size_type std::set< _Key, _Compare, _Alloc
       >::erase (const key_type & __x) [inline]

Erases elements according to the provided key. Bye bestie. πŸ‘‹

Parameters

  • __x β€” Key of element to be erased.

Returns

  • The number of elements erased.

This function erases all the elements located by the given key from a set. Note that this function only erases the element, and that if the element is itself a pointer, the pointed-to memory is not touched in any way. Managing the pointer is the user's responsibility. So like, don't expect it to clean up your new'd stuff. That's on you, babe. πŸ’€


erase (range) πŸ—‘οΈ

   template<typename _Key, typename _Compare = std::less<_Key>, typename _Alloc
       = std::allocator<_Key>> iterator std::set< _Key, _Compare, _Alloc
       >::erase (const_iterator __first, const_iterator __last) [inline]

Erases a [__first,__last) range of elements from a set. Half-open range, iconic.

Parameters

  • __first β€” Iterator pointing to the start of the range to be erased.
  • __last β€” Iterator pointing to the end of the range to be erased.

Returns

  • The iterator __last.

This function erases a sequence of elements from a set. Note that this function only erases the element, and that if the element is itself a pointer, the pointed-to memory is not touched in any way. Managing the pointer is the user's responsibility. Same pointer warning, same energy. 🫠


erase (single iterator) 🎯

   template<typename _Key, typename _Compare = std::less<_Key>, typename _Alloc
       = std::allocator<_Key>> iterator std::set< _Key, _Compare, _Alloc
       >::erase (const_iterator __position) [inline]

Erases an element from a set.

Parameters

  • __position β€” An iterator pointing to the element to be erased.

Returns

  • An iterator pointing to the element immediately following __position prior to the element being erased. If no such element exists, end() is returned.

This function erases an element, pointed to by the given iterator, from a set. Note that this function only erases the element, and that if the element is itself a pointer, the pointed-to memory is not touched in any way. Managing the pointer is the user's responsibility. You know the drill. πŸ’…


find (non-const) πŸ”Ž

   template<typename _Key, typename _Compare = std::less<_Key>, typename _Alloc
       = std::allocator<_Key>> iterator std::set< _Key, _Compare, _Alloc
       >::find (const key_type & __x) [inline]

Tries to locate an element in a set.

Parameters

  • __x β€” Element to be located.

Returns

  • Iterator pointing to sought-after element, or end() if not found.

This function takes a key and tries to locate the element with which the key matches. If successful the function returns an iterator pointing to the sought after element. If unsuccessful it returns the past-the-end (end()) iterator. So either you find it or you get the end. Dramatic. 😭


find (const) πŸ”Žβœ¨

   template<typename _Key, typename _Compare = std::less<_Key>, typename _Alloc
       = std::allocator<_Key>> const_iterator std::set< _Key, _Compare, _Alloc
       >::find (const key_type & __x) const [inline]

Tries to locate an element in a set.

Parameters

  • __x β€” Element to be located.

Returns

  • Iterator pointing to sought-after element, or end() if not found.

This function takes a key and tries to locate the element with which the key matches. If successful the function returns an iterator pointing to the sought after element. If unsuccessful it returns the past-the-end (end()) iterator. Same as above but const, so it's giving read-only mystery. πŸ•΅οΈ


get_allocator 🧺

   template<typename _Key, typename _Compare = std::less<_Key>, typename _Alloc
       = std::allocator<_Key>> allocator_type std::set< _Key, _Compare, _Alloc
       >::get_allocator () const [inline],  [noexcept]

Returns the allocator object with which the set was constructed. It's the set's little memory manager bestie. ✨


insert (range) πŸ“₯

   template<typename _Key, typename _Compare = std::less<_Key>, typename _Alloc
       = std::allocator<_Key>> template<typename _InputIterator> void std::set<
       _Key, _Compare, _Alloc >::insert (_InputIterator __first, _InputIterator
       __last) [inline]

A template function that attempts to insert a range of elements.

Parameters

  • __first β€” Iterator pointing to the start of the range to be inserted.
  • __last β€” Iterator pointing to the end of the range.

Complexity similar to that of the range constructor. So like, same vibe as construction. πŸŒ€


insert (single value) πŸ“Œ

   template<typename _Key, typename _Compare = std::less<_Key>, typename _Alloc
       = std::allocator<_Key>> std::pair< iterator, bool > std::set< _Key,
       _Compare, _Alloc >::insert (const value_type & __x) [inline]

Attempts to insert an element into the set.

Parameters

  • __x β€” Element to be inserted.

Returns

  • A pair, of which the first element is an iterator that points to the possibly inserted element, and the second is a bool that is true if the element was actually inserted.

This function attempts to insert an element into the set. A set relies on unique keys and thus an element is only inserted if it is not already present in the set. No duplicates, no exceptions. Periodt.

Insertion requires logarithmic time. ⏳

Referenced by std::set< _Key, _Cmp, polymorphic_allocator< _Key > >::insert().


insert (hint + const value) πŸͺ„

   template<typename _Key, typename _Compare = std::less<_Key>, typename _Alloc
       = std::allocator<_Key>> iterator std::set< _Key, _Compare, _Alloc
       >::insert (const_iterator __position, const value_type & __x) [inline]

Attempts to insert an element into the set.

Parameters

  • __position β€” An iterator that serves as a hint as to where the element should be inserted.
  • __x β€” Element to be inserted.

Returns

  • An iterator that points to the element with key of __x (may or may not be the element passed in).

This function is not concerned about whether the insertion took place, and thus does not return a boolean like the single-argument insert() does. Note that the first parameter is only a hint and can potentially improve the performance of the insertion process. A bad hint would cause no gains in efficiency. So hint wisely, bestie. 🧠

For more on hinting, see: https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints

If a heterogeneous key __k matches a range of elements, an iterator to the first is returned. Interesting. πŸ‘€

Insertion requires logarithmic time (if the hint is not taken).


insert (hint + rvalue) πŸš€

   template<typename _Key, typename _Compare = std::less<_Key>, typename _Alloc
       = std::allocator<_Key>> iterator std::set< _Key, _Compare, _Alloc
       >::insert (const_iterator __position, value_type && __x) [inline]

Attempts to insert an element into the set.

Parameters

  • __position β€” An iterator that serves as a hint as to where the element should be inserted.
  • __x β€” Element to be inserted.

Returns

  • An iterator that points to the element with key of __x (may or may not be the element passed in).

πŸ’– insert (the hinted one) πŸ’–

template<typename _Key, typename _Compare = std::less<_Key>,
         typename _Alloc = std::allocator<_Key>>
iterator std::set< _Key, _Compare, _Alloc >::insert(const_iterator __position,
                                                    const value_type & __x)

okay so this one does not care whether the insertion actually happened or not β€” like, it's not gonna hand you a boolean back the way the single-argument insert() does. bestie just does the thing. πŸ™ˆ

also, heads up: that first parameter is only a hint. it can potentially make the insertion faster, but a bad hint gives you no gains in efficiency at all β€” so no pressure, it's not a promise, it's a vibe suggestion.

for more on hinting, see:

https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints

if a heterogeneous key __k matches a range of elements, you get an iterator to the first one. 🎯

insertion needs logarithmic time (unless the hint gets taken, iykyk).


πŸ“₯ insert β€” but make it a whole list πŸ“₯

template<typename _Key, typename _Compare = std::less<_Key>,
         typename _Alloc = std::allocator<_Key>>
void std::set< _Key, _Compare, _Alloc >::insert(initializer_list< value_type > __l)

(marked [inline])

attempts to insert a list of elements into the set. yes queen, bulk insert. πŸ’…

Parameters

  • __l β€” a std::initializer_list<value_type> of the elements to be inserted.

Complexity β€” similar to that of the range constructor. slay, that's it. ✨


πŸ” key_comp πŸ”

template<typename _Key, typename _Compare = std::less<_Key>,
         typename _Alloc = std::allocator<_Key>>
key_compare std::set< _Key, _Compare, _Alloc >::key_comp() const

(marked [inline])

returns the comparison object that the set was constructed with. that's the whole thing, she's simple like that. πŸŽ€


🌸 lower_bound (the regular one) 🌸

template<typename _Key, typename _Compare = std::less<_Key>,
         typename _Alloc = std::allocator<_Key>>
iterator std::set< _Key, _Compare, _Alloc >::lower_bound(const key_type & __x)

(marked [inline])

finds the beginning of a subsequence matching the given key.

Parameters

  • __x β€” key to be located.

Returns

  • an iterator pointing to the first element equal to or greater than the key, or end().

so basically: this returns the first element of a subsequence of elements that matches the given key. if that doesn't work out, you get an iterator pointing to the first element that has a greater value than the given key β€” or end() if no such element even exists. no cap. πŸ’€


🌸 lower_bound β€” const edition 🌸

template<typename _Key, typename _Compare = std::less<_Key>,
         typename _Alloc = std::allocator<_Key>>
const_iterator std::set< _Key, _Compare, _Alloc >::lower_bound(const key_type & __x) const

(marked [inline])

finds the beginning of a subsequence matching the given key.

Parameters

  • __x β€” key to be located.

Returns

  • an iterator pointing to the first element equal to or greater than the key, or end().

same deal as above, literally identical energy: returns the first element of a subsequence matching the given key, and if that fails you get the first element with a greater value than the given key, or end() if there's nothing there. πŸ’–


πŸ“ max_size πŸ“

template<typename _Key, typename _Compare = std::less<_Key>,
         typename _Alloc = std::allocator<_Key>>
size_type std::set< _Key, _Compare, _Alloc >::max_size() const

(marked [inline], [noexcept])

returns the maximum size of the set. big girl numbers only. πŸ”₯


πŸŽ€ operator= (copy assignment) πŸŽ€

template<typename _Key, typename _Compare = std::less<_Key>,
         typename _Alloc = std::allocator<_Key>>
set & std::set< _Key, _Compare, _Alloc >::operator=(const set< _Key, _Compare, _Alloc > &)

(marked [default])

set assignment operator. whether the allocator gets copied depends on the allocator traits β€” so that part's not up to me, bestie. 🫢


πŸŽ€ operator= (list assignment) πŸŽ€

template<typename _Key, typename _Compare = std::less<_Key>,
         typename _Alloc = std::allocator<_Key>>
set & std::set< _Key, _Compare, _Alloc >::operator=(initializer_list< value_type > __l)

(marked [inline])

set list assignment operator. πŸ’…

Parameters

  • __l β€” an initializer_list.

this function fills a set with copies of the elements in the initializer list __l.

⚠️ note that the assignment completely changes the set β€” like, fully new identity β€” and the resulting set's size is the same as the number of elements assigned. main character makeover energy, fr fr.

πŸŽ€ operator= (move assignment) πŸŽ€

template<typename _Key, typename _Compare = std::less<_Key>,
         typename _Alloc = std::allocator<_Key>>
set & std::set< _Key, _Compare, _Alloc >::operator=(set< _Key, _Compare, _Alloc > &&)

(marked [default])

move assignment operator. she's moving, not copying. πŸ’«


πŸ¦‹ rbegin πŸ¦‹

template<typename _Key, typename _Compare = std::less<_Key>,
         typename _Alloc = std::allocator<_Key>>
reverse_iterator std::set< _Key, _Compare, _Alloc >::rbegin() const

(marked [inline], [noexcept])

returns a read-only (constant) iterator that points to the last element in the set. iteration is done in descending order according to the keys. so we're going backwards and we're thriving. πŸ•Ί


πŸ¦‹ rend πŸ¦‹

template<typename _Key, typename _Compare = std::less<_Key>,
         typename _Alloc = std::allocator<_Key>>
reverse_iterator std::set< _Key, _Compare, _Alloc >::rend() const

(marked [inline], [noexcept])

returns a read-only (constant) reverse iterator that points to the last pair in the set. iteration is done in descending order according to the keys. yes, same descending-order vibe as rbegin β€” they're a matching set, periodt. ✨


πŸ“ size πŸ“

template<typename _Key, typename _Compare = std::less<_Key>,
         typename _Alloc = std::allocator<_Key>>
size_type std::set< _Key, _Compare, _Alloc >::size() const

(marked [inline], [noexcept])

returns the size of the set. how many besties are in the building. πŸ‘‘


πŸ”„ swap πŸ”„

template<typename _Key, typename _Compare = std::less<_Key>,
         typename _Alloc = std::allocator<_Key>>
void std::set< _Key, _Compare, _Alloc >::swap(set< _Key, _Compare, _Alloc > & __x)

(marked [inline], [noexcept])

swaps data with another set. πŸ”

Parameters

  • __x β€” a set of the same element and allocator types.

this exchanges the elements between two sets in constant time. and like β€” it's literally only swapping a pointer, an integer, and an instance of the Compare type (which itself is often stateless and empty), so it should be quite fast. it's giving effortless. πŸ’…

note that the global std::swap() function is specialized such that std::swap(s1,s2) will feed to this function. cute, right? πŸŽ€

whether the allocators are swapped depends on the allocator traits.

referenced by std::swap().


πŸ”Ž upper_bound (the regular one) πŸ”Ž

template<typename _Key, typename _Compare = std::less<_Key>,
         typename _Alloc = std::allocator<_Key>>
iterator std::set< _Key, _Compare, _Alloc >::upper_bound(const key_type & __x)

(marked [inline])

finds the end of a subsequence matching the given key.

Parameters

  • __x β€” key to be located.

Returns

  • an iterator pointing to the first element greater than the key, or end().

πŸ”Ž upper_bound β€” const edition πŸ”Ž

template<typename _Key, typename _Compare = std::less<_Key>,
         typename _Alloc = std::allocator<_Key>>
const_iterator std::set< _Key, _Compare, _Alloc >::upper_bound(const key_type & __x) const

(marked [inline])

finds the end of a subsequence matching the given key.

Parameters

  • __x β€” key to be located.

Returns

  • an iterator pointing to the first element greater than the key, or end().

yep, exact same energy as the non-const one. the const girlies get the same deal. πŸ’œ


🌈 value_comp 🌈

template<typename _Key, typename _Compare = std::less<_Key>,
         typename _Alloc = std::allocator<_Key>>
value_compare std::set< _Key, _Compare, _Alloc >::value_comp() const

(marked [inline])

returns the comparison object that the set was constructed with. she and key_comp are twinning, honestly. ✨


✍️ Author ✍️

generated automatically by Doxygen for libstdc++ from the source code. so like, not a human author, just a robot doing its little thing and honestly? slay. πŸ€–πŸ’–


and that's a wrap on std::set< ..., _Alloc >(3), bestie!! πŸŽ‰ you made it all the way to the end of the page, which is main character energy for real. this page came to you from libstdc++ and it's filed under std::set< ..., _Alloc >(3) in section 3 β€” go forth and insert responsibly. πŸ«ΆπŸ’…βœ¨

πŸ“¦ where this page comes from
  • arch rolling: gcc 16.2.1+r23+gd564253eb6c8-1 Β· GPL-3.0-or-later WITH GCC-exception-3.1 AND GFDL-1.3-or-later
  • fedora 44: libstdc++-docs 16.2.1-2.fc44
  • fedora 44: libstdc++-docs 16.2.1-3.fc44 Β· GPL-3.0-or-later AND LGPL-3.0-or-later AND (GPL-3.0-or-later WITH GCC-exception-3.1) AND (GPL-3.0-or-later WITH Texinfo-exception) AND (LGPL-2.1-or-later WITH GCC-exception-2.0) AND (GPL-2.0-or-later WITH GCC-exception-2.0) AND (GPL-2.0-or-later WITH GNU-compiler-exception) AND BSL-1.0 AND GFDL-1.3-or-later AND Linux-man-pages-copyleft-2-para AND SunPro AND BSD-1-Clause AND BSD-2-Clause AND BSD-2-Clause-Views AND BSD-3-Clause AND BSD-4-Clause AND BSD-Source-Code AND Zlib AND MIT AND Apache-2.0 AND (Apache-2.0 WITH LLVM-Exception) AND ZPL-2.1 AND ISC AND LicenseRef-Fedora-Public-Domain AND HP-1986 AND curl AND Martin-Birgmeier AND HPND-Markus-Kuhn AND dtoa AND SMLNJ AND AMD-newlib AND OAR AND HPND-merchantability-variant AND HPND-Intel
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms