std::flat_set(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… std::flat_set β€” C++ Standard Libary (manual page std::flat_set(3)) πŸ’…

hey bestie πŸ’– welcome to the std::flat_set(3) page. yes, the header spells it β€œC++ Standard Libary” β€” keeping the typo, it's iconic 🫢. this is the whole manual page, same facts, way more group-chat energy. let's go ✨

πŸ’– NAME πŸ’–

std::flat_set β€” std::flat_set (yes, that's literally what the NAME section says 😭).

🌸 Synopsis 🌸

Defined in header <flat_set>

template<

    class Key,
    class Compare = std::less<Key>,        (since C++23)
    class KeyContainer = std::vector<Key>

> class flat_set;

The flat set is a container adaptor that gives the functionality of an associative container that stores a sorted set of unique objects of type Key. Sorting is done using the key comparison function Compare.

The class template flat_set acts as a wrapper to the underlying sorted container passed as object of type KeyContainer.

Everywhere the standard library uses the Compare requirements, uniqueness is determined by using the equivalence relation. Informally, two objects a and b are considered equivalent if neither compares less than the other: !comp(a, b) && !comp(b, a).

std::flat_set meets the requirements of Container, ReversibleContainer, optional container requirements, and all requirements of AssociativeContainer (including logarithmic search complexity), except that:

  • requirements related to nodes are not applicable,
  • iterator invalidation requirements differ,
  • the complexity of insertion and erasure operations is linear.

A flat set supports most AssociativeContainer's operations that use unique keys.

🧩 Template parameters 🧩

  • Key β€” The type of the stored elements. The program is ill-formed if Key is not the same type as KeyContainer::value_type.
  • Compare β€” A Compare type providing a strict weak ordering.
  • KeyContainer β€” The type of the underlying SequenceContainer to store the elements. The iterators of such container should satisfy LegacyRandomAccessIterator or model random_access_iterator. The standard containers std::vector and std::deque satisfy these requirements.

πŸŽ€ Member types πŸŽ€

  • container_type β€” KeyContainer
  • key_type β€” Key
  • value_type β€” Key
  • key_compare β€” Compare
  • value_compare β€” Compare
  • reference β€” value_type&
  • const_reference β€” const value_type&
  • size_type β€” typename KeyContainer::size_type
  • difference_type β€” typename KeyContainer::difference_type
  • iterator β€” implementation-defined LegacyRandomAccessIterator and random_access_iterator to value_type
  • const_iterator β€” implementation-defined LegacyRandomAccessIterator and random_access_iterator to const value_type
  • reverse_iterator β€” std::reverse_iterator<iterator>
  • const_reverse_iterator β€” std::reverse_iterator<const_iterator>

πŸ¦‹ Member objects πŸ¦‹

  • c (private) β€” the underlying container of container_type (exposition-only member object*)
  • compare (private) β€” the comparison function object of type key_compare (exposition-only member object*)

✨ Member functions ✨

  • constructor β€” constructs the flat_set (public member function)
  • destructor (implicitly declared) β€” destroys every element of the container adaptor (public member function)
  • operator= β€” assigns values to the container adaptor (public member function)

🌈 Iterators 🌈

  • begin β€” returns an iterator to the beginning (public member function)
  • cbegin β€” (public member function)
  • end β€” returns an iterator to the end (public member function)
  • cend β€” (public member function)
  • rbegin β€” returns a reverse iterator to the beginning (public member function)
  • crbegin β€” (public member function)
  • rend β€” returns a reverse iterator to the end (public member function)
  • crend β€” (public member function)

πŸ“ Capacity πŸ“

  • empty β€” checks whether the container adaptor is empty (public member function)
  • size β€” returns the number of elements (public member function)
  • max_size β€” returns the maximum possible number of elements (public member function)

πŸ’œ Modifiers πŸ’œ

  • emplace β€” constructs element in-place (public member function)
  • emplace_hint β€” constructs elements in-place using a hint (public member function)
  • insert β€” inserts elements (public member function)
  • insert_range β€” inserts a range of elements (public member function)
  • extract β€” extracts the underlying container (public member function)
  • replace β€” replaces the underlying container (public member function)
  • erase β€” erases elements (public member function)
  • swap β€” swaps the contents (public member function)
  • clear β€” clears the contents (public member function)

πŸ”Ž Lookup πŸ”Ž

  • find β€” finds element with specific key (public member function)
  • count β€” returns the number of elements matching specific key (public member function)
  • contains β€” checks if the container contains element with specific key (public member function)
  • lower_bound β€” returns an iterator to the first element not less than the given key (public member function)
  • upper_bound β€” returns an iterator to the first element greater than the given key (public member function)
  • equal_range β€” returns range of elements matching a specific key (public member function)

πŸ‘€ Observers πŸ‘€

  • key_comp β€” returns the function that compares keys (public member function)
  • value_comp β€” returns the function that compares keys in objects of type value_type (public member function)

🌟 Non-member functions 🌟

  • operator== β€” lexicographically compares the values of two flat_sets
  • operator<=> β€” (function template) (C++23)
  • std::swap(std::flat_set) β€” specializes the std::swap algorithm; (C++23) (function template)
  • erase_if(std::flat_set) β€” erases all elements satisfying specific criteria; (C++23) (function template)

🫢 Helper classes 🫢

  • std::uses_allocator<std::flat_set> β€” specializes the std::uses_allocator type trait; (C++23) (class template specialization)

🏷️ Tags 🏷️

  • sorted_unique, sorted_unique_t (tag) (C++23) β€” indicates that elements of a range are sorted and unique.

🧚 Deduction guides 🧚

  • nothing listed here on the page 🫢. the heading exists, but no entries follow it.

πŸ“ Notes πŸ“

The member types iterator and const_iterator may be aliases to the same type. This means defining a pair of function overloads using the two types as parameter types may violate the One Definition Rule. Since iterator is convertible to const_iterator, a single function with a const_iterator as parameter type will work instead.

Some advantages of flat set over other standard container adaptors are:

  • Potentially faster lookup (even though search operations have logarithmic complexity).
  • Much faster iteration: random access iterators instead of bidirectional iterators.
  • Less memory consumption for small objects (and for big objects if KeyContainer::shrink_to_fit() is available).
  • Better cache performance (depending on KeyContainer, keys are stored in a contiguous block(s) of memory).

Some disadvantages of flat set are:

  • Non-stable iterators (iterators are invalidated when inserting and erasing elements).
  • Non-copyable and non-movable type values can not be stored.
  • Weaker exception safety (copy/move constructors can throw when shifting values in erasures and insertions).
  • Slower (i.e. linear) insertion and erasure, especially for non-movable types.

Feature-test macro

  • __cpp_lib_flat_set β€” value 202207L, std (C++23), feature std::flat_set and std::flat_multiset.

🎨 Example 🎨

This section is incomplete Reason: no example

πŸ”­ See also πŸ”­

  • flat_multiset β€” adapts a container to provide a collection of keys, sorted by keys; (C++23) (class template)
  • set β€” collection of unique keys, sorted by keys; (class template)
  • unordered_set β€” collection of unique keys, hashed by keys; (C++11) (class template)

πŸ—‚οΈ Categories πŸ—‚οΈ

  • Todo without reason
  • Todo no example

okay that's the whole manual page, bestie 🫢✨ source/version/date shoutout: http://cppreference.com, 2024.06.10, page std::flat_set(3). periodt πŸ’…

πŸ“¦ where this page comes from