std::flat_map - cppreference.com

template<classKey,classT,classCompare=std::less<Key>,classKeyContainer=std::vector<Key>,classMappedContainer=std::vector<T>>classflat_map;(since C++23)The flat map is a

container adaptor

that gives the functionality of an associative container that contains key-value pairs with unique keys. Keys are sorted by using the comparison function Compare.

The class template flat_map acts as a wrapper to the two underlying containers, passed as objects of type KeyContainer and MappedContainer 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 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_map 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 map supports most

AssociativeContainer

's operations that use unique keys.

All member functions of std::flat_map are constexpr: it is possible to create and use std::flat_map objects in the evaluation of a constant expression.

However, defining a constexprstd::flat_map 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_map's initializer.

(since C++26)Iterator invalidation

Template parameters

Key - The type of the keys. The program is ill-formed if Key is not the same type as KeyContainer::value_type. T - The type of mapped values. The program is ill-formed if T is not the same type as MappedContainer::value_type. Compare - A

Compare

type providing a strict weak ordering. KeyContainer
MappedContainer - The types of the underlying

SequenceContainer

to store keys and mapped values correspondingly. The iterators of such containers should satisfy

LegacyRandomAccessIterator

or model

random_access_iterator

. Invocations of their member functions size and max_size should not exit via an exception. The standard containers

std::vector

and

std::deque

satisfy these requirements.

Member types

Type Definition key_container_typeKeyContainer

[edit]

mapped_container_typeMappedContainer

[edit]

key_typeKey

[edit]

mapped_typeT

[edit]

value_typestd::pair<key_type,mapped_type>

[edit]

key_compareCompare

[edit]

referencestd::pair<constkey_type&,mapped_type&>

[edit]

const_referencestd::pair<constkey_type&,constmapped_type&>

[edit]

size_type

std::size_t

[edit]

difference_type

std::ptrdiff_t

[edit]

iteratorimplementation-defined

LegacyInputIterator

,

ConstexprIterator

(since C++26) and

random_access_iterator

to value_type

[edit]

const_iteratorimplementation-defined

LegacyInputIterator

,

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]

containerstype describing the underlying containers structcontainers{key_container_typekeys;mapped_container_typevalues;};

[edit]

Member classes

Member objects

Member Description containersc(private)the adapted containers
(exposition-only member object*)key_comparecompare(private)the comparison function object
(exposition-only member object*)Member functions

(constructor)

constructs the flat_map
(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]

Element access

at

access specified element with bounds checking
(public member function)

[edit]

operator[]

access or insert specified element
(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]

try_emplace

inserts in-place if the key does not exist, does nothing if the key exists
(public member function)

[edit]

insert

inserts elements
(public member function)

[edit]

insert_range

inserts a range of elements
(public member function)

[edit]

insert_or_assign

inserts an element or assigns to the current element if the key already exists
(public member function)

[edit]

extract

extracts the underlying containers
(public member function)

[edit]

replace

replaces the underlying containers
(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]

keys

direct access to the underlying keys container
(public member function)

[edit]

values

direct access to the underlying values container
(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.

Feature-test

macro ValueStdFeature

__cpp_lib_flat_map

202207L

(C++23)std::flat_map and std::flat_multimap

__cpp_lib_constexpr_flat_map

202502L

(C++26)Constexpr std::flat_mapExample

See also

(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]