On this page
std::flat_set
| Defined in header <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.
Iterator invalidation
Template parameters
| Key | - | The type of the stored elements. The program is ill-formed if Keyis not the same type asKeyContainer::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  | 
Member types
| Member type | Definition | 
|---|---|
| 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_iteratortovalue_type | 
| const_iterator | implementation-defined LegacyRandomAccessIterator and random_access_iteratorto const value_type | 
| reverse_iterator | std::reverse_iterator<iterator> | 
| const_reverse_iterator | std::reverse_iterator<const_iterator> | 
Member objects
| Member name | Definition | 
|---|---|
| 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
| constructs the flat_set(public member function) | |
| destructs the flat_set(public member function) | |
| assigns values to the container adaptor (public member function) | |
| Iterators | |
| returns an iterator to the beginning (public member function) | |
| returns an iterator to the end (public member function) | |
| returns a reverse iterator to the beginning (public member function) | |
| returns a reverse iterator to the end (public member function) | |
| Capacity | |
| checks whether the container adaptor is empty (public member function) | |
| returns the number of elements (public member function) | |
| returns the maximum possible number of elements (public member function) | |
| Modifiers | |
| constructs element in-place (public member function) | |
| constructs elements in-place using a hint (public member function) | |
| inserts elements (public member function) | |
| inserts a range of elements (public member function) | |
| extracts the underlying container (public member function) | |
| replaces the underlying container (public member function) | |
| erases elements (public member function) | |
| swaps the contents (public member function) | |
| clears the contents (public member function) | |
| Lookup | |
| finds element with specific key (public member function) | |
| returns the number of elements matching specific key (public member function) | |
| checks if the container contains element with specific key (public member function) | |
| returns an iterator to the first element not less than the given key (public member function) | |
| returns an iterator to the first element greater than the given key (public member function) | |
| returns range of elements matching a specific key (public member function) | |
| Observers | |
| returns the function that compares keys (public member function) | |
| returns the function that compares keys in objects of type value_type(public member function) | |
Non-member functions
| (C++23)
        | lexicographically compares the values of two flat_sets(function template) | 
| (C++23)
        | specializes the std::swapalgorithm(function template) | 
| (C++23)
        | erases all elements satisfying specific criteria (function template) | 
Helper classes
| (C++23)
        | specializes the std::uses_allocatortype trait(class template specialization) | 
Tags
| (C++23)
        | a tag used to indicate that elements of a container or range are sorted and unique (tag) | 
Deduction guides
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 associative containers 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 | Value | Std | Feature | 
|---|---|---|---|
| __cpp_lib_flat_set | 202207L | (C++23) | std::flat_setandstd::flat_multiset | 
Example
See also
| (C++23)
        | adapts a container to provide a collection of keys, sorted by keys (class template) | 
| collection of unique keys, sorted by keys (class template) | |
| (C++11)
        | collection of unique keys, hashed by keys (class template) | 
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
 https://en.cppreference.com/w/cpp/container/flat_set