From cppreference.com
template<classKey,classCompare=std::less<Key>,classKeyContainer=std::vector<Key>>classflat_set;(since C++23)The flat set is a
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
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
,
,
optional container requirements
, and all requirements of
(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
's operations that use unique keys.
All member functions of std::flat_set are constexpr: it is possible to create and use std::flat_set objects in the evaluation of a constant expression.
However, defining a constexprstd::flat_set variable is generally an error, because constant evaluation requires any dynamically allocated storage to be released in the same evaluation, which is usually not the case with std::flat_set's initializer.
(since C++26)Iterator invalidation
Template parameters
Member types
Type Definition container_typeKeyContainer
key_typeKey
value_typeKey
key_compareCompare
value_compareCompare
referencevalue_type&
const_referenceconstvalue_type&
size_typetypenameKeyContainer::size_type
difference_typetypenameKeyContainer::difference_type
iteratorimplementation-defined
,
(since C++26) and
to value_type
const_iteratorimplementation-defined
,
(since C++26) and
to constvalue_type
reverse_iteratorstd::reverse_iterator<iterator>
const_reverse_iteratorstd::reverse_iterator<const_iterator>
Member objects
Member Description container_typec(private)the adapted container
(exposition-only member object*)key_comparecompare(private)the comparison function object
(exposition-only member object*)Member functions
constructs the flat_set
(public member function)
(destructor)
(implicitly declared)
destroys every element of the container adaptor
(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
Helper classes
Tags
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
. 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
are:
Potentially faster lookup (even though search operations have logarithmic complexity).
Much faster iteration:
instead of
.
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.
macro ValueStdFeature
(C++23)std::flat_set and std::flat_multiset
(C++26)Constexpr std::flat_setExample
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)
:
Pages using deprecated source tags