std::flat_set - cppreference.com

From cppreference.com

template<classKey,classCompare=std::less<Key>,classKeyContainer=std::vector<Key>>classflat_set;(since C++23)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.

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

[edit]

key_typeKey

[edit]

value_typeKey

[edit]

key_compareCompare

[edit]

value_compareCompare

[edit]

referencevalue_type&

[edit]

const_referenceconstvalue_type&

[edit]

size_typetypenameKeyContainer::size_type

[edit]

difference_typetypenameKeyContainer::difference_type

[edit]

iteratorimplementation-defined

LegacyRandomAccessIterator

,

ConstexprIterator

(since C++26) and

random_access_iterator

to value_type

[edit]

const_iteratorimplementation-defined

LegacyRandomAccessIterator

,

ConstexprIterator

(since C++26) and

random_access_iterator

to constvalue_type

[edit]

reverse_iteratorstd::reverse_iterator<iterator>

[edit]

const_reverse_iteratorstd::reverse_iterator<const_iterator>

[edit]

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

(constructor)

constructs the flat_set
(public member function)

[edit]

(destructor)

(implicitly declared)

destroys every element of the container adaptor
(public member function)

operator=

assigns values to the container adaptor
(public member function)

[edit]

Iterators

begincbegin

returns an iterator to the beginning
(public member function)

[edit]

endcend

returns an iterator to the end
(public member function)

[edit]

rbegincrbegin

returns a reverse iterator to the beginning
(public member function)

[edit]

rendcrend

returns a reverse iterator to the end
(public member function)

[edit]

Capacity

empty

checks whether the container adaptor is empty
(public member function)

[edit]

size

returns the number of elements
(public member function)

[edit]

max_size

returns the maximum possible number of elements
(public member function)

[edit]

Modifiers

emplace

constructs element in-place
(public member function)

[edit]

emplace_hint

constructs elements in-place using a hint
(public member function)

[edit]

insert

inserts elements
(public member function)

[edit]

insert_range

inserts a range of elements
(public member function)

[edit]

extract

extracts the underlying container
(public member function)

[edit]

replace

replaces the underlying container
(public member function)

[edit]

erase

erases elements
(public member function)

[edit]

swap

swaps the contents
(public member function)

[edit]

clear

clears the contents
(public member function)

[edit]

Lookup

find

finds element with specific key
(public member function)

[edit]

count

returns the number of elements matching specific key
(public member function)

[edit]

contains

checks if the container contains element with specific key
(public member function)

[edit]

lower_bound

returns an iterator to the first element not less than the given key
(public member function)

[edit]

upper_bound

returns an iterator to the first element greater than the given key
(public member function)

[edit]

equal_range

returns range of elements matching a specific key
(public member function)

[edit]

Observers

key_comp

returns the function that compares keys
(public member function)

[edit]

value_comp

returns the function that compares keys in objects of type value_type
(public member function)

[edit]

Non-member functions

Helper classes

Tags

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

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 ValueStdFeature

__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_setExample

See also

(C++23)

adapts a container to provide a collection of keys, sorted by keys
(class template)

[edit]

collection of unique keys, sorted by keys
(class template)

[edit]

(C++11)

collection of unique keys, hashed by keys
(class template)

[edit]

Categories

:

Pages using deprecated source tags

Pages using deprecated enclose attributes

Todo without reason

Todo no example