You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Maps are associative containers that store elements formed by a
combination of a key value and a mapped value, following a specific
order.
In a map, the key values are generally used to sort and uniquely
identify the elements, while the mapped values store the content
associated to this key.
The types of key and mapped value may differ, and are grouped together in
member type value_type, which is a pair type combining both:
typedef pair<const Key, T> value_type;.
Different mapped values must have different key values.
Internally, the elements in a map are always sorted by its key
following a specific strict weak ordering criterion indicated by its internal
comparison object (of type Compare).
Map containers are generally slower than unordered_map containers to
access individual elements by their key, but they allow the direct
iteration on subsets based on their order.
The mapped values in a map can be accessed directly by their corresponding
key using the bracket operator ((operator[]).
Maps are typically implemented as red–black trees.
// Default constructor. Constructs an empty container.
std::map< KType, VType, Compare > map_name;
// Copy-constructs the temporary `Compare` class object. Constructs an empty container.
std::map< KType, VType, Compare > rmap_name( Compare( true ) );
// Copy-constructs the comparison functor `comp` with the contents of compare. Constructs an empty container.
Compare comp;
std::map< KType, VType, Compare > map_name( comp );
Compare: A Compare type providing a strict weak ordering. By default, the
first key (smallest key) is at the beginning of the map because its default
Compare is std::less< Key >, which sorts the elements in ascending order.
Allocator: An allocator that is used to acquire/release memory and to
construct/destroy the elements in that memory.
Member Types
key_type: Key.
mapped_type: T.
value_type: std::pair< const Key, T >.
size_type: Unsigned integer type (usually std::size_t).
difference_type: Signed integer type (usually std::ptrdiff_t).
instantiated with template arguments iterator and node_type.
Member Classes
value_compare: Compares objects of type value_type (class).
Member Functions
(constructor): Constructs the map (public member function).
(destructor): Destructs the map (public member function).
operator=: Assigns values to the container (public member function).
get_allocator: Returns the associated allocator (public member function).
at: Access specified element with bounds checking (public member function).
operator[]: Access or insert specified element (public member function).
begin, cbegin: Returns an iterator to the beginning (public member
function).
end, cend: Returns an iterator to the end (public member function).
rbegin, crbegin: Returns a reverse iterator to the beginning (public
member function).
rend, crend: Returns a reverse iterator to the end (public member
function).
empty: Checks whether the container is empty (public member function).
size: Returns the number of elements (public member function).
max_size: Returns the maximum possible number of elements (public member
function).
clear: Clears the contents (public member function).
insert: Inserts elements or nodes(since C++17) (public member function).
insert_range (C++23): Inserts a range of elements (public member
function).
insert_or_assign (C++17): Inserts an element or assigns to the current
element if the key already exists (public member function).
emplace: Constructs elements in-place (public member function).
emplace_hint: Constructs elements in-place using a hint (public member
function).
try_emplace (C++17): Inserts in-place if the key does not exist, does
nothing if the key exists (public member function).
erase: Erases elements and returns a valid iterator (public member
function).
swap: Swaps the contents (public member function).
extract (C++17): Extracts nodes from the container (public member
function).
merge (C++17): Splices nodes from another container (public member
function).
count: Returns the number of elements matching specific key (public member
function).
find: Finds an element with specific key (public member function).
contains (C++20): Checks if the container contains an element with
specific key (public member function).
equal_range: Returns range of elements matching a specific key (public
member function).
lower_bound: Returns an iterator to the first element not less than the
given key (public member function).
upper_bound: Returns an iterator to the first element greater than the
given key (public member function).
key_comp: Returns the function that compares keys (public member
function).
value_comp: Returns the function that compares keys in objects of type
value_type (public member function).
Non-member Functions
operator==, operator!=/</<=/>/>= (removed in C++20), operator<=>
(C++20): Lexicographically compares the values of two maps (function
template).
std::swap( std::map ): Specializes the std::swap algorithm (function
template).
erase_if( std::map ) (C++20): Erases all elements satisfying specific
criteria (function template).
Multimaps
Explanation
Multimaps are associative containers that store elements formed by
a combination of a key value and a mapped value, following a specific
order, and where multiple elements can have equivalent keys.
In a multimap, the key values are generally used to sort and uniquely
identify the elements, while the mapped values store the content
associated to this key.
The types of key and mapped value may differ, and are grouped together in
member type value_type, which is a pair type combining both:
typedef pair<const Key, T> value_type;.
Internally, the elements in a multimap are always sorted by its key
following a specific strict weak ordering criterion indicated by its internal
comparison object (of type Compare).
Multimap containers are generally slower than unordered_multimap
containers to access individual elements by their key, but they
allow the direct iteration on subsets based on their order.
Multimaps are typically implemented as red–black trees.
// Default constructor. Constructs an empty container.
std::multimap< KType, VType, Compare > mmap_name;
// Copy-constructs the temporary `Compare` class object. Constructs an empty container.
std::multimap< KType, VType, Compare > rmmap_name( Compare( true ) );
// Copy-constructs the comparison functor `comp` with the contents of compare. Constructs an empty container.
Compare comp;
std::multimap< KType, VType, Compare > mmap_name( comp );
Compare: A Compare type providing a strict weak ordering. By default, the
first key (smallest key) is at the beginning of the multimap because its
default Compare is std::less< Key >, which sorts the elements in
ascending order.
Allocator: An allocator that is used to acquire/release memory and to
construct/destroy the elements in that memory.
Member Types
key_type: Key.
mapped_type: T.
value_type: std::pair< const Key, T >.
size_type: Unsigned integer type (usually std::size_t).
difference_type: Signed integer type (usually std::ptrdiff_t).
node_type (since C++17): A specialization of node handle representing a
container node.
Member Classes
value_compare: Compares objects of type value_type (class).
Member Functions
(constructor): Constructs the multimap (public member function).
(destructor): Destructs the multimap (public member function).
operator=: Assigns values to the container (public member function).
get_allocator: Returns the associated allocator (public member function).
begin, cbegin: Returns an iterator to the beginning (public member
function).
end, cend: Returns an iterator to the end (public member function).
rbegin, crbegin: Returns a reverse iterator to the beginning (public
member function).
rend, crend: Returns a reverse iterator to the end (public member
function).
empty: Checks whether the container is empty (public member function).
size: Returns the number of elements (public member function).
max_size: Returns the maximum possible number of elements (public member
function).
clear: Clears the contents (public member function).
insert: Inserts elements or nodes(since C++17) (public member function).
insert_range (C++23): Inserts a range of elements (public member
function).
emplace: Constructs elements in-place (public member function).
emplace_hint: Constructs elements in-place using a hint (public member
function).
erase: Erases elements and returns a valid iterator (public member
function).
swap: Swaps the contents (public member function).
extract (C++17): Extracts nodes from the container (public member
function).
merge (C++17): Splices nodes from another container (public member
function).
count: Returns the number of elements matching specific key (public member
function).
find: Finds an element with specific key (public member function).
contains (C++20): Checks if the container contains an element with
specific key (public member function).
equal_range: Returns range of elements matching a specific key (public
member function).
lower_bound: Returns an iterator to the first element not less than the
given key (public member function).
upper_bound: Returns an iterator to the first element greater than the
given key (public member function).
key_comp: Returns the function that compares keys (public member
function).
value_comp: Returns the function that compares keys in objects of type
value_type (public member function).
Non-member Functions
operator==, operator!=/</<=/>/>= (removed in C++20), operator<=>
(C++20): Lexicographically compares the values of two multimaps (function
template).
std::swap( std::multimap ): Specializes the std::swap algorithm (function
template).
erase_if( std::multimap ) (C++20): Erases all elements satisfying specific
criteria (function template).
Unordered Maps
Explanation
Unordered maps are associative containers that store elements formed
by the combination of a key value and a mapped value, and which allows
for fast retrieval of individual elements based on their keys.
In an unordered_map, the key value is generally used to uniquely
identify the element, while the mapped value is an object with the
content associated to this key. Types of key and mapped value may differ.
Internally, the elements in the unordered_map are not sorted in any
particular order with respect to either their key or mapped values, but
organized into buckets depending on their hash values to allow for
fast access to individual elements directly by their key values
(with a constant average time complexity on average).
unordered_map containers are faster than map containers to access
individual elements by their key, although they are generally less
efficient for range iteration through a subset of their elements.
Unordered maps implement the direct access operator (operator[]) which
allows for direct access of the mapped value using its key value as argument.
Iterators in the container are at least forward iterators.
Their header file is <unordered_map>.
Declaration Syntax
std::unordered_map< KType, VType > umap_name;
// Not common, not recommendstructHash {
std::size_toperator()( const KType& obj ) const {
// This is only an example.return std::hash< SubKType1 >()( obj._mem1 )
^ std::hash< SubKType2 >()( obj._mem2 );
};
};
structKeyEqual {
booloperator()( const KType& lhs, const KType& rhs ) const {
// This is only an example.return lhs.id == rhs.id; // Custom equality based on id only
};
};
std::unordered_map< KType, VType, Hash, KeyEqual > umap_name;
Hash: A unary function object type that takes an object of the same type as
the elements as argument and returns a unique value of type size_t based on
it. This can either be a class implementing a function call operator or a
pointer to a function (see constructor for an example). This defaults to
std::hash< Key >.
KeyEqual: A binary predicate that takes two arguments of the same type as
the elements and returns a bool. The expression KeyEqual( a,b ), where
KeyEqual is an object of this type and a and b are key values, shall
return true if a is to be considered equivalent to b.
Allocator: An allocator that is used to acquire/release memory and to
construct/destroy the elements in that memory.
Member Types
key_type: Key.
mapped_type: T.
value_type: std::pair< const Key, T >.
size_type: Unsigned integer type (usually std::size_t).
difference_type: Signed integer type (usually std::ptrdiff_t).
iterator: Constant LegacyForwardIterator to value_type.
const_iterator: LegacyForwardIterator to const value_type.
local_iterator: An iterator type whose category, value, difference,
pointer and reference types are the same as iterator. This iterator can be
used to iterate through a single bucket but not across buckets
const_local_iterator: An iterator type whose category, value, difference,
pointer and reference types are the same as const_iterator. This iterator
can be used to iterate through a single bucket but not across buckets
node_type (since C++17): A specialization of node handle representing a
container node.
insert_return_type (since C++17): Type describing the result of inserting
a node_type, a specialization of
instantiated with template arguments iterator and node_type.
Member Functions
(constructor): Constructs the unordered map (public member function).
(destructor): Destructs the unordered map (public member function).
operator=: Assigns values to the container (public member function).
get_allocator: Returns the associated allocator (public member function).
begin, cbegin: Returns an iterator to the beginning (public member
function).
end, cend: Returns an iterator to the end (public member function).
empty: Checks whether the container is empty (public member function).
size: Returns the number of elements (public member function).
max_size: Returns the maximum possible number of elements (public member
function).
clear: Clears the contents (public member function).
insert: Inserts elements or nodes(since C++17) (public member function).
insert_range (C++23): Inserts a range of elements (public member
function).
insert_or_assign (C++17): Inserts an element or assigns to the current
element if the key
emplace: Constructs elements in-place (public member function).
emplace_hint: Constructs elements in-place using a hint (public member
function).
try_emplace (C++17): Inserts in-place if the key does not exist, does
nothing if the key
erase: Erases elements and returns a valid iterator (public member
function).
swap: Swaps the contents (public member function).
extract (C++17): Extracts nodes from the container (public member
function).
merge (C++17): Splices nodes from another container (public member
function).
at: Access specified element with bounds checking (public member
function).
operator[]: Access or insert specified element (public member function).
count: Returns the number of elements matching specific key (public member
function).
find: Finds an element with specific key (public member function).
contains (C++20): Checks if the container contains an element with
specific key (public member function).
equal_range: Returns range of elements matching a specific key (public
member function).
begin( size_type ), cbegin( size_type ): Returns an iterator to the
beginning of the specified bucket (public member function).
end( size_type ), cend( size_type ): Returns an iterator to the end of
the specified bucket (public member function).
bucket_count: Returns the number of buckets (public member function).
max_bucket_count: Returns the maximum number of buckets (public member
function).
bucket_size: Returns the number of elements in specific bucket (public
member function).
bucket: Returns the bucket for specific key (public member function).
load_factor: Returns average number of elements per bucket (public member
function).
max_load_factor: Manages maximum average number of elements per bucket
(public member function).
rehash: Reserves at least the specified number of buckets and regenerates
the hash table (public member function).
reserve: Reserves space for at least the specified number of elements and
regenerates the hash table (public member function).
hash_function: Returns function used to hash the keys (public member
function).
key_eq: Returns the function used to compare keys for equality (public
member function).
Non-member Functions
operator==, operator!= (removed in C++20): Lexicographically compares the
values of two unordered maps (function template).
std::swap( std::unordered_map ): Specializes the std::swap algorithm
(function template).
erase_if( std::unordered_map ) (C++20): Erases all elements satisfying
specific criteria (function template).
Unordered Multimaps
Explanation
Unordered multimaps are associative containers that store elements
formed by the combination of a key value and a mapped value, much like
unordered_map containers, but allowing different elements to have
equivalent keys.
In an unordered_multimap, the key value is generally used to uniquely
identify the element, while the mapped value is an object with the
content associated to this key. Types of key and mapped value may differ.
Internally, the elements in the unordered_multimap are not sorted in
any particular order with respect to either their key or mapped values, but
organized into buckets depending on their hash values to allow for
fast access to individual elements directly by their key values
(with a constant average time complexity on average).
Elements with equivalent keys are grouped together in the same bucket and
in such a way that an iterator (see equal_range) can iterate through all of
them.
Iterators in the container are at least forward iterators.
// Not common, not recommendstructHash {
std::size_toperator()( const KType& obj ) const {
// This is only an example.return std::hash< SubKType1 >()( obj._mem1 )
^ std::hash< SubKType2 >()( obj._mem2 );
};
};
structKeyEqual {
booloperator()( const KType& lhs, const KType& rhs ) const {
// This is only an example.return lhs.id == rhs.id; // Custom equality based on id only
};
};
std::unordered_multimap< KType, VType, Hash, KeyEqual > ummap_name;
Hash: A unary function object type that takes an object of the same type as
the elements as argument and returns a unique value of type size_t based on
it. This can either be a class implementing a function call operator or a
pointer to a function (see constructor for an example). This defaults to
std::hash< Key >.
KeyEqual: A binary predicate that takes two arguments of the same type as
the elements and returns a bool. The expression KeyEqual( a,b ), where
KeyEqual is an object of this type and a and b are key values, shall
return true if a is to be considered equivalent to b.
Allocator: An allocator that is used to acquire/release memory and to
construct/destroy the elements in that memory.
Member Types
key_type: Key.
mapped_type: T.
value_type: std::pair< const Key, T >.
size_type: Unsigned integer type (usually std::size_t).
difference_type: Signed integer type (usually std::ptrdiff_t).
iterator: Constant LegacyForwardIterator to value_type.
const_iterator: LegacyForwardIterator to const value_type.
local_iterator: An iterator type whose category, value, difference,
pointer and reference types are the same as iterator. This iterator can be
used to iterate through a single bucket but not across buckets
const_local_iterator: An iterator type whose category, value, difference,
pointer and reference types are the same as const_iterator. This iterator
can be used to iterate through a single bucket but not across buckets
node_type (since C++17): A specialization of node handle representing a
container node.
Member Functions
(constructor): Constructs the unordered multimap (public member function).
(destructor): Destructs the unordered multimap (public member function).
operator=: Assigns values to the container (public member function).
get_allocator: Returns the associated allocator (public member function).
begin, cbegin: Returns an iterator to the beginning (public member
function).
end, cend: Returns an iterator to the end (public member function).
empty: Checks whether the container is empty (public member function).
size: Returns the number of elements (public member function).
max_size: Returns the maximum possible number of elements (public member
function).
clear: Clears the contents (public member function).
insert: Inserts elements or nodes(since C++17) (public member function).
insert_range (C++23): Inserts a range of elements (public member
function).
emplace: Constructs elements in-place (public member function).
emplace_hint: Constructs elements in-place using a hint (public member
function).
erase: Erases elements and returns a valid iterator (public member
function).
swap: Swaps the contents (public member function).
extract (C++17): Extracts nodes from the container (public member
function).
merge (C++17): Splices nodes from another container (public member
function).
count: Returns the number of elements matching specific key (public member
function).
find: Finds an element with specific key (public member function).
contains (C++20): Checks if the container contains an element with
specific key (public member function).
equal_range: Returns range of elements matching a specific key (public
member function).
begin( size_type ), cbegin( size_type ): Returns an iterator to the
beginning of the specified bucket (public member function).
end( size_type ), cend( size_type ): Returns an iterator to the end of
the specified bucket (public member function).
bucket_count: Returns the number of buckets (public member function).
max_bucket_count: Returns the maximum number of buckets (public member
function).
bucket_size: Returns the number of elements in specific bucket (public
member function).
bucket: Returns the bucket for specific key (public member function).
load_factor: Returns average number of elements per bucket (public member
function).
max_load_factor: Manages maximum average number of elements per bucket
(public member function).
rehash: Reserves at least the specified number of buckets and regenerates
the hash table (public member function).
reserve: Reserves space for at least the specified number of elements and
regenerates the hash table (public member function).
hash_function: Returns function used to hash the keys (public member
function).
key_eq: Returns the function used to compare keys for equality (public
member function).
Non-member Functions
operator==, operator!= (removed in C++20): Lexicographically compares the
values of two unordered multimaps (function template).
std::swap( std::unordered_multimap ): Specializes the std::swap algorithm
(function template).
erase_if( std::unordered_multimap ) (C++20): Erases all elements satisfying
specific criteria (function template).
Flat Maps
Explanation
Flat maps are an associative container that stores elements formed by
a combination of a key value and a mapped value, but is implemented as
a sorted sequence, typically using a vector.
In a flat_map, the key values are generally used to sort and uniquely
identify the elements, while the mapped values store the content
associated with this key, similar to traditional maps.
The types of key and mapped value may differ and are grouped together in
the member type value_type, which is a pair type combining both:
typedef std::pair<const Key, T> value_type;.
Different mapped values must have different key values, ensuring that
each key in a flat_map uniquely identifies a single mapped value.
Internally, the elements in a flat_map are stored as a sorted vector,
which allows for binary search for efficient lookups and iteration
based on the key’s order.
flat_map containers can be faster than traditional maps for access to
individual elements by their key due to contiguous memory storage,
but they may incur overhead for insertions and deletions due to the
need to maintain sorted order.
The mapped values in a flat_map can be accessed directly by their
corresponding key using the bracket operator (operator[]), similar to
regular maps.
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.
The header file for flat_map is <experimental/flat_map> (or may be found
in other namespaces in different implementations).
// Constructs the two underlying containers by copying the contents of the container `kcont` and `vcont` separately.// Construct a default `comp` to sort all elements.
KeyContainer< KType > kcont = { ... };
MappedContainer< VType > vcont = { ... };
std::flat_map< KType, VType, Compare, KeyContainer< KType >, MappedContainer< VType > > fmap_name( kcont, vcont );
// Constructs the two underlying containers by copying the contents of the container `kcont` and `vcont` separately.// Copy the `comp` to sort all elements.
KeyContainer< KType > kcont = { ... };
MappedContainer< VType > vcont = { ... };
Compare comp;
std::flat_map< KType, VType, Compare, KeyContainer< KType >, MappedContainer< VType > > fmap_name( kcont, vcont, comp );
// Specify that all elements are unique. Just a tag.// Constructs the two underlying containers by copying the contents of the container `kcont` and `vcont` separately.// Construct a default `comp` to sort all elements.
std::sorted_unique_t s;
KeyContainer< KType > kcont = { ... };
MappedContainer< VType > vcont = { ... };
std::flat_map< KType, VType, Compare, KeyContainer< KType >, MappedContainer< VType > > fmap_name( s, kcont, vcont );
// Specify that all elements are unique. Just a tag.// Constructs the two underlying containers by copying the contents of the container `kcont` and `vcont` separately.// Copy the `comp` to sort all elements.
std::sorted_unique_t s:
KeyContainer< KType > kcont = { ... };
MappedContainer< VType > vcont = { ... };
Compare comp;
std::flat_map< KType, VType, Compare, KeyContainer< KType >, MappedContainer< VType > > fmap_name( s, kcont, vcont, comp );
// Copy-constructs the comparison functor `comp` with the contents of compare. Value-initializes the underlying container.
Compare comp;
std::flat_map< KType, VType, Compare, KeyContainer< KType >, MappedContainer< VType > > fmap_name( comp );
// Default constructor. Value-initializes the comparator and the underlying container.
std::flat_map< KType, VType > fmap_name1;
// Copy constructor.
std::flat_map< KType, VType > fmap_name2( fmap_name1 );
// Default constructor. Value-initializes the comparator and the underlying container.
std::flat_map< KType, VType > fmap_name1;
// Copy constructor.
std::flat_map< KType, VType > fmap_name2 = fmap_name1;
// Default constructor. Value-initializes the comparator and the underlying container.
std::flat_map< KType, VType > fmap_name1;
// Move constructor.
std::flat_map< KType, VType > fmap_name2( std::move( fmap_name1 ) );
// Specify that all elements are unique. Just a tag.// Constructs the two underlying containers by copying the contents of the container `kcont` and `vcont` separately.// Copy the `comp` to sort all elements.
std::sorted_unique_t s:
KeyContainer< KType > kcont = { ... };
MappedContainer< VType > vcont = { ... };
Compare comp;
std::flat_map< KType, VType, Compare, KeyContainer< KType >, MappedContainer< VType > > fmap_name1( s, kcont, vcont, comp );
// Constructs the container with the contents of the range `[first, last)`.
std::flat_map< KType, VType, Compare, KeyContainer< KType >, MappedContainer< VType > > fmap_name2( s /* optional */, fmap_name1.begin(), fmap_name1.end(), comp /* optional */ );
Compare: A Compare type providing a strict weak ordering. By default, the
first key (smallest key) is at the beginning of the flat set because its
default Compare is std::less< Key >, which sorts the elements in
ascending order.
KeyContainer, MappedContainer: The type of the underlying
SequenceContainer to store the elements. The iterators of such container
should satisfy LegacyRandomAccessIterator or model
random_access_iterator. The standard containers std::vector and
std::deque satisfy these requirements.
value_compare: Compares objects of type value_type (class).
Member Objects
c (private): The object of type containers (exposition-only member
object*).
compare (private): The comparison function object of type key_compare
(exposition-only member object*).
Member Functions
(constructor): Constructs the flat map (public member function).
(destructor) (implicitly declared): Destroys every element of the container
adaptor (public member function).
operator=: Assigns values to the container adaptor (public member
function).
at: Access specified element with bounds checking (public member function).
operator[]: Access or insert specified element (public member function).
begin, cbegin: Returns an iterator to the beginning (public member
function).
end, cend: Returns an iterator to the end (public member function).
rbegin, crbegin: Returns a reverse iterator to the beginning (public
member function).
rend, crend: Returns a reverse iterator to the end (public member
function).
empty: Checks whether the container adaptor is empty (public member
function).
size: Returns the number of elements (public member function).
max_size: Returns the maximum possible number of elements (public member
function).
emplace: Constructs elements in-place (public member function).
emplace_hint: Constructs elements in-place using a hint (public member
function).
try_emplace (C++17): Inserts in-place if the key does not exist, does
nothing if the key
insert: Inserts elements (public member function).
insert_range: Inserts a range of elements (public member function).
insert_or_assign (C++17): Inserts an element or assigns to the current
element if the key
extract: Extracts the underlying containers (public member function).
replace: Replaces the underlying containers (public member function).
erase: Erases elements and returns a valid iterator (public member
function).
swap: Swaps the contents (public member function).
clear: Clears the contents (public member function).
find: Finds an element with specific key (public member function).
count: Returns the number of elements matching specific key (public member
function).
contains: Checks if the container contains an element with specific key
(public member function).
lower_bound: Returns an iterator to the first element not less than the
given key (public member function).
upper_bound: Returns an iterator to the first element greater than the
given key (public member function).
equal_range: Returns range of elements matching a specific key (public
member function).
key_comp: Returns the function that compares keys (public member
function).
value_comp: Returns the function that compares keys in objects of type
value_type (public member function).
keys: Direct access to the underlying keys container (public member
function).
values: Direct access to the underlying values container (public member
function).
Non-member Functions
operator==, operator<=>: Lexicographically compares the values of two
flat maps (function template).
std::swap( std::flat_map ): Specializes the std::swap algorithm (function
template).
erase_if( std::flat_map ): Erases all elements satisfying specific criteria
(function template).
Helper classes
std::uses_allocator< std::flat_map > (C++23): Specializes the
std::uses_allocat or type trait (class template specialization).
Tags
sorted_unique, sorted_unique_t (C++23): indicates that elements of a
range are sorted and unique (tag).
Flat Multimaps
Explanation
Flat multimaps are an associative container that stores elements
formed by a combination of a key value and a mapped value, implemented
as a sorted sequence, typically using a vector.
In a flat_multimap, the key values can be associated with multiple mapped
values, allowing for duplicate keys, which distinguishes it from
flat_map.
The types of key and mapped value may differ and are grouped together in
the member type value_type, which is a pair type combining both:
typedef std::pair<const Key, T> value_type;.
Multiple mapped values can share the same key value, enabling the
storage of collections of values for each unique key, reflecting the nature
of multimaps.
Internally, the elements in a flat_multimap are stored as a sorted
vector, which allows for binary search for efficient lookups and
iteration based on the key’s order.
flat_multimap containers can be faster than traditional multimaps for
access to individual elements by their key due to contiguous memory
storage, but they may incur overhead for insertions and deletions due
to the need to maintain sorted order.
The mapped values in a flat_multimap can be accessed using iterators or
the equal_range method to retrieve all values corresponding to a
particular key.
flat_multimap is typically implemented using a sorted vector, allowing
for fast lookups while maintaining a compact memory footprint.
The header file for flat_multimap is <experimental/flat_map> (or may be
found in other namespaces in different implementations).
// Constructs the two underlying containers by copying the contents of the container `kcont` and `vcont` separately.// Construct a default `comp` to sort all elements.
KeyContainer< KType > kcont = { ... };
MappedContainer< VType > vcont = { ... };
std::flat_multimap< KType, VType, Compare, KeyContainer< KType >, MappedContainer< VType > > fmmap_name( kcont, vcont );
// Constructs the two underlying containers by copying the contents of the container `kcont` and `vcont` separately.// Copy the `comp` to sort all elements.
KeyContainer< KType > kcont = { ... };
MappedContainer< VType > vcont = { ... };
Compare comp;
std::flat_multimap< KType, VType, Compare, KeyContainer< KType >, MappedContainer< VType > > fmmap_name( kcont, vcont, comp );
// Allow different elements with the same value. Just a tag.// Constructs the two underlying containers by copying the contents of the container `kcont` and `vcont` separately.// Construct a default `comp` to sort all elements.
std::sorted_equivalent_t s;
KeyContainer< KType > kcont = { ... };
MappedContainer< VType > vcont = { ... };
std::flat_multimap< KType, VType, Compare, KeyContainer< KType >, MappedContainer< VType > > fmmap_name( s, kcont, vcont );
// Allow different elements with the same value. Just a tag.// Constructs the two underlying containers by copying the contents of the container `kcont` and `vcont` separately.// Copy the `comp` to sort all elements.
std::sorted_equivalent_t s:
KeyContainer< KType > kcont = { ... };
MappedContainer< VType > vcont = { ... };
Compare comp;
std::flat_multimap< KType, VType, Compare, KeyContainer< KType >, MappedContainer< VType > > fmmap_name( s, kcont, vcont, comp );
// Copy-constructs the comparison functor `comp` with the contents of compare. Value-initializes the underlying container.
Compare comp;
std::flat_multimap< KType, VType, Compare, KeyContainer< KType >, MappedContainer< VType > > fmmap_name( comp );
// Default constructor. Value-initializes the comparator and the underlying container.
std::flat_multimap< KType, VType > fmmap_name1;
// Copy constructor.
std::flat_multimap< KType, VType > fmmap_name2( fmmap_name1 );
// Default constructor. Value-initializes the comparator and the underlying container.
std::flat_multimap< KType, VType > fmmap_name1;
// Copy constructor.
std::flat_multimap< KType, VType > fmmap_name2 = fmmap_name1;
// Default constructor. Value-initializes the comparator and the underlying container.
std::flat_multimap< KType, VType > fmmap_name1;
// Move constructor.
std::flat_multimap< KType, VType > fmmap_name2( std::move( fmmap_name1 ) );
// Allow different elements with the same value. Just a tag.// Constructs the two underlying containers by copying the contents of the container `kcont` and `vcont` separately.// Copy the `comp` to sort all elements.
std::sorted_equivalent_t s;
KeyContainer< KType > kcont = { ... };
MappedContainer< VType > vcont = { ... };
Compare comp;
std::flat_multimap< KType, VType, Compare, KeyContainer< KType >, MappedContainer< VType > > fmmap_name1( s, kcont, vcont, comp );
// Constructs the container with the contents of the range `[first, last)`.
std::flat_multimap< KType, VType, Compare, KeyContainer< KType >, MappedContainer< VType > > fmmap_name2( s /* optional */, fmmap_name1.begin(), fmmap_name1.end(), comp /* optional */ );
Compare: A Compare type providing a strict weak ordering. By default, the
first key (smallest key) is at the beginning of the flat set because its
default Compare is std::less< Key >, which sorts the elements in
ascending order.
KeyContainer, MappedContainer: The type of the underlying
SequenceContainer to store the elements. The iterators of such container
should satisfy LegacyRandomAccessIterator or model
random_access_iterator. The standard containers std::vector and
std::deque satisfy these requirements.