(C++17) | ||||
Sequence | ||||
(C++11) | ||||
(C++26) | ||||
(C++26) | ||||
(C++11) | ||||
Associative | ||||
Unordered associative | ||||
(C++11) | ||||
(C++11) | ||||
(C++11) | ||||
(C++11) | ||||
Adaptors | ||||
(C++23) | ||||
(C++23) | ||||
flat_map (C++23) | ||||
(C++23) | ||||
Views | ||||
(C++20) | ||||
(C++23) | ||||
Tables | ||||
Iterator invalidation | ||||
Member function table | ||||
Non-member function table |
Defined in header <flat_map> | ||
template< class Key, | (since C++23) | |
The flat map is acontainer adaptor that gives the functionality of an associative container that contains key-value pairs with unique keys. Keys are sorted by using the comparison functionCompare
.
The class templateflat_map
acts as a wrapper to the two underlying containers, passed as objects of typeKeyContainer
andMappedContainer
respectively. The first container is sorted, and for each key its corresponding value is in the second container at the same index (offset). The number of elements in both containers is the same.
Everywhere the standard library uses theCompare requirements, uniqueness is determined by using the equivalence relation. Informally, two objectsa andb are considered equivalent if neither compares less than the other:!comp(a, b)&&!comp(b, a).
std::flat_map
meets the requirements ofContainer,ReversibleContainer,optional container requirements, and all requirements ofAssociativeContainer (including logarithmic search complexity), except that:
A flat map supports mostAssociativeContainer's operations that use unique keys.
All member functions ofstd::flat_map areconstexpr: it is possible to create and usestd::flat_map objects in the evaluation of a constant expression.However, | (since C++26) |
Contents |
This section is incomplete |
Key | - | The type of the keys. The program is ill-formed ifKey is not the same type asKeyContainer::value_type . |
T | - | The type of mapped values. The program is ill-formed ifT is not the same type asMappedContainer::value_type . |
Compare | - | ACompare type providing a strict weak ordering. |
KeyContainer MappedContainer | - | The types of the underlyingSequenceContainer to store keys and mapped values correspondingly. The iterators of such containers should satisfyLegacyRandomAccessIterator or modelrandom_access_iterator . Invocations of their member functionssize andmax_size should not exit via an exception.The standard containersstd::vector andstd::deque satisfy these requirements. |
Type | Definition |
key_container_type | KeyContainer [edit] |
mapped_container_type | MappedContainer [edit] |
key_type | Key [edit] |
mapped_type | T [edit] |
value_type | std::pair<key_type, mapped_type>[edit] |
key_compare | Compare [edit] |
reference | std::pair<const key_type&, mapped_type&>[edit] |
const_reference | std::pair<const key_type&,const mapped_type&>[edit] |
size_type | std::size_t[edit] |
difference_type | std::ptrdiff_t[edit] |
iterator | implementation-definedLegacyInputIterator,ConstexprIterator(since C++26) andrandom_access_iterator tovalue_type [edit] |
const_iterator | implementation-definedLegacyInputIterator,ConstexprIterator(since C++26) andrandom_access_iterator toconst value_type[edit] |
reverse_iterator | std::reverse_iterator<iterator>[edit] |
const_reverse_iterator | std::reverse_iterator<const_iterator>[edit] |
containers | type describing the underlying containers struct containers |
compares objects of typevalue_type (class)[edit] |
Member | Description |
containers c (private) | the adapted containers (exposition-only member object*) |
key_compare compare (private) | the comparison function object (exposition-only member object*) |
constructs theflat_map (public member function)[edit] | |
(destructor) (implicitly declared) | destroys every element of the container adaptor (public member function) |
assigns values to the container adaptor (public member function)[edit] | |
Element access | |
access specified element with bounds checking (public member function)[edit] | |
access or insert specified element (public member function)[edit] | |
Iterators | |
returns an iterator to the beginning (public member function)[edit] | |
returns an iterator to the end (public member function)[edit] | |
returns a reverse iterator to the beginning (public member function)[edit] | |
returns a reverse iterator to the end (public member function)[edit] | |
Capacity | |
checks whether the container adaptor is empty (public member function)[edit] | |
returns the number of elements (public member function)[edit] | |
returns the maximum possible number of elements (public member function)[edit] | |
Modifiers | |
constructs element in-place (public member function)[edit] | |
constructs elements in-place using a hint (public member function)[edit] | |
inserts in-place if the key does not exist, does nothing if the key exists (public member function)[edit] | |
inserts elements (public member function)[edit] | |
inserts a range of elements (public member function)[edit] | |
inserts an element or assigns to the current element if the key already exists (public member function)[edit] | |
extracts the underlying containers (public member function)[edit] | |
replaces the underlying containers (public member function)[edit] | |
erases elements (public member function)[edit] | |
swaps the contents (public member function)[edit] | |
clears the contents (public member function)[edit] | |
Lookup | |
finds element with specific key (public member function)[edit] | |
returns the number of elements matching specific key (public member function)[edit] | |
checks if the container contains element with specific key (public member function)[edit] | |
returns an iterator to the first elementnot less than the given key (public member function)[edit] | |
returns an iterator to the first elementgreater than the given key (public member function)[edit] | |
returns range of elements matching a specific key (public member function)[edit] | |
Observers | |
returns the function that compares keys (public member function)[edit] | |
returns the function that compares keys in objects of typevalue_type (public member function)[edit] | |
direct access to the underlying keys container (public member function)[edit] | |
direct access to the underlying values container (public member function)[edit] |
(C++23) | lexicographically compares the values of twoflat_map s(function template)[edit] |
(C++23) | specializes thestd::swap algorithm (function template)[edit] |
(C++23) | erases all elements satisfying specific criteria (function template)[edit] |
specializes thestd::uses_allocator type trait (class template specialization)[edit] |
(C++23) | indicates that elements of a range are sorted and unique (tag)[edit] |
The member typesiterator
andconst_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 theOne Definition Rule. Sinceiterator
is convertible toconst_iterator
, a single function with aconst_iterator
as parameter type will work instead.
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_flat_map | 202207L | (C++23) | std::flat_map andstd::flat_multimap |
__cpp_lib_constexpr_flat_map | 202502L | (C++26) | constexprstd::flat_map |
This section is incomplete Reason: no example |
(C++23) | adapts two containers to provide a collection of key-value pairs, sorted by keys (class template)[edit] |
collection of key-value pairs, sorted by keys, keys are unique (class template)[edit] | |
(C++11) | collection of key-value pairs, hashed by keys, keys are unique (class template)[edit] |