std::flat_map(3)
π std::flat_map(3) β C++ Standard Library π
Okay bestie, this is the manual page for std::flat_map(3) from the C++ Standard Library β and yes, itβs giving very organized container-adaptor energy. Letβs get into it. π¦
β¨ NAME β¨
std::flat_map β literally std::flat_map, no cap.
πΈ Synopsis πΈ
Defined in header <flat_map>.
template<
class Key,
class T,
class Compare = std::less<Key>, (since C++23)
class KeyContainer = std::vector<Key>,
class MappedContainer = std::vector<T>
> class flat_map;
The flat map is a container adaptor that gives you the functionality of an associative container containing 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. Slay. π
π Template parameters π
Keyβ the type of the keys. The program is ill-formed ifKeyis not the same type asKeyContainer::value_type.Tβ the type of mapped values. The program is ill-formed ifTis not the same type asMappedContainer::value_type.Compareβ aComparetype providing a strict weak ordering.KeyContainerβ the type of the underlyingSequenceContainerto store keys. The iterators of such containers should satisfyLegacyRandomAccessIteratoror modelrandom_access_iterator. Invocations of their member functionssizeandmax_sizeshould not exit via an exception.MappedContainerβ the type of the underlyingSequenceContainerto store mapped values correspondingly. The same iterator andsize/max_sizerequirements apply.
The standard containers std::vector and std::deque satisfy these requirements. π
π§Έ Member types π§Έ
key_container_typeβKeyContainermapped_container_typeβMappedContainerkey_typeβKeymapped_typeβTvalue_typeβstd::pair<key_type, mapped_type>key_compareβComparereferenceβstd::pair<const key_type&, mapped_type&>const_referenceβstd::pair<const key_type&, const mapped_type&>size_typeβstd::size_tdifference_typeβstd::ptrdiff_titeratorβ implementation-definedLegacyInputIteratorandrandom_access_iteratortovalue_typeconst_iteratorβ implementation-definedLegacyInputIteratorandrandom_access_iteratortoconst value_typereverse_iteratorβstd::reverse_iterator<iterator>const_reverse_iteratorβstd::reverse_iterator<const_iterator>containersβ type describing the underlying containers:
struct containers
{
key_container_type keys;
mapped_container_type values;
};
π Member classes π
value_compareβ compares objects of typevalue_type(class).
π§ Member objects π§
c(private) β the object of typecontainers(exposition-only member object*).compare(private) β the comparison function object of typekey_compare(exposition-only member object*).
π Member functions π
constructorβ constructs theflat_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).
πΈ Element access πΈ
atβ access specified element with bounds checking (public member function).operator[]β access or insert specified element (public member function).
π¦ Iterators π¦
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).
π Capacity π
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).
π₯ Modifiers π₯
emplaceβ constructs element in-place (public member function).emplace_hintβ constructs elements in-place using a hint (public member function).try_emplaceβ inserts in-place if the key does not exist, does nothing if the key exists (public member function).insertβ inserts elements (public member function).insert_rangeβ inserts a range of elements (public member function).insert_or_assignβ inserts an element or assigns to the current element if the key already exists (public member function).extractβ extracts the underlying containers (public member function).replaceβ replaces the underlying containers (public member function).eraseβ erases elements (public member function).swapβ swaps the contents (public member function).clearβ clears the contents (public member function).
π Lookup π
findβ finds 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 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).
π Observers π
key_compβ returns the function that compares keys (public member function).value_compβ returns the function that compares keys in objects of typevalue_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 twoflat_maps (function template);operator<=>is(C++23).std::swap(std::flat_map)(C++23)β specializes thestd::swapalgorithm (function template).erase_if(std::flat_map)(C++23)β erases all elements satisfying specific criteria (function template).
π Helper classes π
std::uses_allocator<std::flat_map>(C++23)β specializes thestd::uses_allocatortype trait (class template specialization).
π·οΈ Tags π·οΈ
sorted_unique,sorted_unique_t(C++23)β tag indicating that elements of a range are sorted and unique.
π Deduction guides π
Nothing listed here on this page β the section is just vibing empty. π«Ά
π§ 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.
__cpp_lib_flat_mapβ value202207L, std(C++23), feature:std::flat_mapandstd::flat_multimap.
π Example π
This section is incomplete.
Reason: no example.
π₯ See also π₯
flat_multimap(C++23)β adapts two containers to provide a collection of key-value pairs, sorted by keys (class template).mapβ collection of key-value pairs, sorted by keys, keys are unique (class template).unordered_map(C++11)β collection of key-value pairs, hashed by keys, keys are unique (class template).
π Categories π
- Todo without reason
- Todo no example
π Cute sign-off: source/version is http://cppreference.com, date is 2024.06.10, and this iconic manual page is std::flat_map(3). Stay flat, sorted, and slay, bestie! π«Ά
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms