std::flat_set
<flat_set>
class Key,
class Compare = std::less <Key>,
class KeyContainer = std::vector <Key>
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.
std::flat_set
are constexpr: it is possible to create and use std::flat_set
objects in the evaluation of a constant expression.However, std::flat_set
objects generally cannot be constexpr, because any dynamically allocated storage must be released in the same evaluation of constant expression.
Contents
[edit] Iterator invalidation
[edit] Template parameters
Key
is not the same type as KeyContainer::value_type
.
random_access_iterator
.
The standard containers std::vector and std::deque satisfy these requirements.
[edit] Member types
iterator
implementation-defined LegacyRandomAccessIterator , ConstexprIterator (since C++26) and random_access_iterator
to value_type
[edit]
const_iterator
implementation-defined LegacyRandomAccessIterator , ConstexprIterator (since C++26) and random_access_iterator
to const value_type[edit]
[edit] Member objects
container_type
c
(private)
the adapted container(exposition-only member object*)
key_compare
compare
(private)
the comparison function object(exposition-only member object*)
[edit] Member functions
(public member function)
Iterators
Capacity
Modifiers
Lookup
(public member function) [edit]
Observers
value_type
(public member function) [edit]
[edit] Non-member functions
[edit] Helper classes
[edit] Tags
[edit] Deduction guides
[edit] 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 | Value | Std | Feature |
---|---|---|---|
__cpp_lib_flat_set |
202207L |
(C++23) | std::flat_set and std::flat_multiset
|
__cpp_lib_constexpr_flat_set |
202502L |
(C++26) | constexpr std::flat_set
|
[edit] Example
Reason: no example