Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/container/flat map"

From cppreference.com
< cpp‎ | container
(I'd rather keep it redlinked until there is content, but okay, let's mark todo)
m (+page, early draft.)
Line 1: Line 1:
 
{{cpp/title|flat_map}}
 
{{cpp/title|flat_map}}
 
{{cpp/container/flat_map/navbar}}
 
{{cpp/container/flat_map/navbar}}
 +
{{ddcl|header=flat_map|num=1|1=
 +
template<
 +
    class Key,
 +
    class T,
 +
    class Compare = std::less<Key>,
 +
    class KeyContainer = std::vector<Key>,
 +
    class MappedContainer = std::vector<T>
 +
> class flat_map;
 +
}}
  
{{todo}}
+
The flat map is a [[cpp/container#Container adaptors|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 {{tt|Compare}}.
 +
 
 +
The class template {{tt|flat_map}} acts as a wrapper to the two underlying containers, passed as objects of type {{tt|KeyContainer}} and {{tt|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 {{named req|Compare}} requirements, uniqueness is determined by using the equivalence relation. Informally, two objects {{c|a}} and {{c|b}} are considered equivalent if neither compares less than the other: {{c|!comp(a, b) && !comp(b, a)}}.
 +
 
 +
{{tt|std::flat_map}} meets the requirements of {{named req|Container}}, {{named req|ReversibleContainer}}, optional container requirements<!--TODO: give a link to N4950.24.2.2.4-->, and all requirements of {{named req|AssociativeContainer}} (including logarithmic search complexity), except that:
 +
* requirements related to nodes are not applicable,
 +
* iterator invalidation requirements differ, <!--TODO: describe iterator/pointer invalidation properties, maybe per each function, in their pages -->
 +
* the complexity of insertion and erasure operations is linear.
 +
 
 +
A flat map supports most {{named req|AssociativeContainer}}'s operations that use unique keys.
 +
 
 +
===Iterator invalidation===
 +
{{todo}} <!-- TODO: see e.g. [[cpp/container/unordered_map#Iterator invalidation]] as a basis -->
 +
 
 +
===Template parameters===
 +
{{par begin}}
 +
{{par|Key|The type of the keys. The program is ill-formed if {{tt|Key}} is not the same type as {{tt|KeyContainer::value_type}}.}}
 +
{{par|T|The type of mapped values. The program is ill-formed if {{tt|T}} is not the same type as {{tt|MappedContainer::value_type}}.}}
 +
{{par|Compare|A {{named req|Compare}} type providing a strict weak ordering.
 +
{{par|KeyContainer<br>MappedContainer|The types of the underlying {{named req|SequenceContainer}} to store keys and mapped values correspondingly. The iterators of such containers should satisfy {{named req|RandomAccessIterator}} or model {{lconcept|random_access_iterator}}. Invocations of their member functions {{tt|size}} and {{tt|max_size}} should not exit via an exception.
 +
 
 +
The standard containers {{lc|std::vector}} and {{lc|std::deque}} satisfy these requirements.}}{{par end}}
 +
<!---->
 +
===Member types===
 +
{{dsc begin}}
 +
{{dsc hitem|Member type|Definition}}
 +
{{dsc inc|cpp/container/dsc key_container_type|flat_map}}
 +
{{dsc inc|cpp/container/dsc mapped_container_type|flat_map}}
 +
{{dsc inc|cpp/container/dsc key_type|flat_map}}
 +
{{dsc inc|cpp/container/dsc mapped_type|flat_map}}
 +
{{dsc inc|cpp/container/dsc value_type|flat_map}}
 +
{{dsc inc|cpp/container/dsc key_compare|flat_map}}
 +
{{dsc inc|cpp/container/dsc reference|flat_map}}
 +
{{dsc inc|cpp/container/dsc const_reference|flat_map}}
 +
{{dsc inc|cpp/container/dsc size_type|flat_map}}
 +
{{dsc inc|cpp/container/dsc difference_type|flat_map}}
 +
{{dsc inc|cpp/container/dsc iterator|flat_map}}
 +
{{dsc inc|cpp/container/dsc const_iterator|flat_map}}
 +
{{dsc inc|cpp/container/dsc reverse_iterator|flat_map}}
 +
{{dsc inc|cpp/container/dsc const_reverse_iterator|flat_map}}
 +
{{dsc inc|cpp/container/dsc containers|flat_map}}
 +
{{dsc end}}
 +
 
 +
===Member classes===
 +
{{dsc begin}}
 +
{{dsc inc|cpp/container/dsc value_compare|flat_map}}<!--
 +
{{dsc inc|cpp/container/dsc key_equiv|flat_map}}-->
 +
{{dsc end}}
 +
 
 +
===Member objects===
 +
{{dsc begin}}
 +
{{dsc hitem|Member name|Definition}}
 +
{{dsc expos mem obj|private=yes|c|the object of type {{tt|containers}}}}
 +
{{dsc expos mem obj|private=yes|compare|the comparison function object of type {{tt|key_compare}}}}
 +
{{dsc end}}
 +
 
 +
===Member functions===
 +
{{dsc begin}}
 +
{{dsc inc|cpp/container/dsc constructor|flat_map}}
 +
{{dsc inc|cpp/container/dsc destructor|flat_map}}<!--TODO: implicitly defined! -->
 +
{{dsc inc|cpp/container/dsc operator{{=}}|flat_map}}
 +
 
 +
{{dsc h2|Element access}}
 +
{{dsc inc|cpp/container/dsc at|flat_map}}
 +
{{dsc inc|cpp/container/dsc operator_at|flat_map}}
 +
 
 +
{{dsc h2|Iterators}}
 +
{{dsc inc|cpp/container/dsc begin|flat_map}}
 +
{{dsc inc|cpp/container/dsc end|flat_map}}
 +
{{dsc inc|cpp/container/dsc rbegin|flat_map}}
 +
{{dsc inc|cpp/container/dsc rend|flat_map}}
 +
 
 +
{{dsc h2|Capacity}}
 +
{{dsc inc|cpp/container/dsc empty|flat_map}}
 +
{{dsc inc|cpp/container/dsc size|flat_map}}
 +
{{dsc inc|cpp/container/dsc max_size|flat_map}}
 +
 
 +
{{dsc h2|Modifiers}}
 +
{{dsc inc|cpp/container/dsc emplace|flat_map}}
 +
{{dsc inc|cpp/container/dsc emplace_hint|flat_map}}
 +
{{dsc inc|cpp/container/dsc try_emplace|flat_map}}
 +
{{dsc inc|cpp/container/dsc insert|flat_map}}
 +
{{dsc inc|cpp/container/dsc insert_range|flat_map}}
 +
{{dsc inc|cpp/container/dsc insert_or_assign|flat_map}}
 +
{{dsc inc|cpp/container/dsc extract|flat_map}}
 +
{{dsc inc|cpp/container/dsc replace|flat_map}}
 +
{{dsc inc|cpp/container/dsc erase|flat_map}}
 +
{{dsc inc|cpp/container/dsc swap|flat_map}}
 +
{{dsc inc|cpp/container/dsc clear|flat_map}}
 +
 
 +
{{dsc h2|Lookup}}
 +
{{dsc inc|cpp/container/dsc find|flat_map}}
 +
{{dsc inc|cpp/container/dsc count|flat_map}}
 +
{{dsc inc|cpp/container/dsc contains|flat_map}}
 +
{{dsc inc|cpp/container/dsc lower_bound|flat_map}}
 +
{{dsc inc|cpp/container/dsc upper_bound|flat_map}}
 +
{{dsc inc|cpp/container/dsc equal_range|flat_map}}
 +
 
 +
{{dsc h2|Observers}}
 +
{{dsc inc|cpp/container/dsc key_comp|flat_map}}
 +
{{dsc inc|cpp/container/dsc value_comp|flat_map}}
 +
{{dsc inc|cpp/container/dsc keys|flat_map}}
 +
{{dsc inc|cpp/container/dsc values|flat_map}}
 +
{{dsc end}}
 +
 
 +
===Non-member functions===
 +
{{dsc begin}}
 +
{{dsc inc|cpp/container/dsc operator_cmp|flat_map}}
 +
{{dsc inc|cpp/container/dsc swap2|flat_map}}
 +
{{dsc inc|cpp/container/dsc erase_if|flat_map}}
 +
{{dsc end}}
 +
 
 +
===Helper classes===
 +
{{dsc begin}}
 +
{{dsc inc|cpp/container/dsc uses_allocator|flat_map}}
 +
{{dsc end}}
 +
 
 +
===Tags===
 +
{{dsc begin}}
 +
{{dsc inc|cpp/container/dsc sorted_unique}}
 +
{{dsc inc|cpp/container/dsc sorted_equivalent}}
 +
{{dsc end}}
 +
 
 +
==={{rl|deduction guides|Deduction guides}}===
 +
 
 +
===Notes===
 +
{{cpp/container/assoc_note}}<!-- TODO: is this relevant to flat_map? -->
 +
 
 +
<!-- TODO: maybe this lyrics is unnecessary:
 +
Some advantages of flat map over other standard [[associative containers]] are:
 +
* Potentially faster lookup (even though search operations have logarithmic complexity).
 +
* Much faster iteration: {{lt|cpp/iterator/random access iterator}}s instead of {{lt|cpp/iterator/bidirectional iterator}}s.
 +
* Less memory consumption for small objects (and for big objects if {{c|KeyContainer::shrink_to_fit()}} is available).
 +
* Better cache performance (depending on {{tt|KeyContainer}}, keys are stored in a contiguous block(s) of memory).
 +
 
 +
Some disadvantages of flat map 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.
 +
-->
 +
 
 +
{{ftm begin}}
 +
{{ftm|__cpp_lib_flat_map|value=202207L|std=C++23|{{tt|std::flat_map}} and {{lc|std::flat_multimap}}}}
 +
{{ftm end}}
 +
 
 +
===Example===
 +
{{example}}
 +
 
 +
===See also===
 +
{{dsc begin}}
 +
{{dsc inc|cpp/container/dsc flat_multimap}}
 +
{{dsc inc|cpp/container/dsc map}}
 +
{{dsc inc|cpp/container/dsc unordered_map}}
 +
{{dsc end}}
 +
 
 +
{{langlinks|cs|de|es|fr|it|ja|pl|pt|ru|zh}}
 +
 
 +
<!--TODO: N4950::24.6.9 says:
 +
TODO: add this to the "Exceptions" sections of appropriate member functions:
 +
6. If any member function in 24.6.9.2 exits via an exception, the invariant is restored.
 +
[Note 2 : This can result in the flat_map’s being emptied. — end note].
 +
-->
 +
 
 +
<!--TODO: N4950::24.6.9.1 says:
 +
TODO: add to constructors:
 +
9. The effect of calling a constructor that takes both key_container_type and mapped_container_type arguments with containers of different sizes is undefined.
 +
10. The effect of calling a constructor or member function that takes a sorted_unique_t argument with a container, containers, or range that is not sorted with respect to key_comp(), or that contains equal elements, is undefined.
 +
-->
 +
 
 +
<!-- TODO: also complete [[cpp/container#Iterator invalidation]] table for flat_* adaptors -->

Revision as of 15:36, 25 December 2023

 
 
 
 
Defined in header <flat_map>
template<

    class Key,
    class T,
    class Compare = std::less<Key>,
    class KeyContainer = std::vector<Key>,
    class MappedContainer = std::vector<T>

> class flat_map;

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.

Contents

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.

{{par|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

Member 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-defined LegacyInputIterator and random_access_iterator to value_type[edit]
const_iterator implementation-defined LegacyInputIterator and random_access_iterator to const 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
{
    key_container_type keys;
    mapped_container_type values;
};
[edit]

Member classes

compares objects of type value_type
(class) [edit]

Member objects

Member name Definition
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

constructs the flat_map
(public member function) [edit]
destructs the flat_map
(public member function) [edit]
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 element not less than the given key
(public member function) [edit]
returns an iterator to the first element greater 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 type value_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]

Non-member functions

lexicographically compares the values of two flat_maps
(function template) [edit]
specializes the std::swap algorithm
(function template) [edit]
erases all elements satisfying specific criteria
(function template) [edit]

Helper classes

specializes the std::uses_allocator type trait
(class template specialization) [edit]

Tags

indicates that elements of a range are sorted and unique
(tag)[edit]
indicates that elements of a range are sorted (uniqueness is not required)
(tag)[edit]

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 Value Std Feature
__cpp_lib_flat_map 202207L (C++23) std::flat_map and std::flat_multimap

Example

See also

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]
collection of key-value pairs, hashed by keys, keys are unique
(class template) [edit]