Files
2025-10-25 03:02:53 +03:00

910 lines
51 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[flat.map]
# 23 Containers library [[containers]](./#containers)
## 23.6 Container adaptors [[container.adaptors]](container.adaptors#flat.map)
### 23.6.8 Class template flat_map [flat.map]
#### [23.6.8.1](#overview) Overview [[flat.map.overview]](flat.map.overview)
[1](#overview-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L16954)
A flat_map is a container adaptor
that provides an associative container interface
that supports unique keys
(i.e., contains at most one of each key value) and
provides for fast retrieval of values of another type T based on the keys[.](#overview-1.sentence-1)
flat_map supports iterators
that meet the *Cpp17InputIterator* requirements and
model the[random_access_iterator](iterator.concept.random.access#concept:random_access_iterator "24.3.4.13Concept random_­access_­iterator[iterator.concept.random.access]") concept ([[iterator.concept.random.access]](iterator.concept.random.access "24.3.4.13Concept random_­access_­iterator"))[.](#overview-1.sentence-2)
[2](#overview-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L16967)
A flat_map meets all of the requirements
of a container ([[container.reqmts]](container.reqmts "23.2.2.2Container requirements")) and
of a reversible container ([[container.rev.reqmts]](container.rev.reqmts "23.2.2.3Reversible container requirements")),
plus the optional container requirements ([[container.opt.reqmts]](container.opt.reqmts "23.2.2.4Optional container requirements"))[.](#overview-2.sentence-1)
flat_map meets the requirements of
an associative container ([[associative.reqmts]](associative.reqmts "23.2.7Associative containers")),
except that:
- [(2.1)](#overview-2.1)
it does not meet the requirements related to node handles ([[container.node]](container.node "23.2.5Node handles")),
- [(2.2)](#overview-2.2)
it does not meet the requirements related to iterator invalidation, and
- [(2.3)](#overview-2.3)
the time complexity of the operations that insert or erase a single
element from the map is linear, including the ones that take an insertion
position iterator[.](#overview-2.sentence-2)
[*Note [1](#overview-note-1)*:
A flat_map does not meet the additional requirements of
an allocator-aware container ([[container.alloc.reqmts]](container.alloc.reqmts "23.2.2.5Allocator-aware containers"))[.](#overview-2.sentence-3)
— *end note*]
[3](#overview-3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L16990)
A flat_map also provides most operations
described in [[associative.reqmts]](associative.reqmts "23.2.7Associative containers") for unique keys[.](#overview-3.sentence-1)
This means that a flat_map supports
the a_uniq operations in [[associative.reqmts]](associative.reqmts "23.2.7Associative containers") but not the a_eq operations[.](#overview-3.sentence-2)
For a flat_map<Key, T> the key_type is Key and
the value_type is pair<Key, T>[.](#overview-3.sentence-3)
[4](#overview-4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17000)
Descriptions are provided here only for operations on flat_map that
are not described in one of those sets of requirements or for operations where
there is additional semantic information[.](#overview-4.sentence-1)
[5](#overview-5)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17005)
A flat_map maintains the following invariants:
- [(5.1)](#overview-5.1)
it contains the same number of keys and values;
- [(5.2)](#overview-5.2)
the keys are sorted with respect to the comparison object; and
- [(5.3)](#overview-5.3)
the value at offset off within the value container is
the value associated with the key at offset off within the key container[.](#overview-5.sentence-1)
[6](#overview-6)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17018)
If any member function in [[flat.map.defn]](#defn "23.6.8.2Definition") exits via an exception
the invariants are restored[.](#overview-6.sentence-1)
[*Note [2](#overview-note-2)*:
This can result in the flat_map being emptied[.](#overview-6.sentence-2)
— *end note*]
[7](#overview-7)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17025)
Any type C that meets the sequence container requirements ([[sequence.reqmts]](sequence.reqmts "23.2.4Sequence containers"))
can be used to instantiate flat_map,
as long asC::iterator meets the *Cpp17RandomAccessIterator* requirements and
invocations of
member functions C::size and C::max_size do not exit via an exception[.](#overview-7.sentence-1)
In particular, vector ([[vector]](vector "23.3.13Class template vector")) and deque ([[deque]](deque "23.3.5Class template deque"))
can be used[.](#overview-7.sentence-2)
[*Note [3](#overview-note-3)*:
vector<bool> is not a sequence container[.](#overview-7.sentence-3)
— *end note*]
[8](#overview-8)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17039)
The program is ill-formed ifKey is not the same type as KeyContainer::value_type orT is not the same type as MappedContainer::value_type[.](#overview-8.sentence-1)
[9](#overview-9)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17044)
The effect of calling a constructor
that takes
both key_container_type and mapped_container_type arguments with
containers of different sizes is undefined[.](#overview-9.sentence-1)
[10](#overview-10)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17050)
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[.](#overview-10.sentence-1)
[11](#overview-11)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17058)
The types iterator and const_iterator meet
the constexpr iterator requirements ([[iterator.requirements.general]](iterator.requirements.general "24.3.1General"))[.](#overview-11.sentence-1)
#### [23.6.8.2](#defn) Definition [[flat.map.defn]](flat.map.defn)
namespace std {template<class Key, class T, class Compare = less<Key>, class KeyContainer = vector<Key>, class MappedContainer = vector<T>>class flat_map {public:// typesusing key_type = Key; using mapped_type = T; using value_type = pair<key_type, mapped_type>; using key_compare = Compare; using reference = pair<const key_type&, mapped_type&>; using const_reference = pair<const key_type&, const mapped_type&>; using size_type = size_t; using difference_type = ptrdiff_t; using iterator = *implementation-defined*; // see [[container.requirements]](container.requirements "23.2Requirements")using const_iterator = *implementation-defined*; // see [[container.requirements]](container.requirements "23.2Requirements")using reverse_iterator = std::reverse_iterator<iterator>; using const_reverse_iterator = std::reverse_iterator<const_iterator>; using key_container_type = KeyContainer; using mapped_container_type = MappedContainer; class value_compare {private: key_compare *comp*; // *exposition only*constexpr value_compare(key_compare c) : *comp*(c) { } // *exposition only*public:constexpr bool operator()(const_reference x, const_reference y) const {return *comp*(x.first, y.first); }}; struct containers { key_container_type keys;
mapped_container_type values; }; // [[flat.map.cons]](#cons "23.6.8.3Constructors"), constructorsconstexpr flat_map() : flat_map(key_compare()) { }constexpr explicit flat_map(const key_compare& comp): *c*(), *compare*(comp) { }constexpr flat_map(key_container_type key_cont, mapped_container_type mapped_cont, const key_compare& comp = key_compare()); constexpr flat_map(sorted_unique_t, key_container_type key_cont,
mapped_container_type mapped_cont, const key_compare& comp = key_compare()); template<class InputIterator>constexpr flat_map(InputIterator first, InputIterator last, const key_compare& comp = key_compare()): *c*(), *compare*(comp) { insert(first, last); }template<class InputIterator>constexpr flat_map(sorted_unique_t, InputIterator first, InputIterator last, const key_compare& comp = key_compare()): *c*(), *compare*(comp) { insert(sorted_unique, first, last); }template<[*container-compatible-range*](container.intro.reqmts#concept:container-compatible-range "23.2.2.1Introduction[container.intro.reqmts]")<value_type> R>constexpr flat_map(from_range_t, R&& rg): flat_map(from_range, std::forward<R>(rg), key_compare()) { }template<[*container-compatible-range*](container.intro.reqmts#concept:container-compatible-range "23.2.2.1Introduction[container.intro.reqmts]")<value_type> R>constexpr flat_map(from_range_t, R&& rg, const key_compare& comp): flat_map(comp) { insert_range(std::forward<R>(rg)); }constexpr flat_map(initializer_list<value_type> il, const key_compare& comp = key_compare()): flat_map(il.begin(), il.end(), comp) { }constexpr flat_map(sorted_unique_t, initializer_list<value_type> il, const key_compare& comp = key_compare()): flat_map(sorted_unique, il.begin(), il.end(), comp) { }// [[flat.map.cons.alloc]](#cons.alloc "23.6.8.4Constructors with allocators"), constructors with allocatorstemplate<class Alloc>constexpr explicit flat_map(const Alloc& a); template<class Alloc>constexpr flat_map(const key_compare& comp, const Alloc& a); template<class Alloc>constexpr flat_map(const key_container_type& key_cont, const mapped_container_type& mapped_cont, const Alloc& a); template<class Alloc>constexpr flat_map(const key_container_type& key_cont, const mapped_container_type& mapped_cont, const key_compare& comp, const Alloc& a); template<class Alloc>constexpr flat_map(sorted_unique_t, const key_container_type& key_cont, const mapped_container_type& mapped_cont, const Alloc& a); template<class Alloc>constexpr flat_map(sorted_unique_t, const key_container_type& key_cont, const mapped_container_type& mapped_cont, const key_compare& comp, const Alloc& a); template<class Alloc>constexpr flat_map(const flat_map&, const Alloc& a); template<class Alloc>constexpr flat_map(flat_map&&, const Alloc& a); template<class InputIterator, class Alloc>constexpr flat_map(InputIterator first, InputIterator last, const Alloc& a); template<class InputIterator, class Alloc>constexpr flat_map(InputIterator first, InputIterator last, const key_compare& comp, const Alloc& a); template<class InputIterator, class Alloc>constexpr flat_map(sorted_unique_t, InputIterator first, InputIterator last, const Alloc& a); template<class InputIterator, class Alloc>constexpr flat_map(sorted_unique_t, InputIterator first, InputIterator last, const key_compare& comp, const Alloc& a); template<[*container-compatible-range*](container.intro.reqmts#concept:container-compatible-range "23.2.2.1Introduction[container.intro.reqmts]")<value_type> R, class Alloc>constexpr flat_map(from_range_t, R&& rg, const Alloc& a); template<[*container-compatible-range*](container.intro.reqmts#concept:container-compatible-range "23.2.2.1Introduction[container.intro.reqmts]")<value_type> R, class Alloc>constexpr flat_map(from_range_t, R&& rg, const key_compare& comp, const Alloc& a); template<class Alloc>constexpr flat_map(initializer_list<value_type> il, const Alloc& a); template<class Alloc>constexpr flat_map(initializer_list<value_type> il, const key_compare& comp, const Alloc& a); template<class Alloc>constexpr flat_map(sorted_unique_t, initializer_list<value_type> il, const Alloc& a); template<class Alloc>constexpr flat_map(sorted_unique_t, initializer_list<value_type> il, const key_compare& comp, const Alloc& a); constexpr flat_map& operator=(initializer_list<value_type>); // iteratorsconstexpr iterator begin() noexcept; constexpr const_iterator begin() const noexcept; constexpr iterator end() noexcept; constexpr const_iterator end() const noexcept; constexpr reverse_iterator rbegin() noexcept; constexpr const_reverse_iterator rbegin() const noexcept; constexpr reverse_iterator rend() noexcept; constexpr const_reverse_iterator rend() const noexcept; constexpr const_iterator cbegin() const noexcept; constexpr const_iterator cend() const noexcept; constexpr const_reverse_iterator crbegin() const noexcept; constexpr const_reverse_iterator crend() const noexcept; // [[flat.map.capacity]](#capacity "23.6.8.5Capacity"), capacityconstexpr bool empty() const noexcept; constexpr size_type size() const noexcept; constexpr size_type max_size() const noexcept; // [[flat.map.access]](#access "23.6.8.6Access"), element accessconstexpr mapped_type& operator[](const key_type& x); constexpr mapped_type& operator[](key_type&& x); template<class K> constexpr mapped_type& operator[](K&& x); constexpr mapped_type& at(const key_type& x); constexpr const mapped_type& at(const key_type& x) const; template<class K> constexpr mapped_type& at(const K& x); template<class K> constexpr const mapped_type& at(const K& x) const; // [[flat.map.modifiers]](#modifiers "23.6.8.7Modifiers"), modifierstemplate<class... Args> constexpr pair<iterator, bool> emplace(Args&&... args); template<class... Args>constexpr iterator emplace_hint(const_iterator position, Args&&... args); constexpr pair<iterator, bool> insert(const value_type& x){ return emplace(x); }constexpr pair<iterator, bool> insert(value_type&& x){ return emplace(std::move(x)); }constexpr iterator insert(const_iterator position, const value_type& x){ return emplace_hint(position, x); }constexpr iterator insert(const_iterator position, value_type&& x){ return emplace_hint(position, std::move(x)); }template<class P> constexpr pair<iterator, bool> insert(P&& x); template<class P>constexpr iterator insert(const_iterator position, P&&); template<class InputIterator>constexpr void insert(InputIterator first, InputIterator last); template<class InputIterator>constexpr void insert(sorted_unique_t, InputIterator first, InputIterator last); template<[*container-compatible-range*](container.intro.reqmts#concept:container-compatible-range "23.2.2.1Introduction[container.intro.reqmts]")<value_type> R>constexpr void insert_range(R&& rg); constexpr void insert(initializer_list<value_type> il){ insert(il.begin(), il.end()); }constexpr void insert(sorted_unique_t, initializer_list<value_type> il){ insert(sorted_unique, il.begin(), il.end()); }constexpr containers extract() &&; constexpr void replace(key_container_type&& key_cont, mapped_container_type&& mapped_cont); template<class... Args>constexpr pair<iterator, bool> try_emplace(const key_type& k, Args&&... args); template<class... Args>constexpr pair<iterator, bool> try_emplace(key_type&& k, Args&&... args); template<class K, class... Args>constexpr pair<iterator, bool> try_emplace(K&& k, Args&&... args); template<class... Args>constexpr iterator try_emplace(const_iterator hint, const key_type& k, Args&&... args); template<class... Args>constexpr iterator try_emplace(const_iterator hint, key_type&& k, Args&&... args); template<class K, class... Args>constexpr iterator try_emplace(const_iterator hint, K&& k, Args&&... args); template<class M>constexpr pair<iterator, bool> insert_or_assign(const key_type& k, M&& obj); template<class M>constexpr pair<iterator, bool> insert_or_assign(key_type&& k, M&& obj); template<class K, class M>constexpr pair<iterator, bool> insert_or_assign(K&& k, M&& obj); template<class M>constexpr iterator insert_or_assign(const_iterator hint, const key_type& k, M&& obj); template<class M>constexpr iterator insert_or_assign(const_iterator hint, key_type&& k, M&& obj); template<class K, class M>constexpr iterator insert_or_assign(const_iterator hint, K&& k, M&& obj); constexpr iterator erase(iterator position); constexpr iterator erase(const_iterator position); constexpr size_type erase(const key_type& x); template<class K> constexpr size_type erase(K&& x); constexpr iterator erase(const_iterator first, const_iterator last); constexpr void swap(flat_map& y) noexcept; constexpr void clear() noexcept; // observersconstexpr key_compare key_comp() const; constexpr value_compare value_comp() const; constexpr const key_container_type& keys() const noexcept { return *c*.keys; }constexpr const mapped_container_type& values() const noexcept { return *c*.values; }// map operationsconstexpr iterator find(const key_type& x); constexpr const_iterator find(const key_type& x) const; template<class K> constexpr iterator find(const K& x); template<class K> constexpr const_iterator find(const K& x) const; constexpr size_type count(const key_type& x) const; template<class K> constexpr size_type count(const K& x) const; constexpr bool contains(const key_type& x) const; template<class K> constexpr bool contains(const K& x) const; constexpr iterator lower_bound(const key_type& x); constexpr const_iterator lower_bound(const key_type& x) const; template<class K> constexpr iterator lower_bound(const K& x); template<class K> constexpr const_iterator lower_bound(const K& x) const; constexpr iterator upper_bound(const key_type& x); constexpr const_iterator upper_bound(const key_type& x) const; template<class K> constexpr iterator upper_bound(const K& x); template<class K> constexpr const_iterator upper_bound(const K& x) const; constexpr pair<iterator, iterator> equal_range(const key_type& x); constexpr pair<const_iterator, const_iterator> equal_range(const key_type& x) const; template<class K> constexpr pair<iterator, iterator> equal_range(const K& x); template<class K>constexpr pair<const_iterator, const_iterator> equal_range(const K& x) const; friend constexpr bool operator==(const flat_map& x, const flat_map& y); friend constexpr *synth-three-way-result*<value_type>operator<=>(const flat_map& x, const flat_map& y); friend constexpr void swap(flat_map& x, flat_map& y) noexcept{ x.swap(y); }private: containers *c*; // *exposition only* key_compare *compare*; // *exposition only*struct *key-equiv* { // *exposition only*constexpr *key-equiv*(key_compare c) : comp(c) { }constexpr bool operator()(const_reference x, const_reference y) const {return !comp(x.first, y.first) && !comp(y.first, x.first); } key_compare comp; }; }; template<class KeyContainer, class MappedContainer, class Compare = less<typename KeyContainer::value_type>> flat_map(KeyContainer, MappedContainer, Compare = Compare())-> flat_map<typename KeyContainer::value_type, typename MappedContainer::value_type,
Compare, KeyContainer, MappedContainer>; template<class KeyContainer, class MappedContainer, class Allocator> flat_map(KeyContainer, MappedContainer, Allocator)-> flat_map<typename KeyContainer::value_type, typename MappedContainer::value_type,
less<typename KeyContainer::value_type>, KeyContainer, MappedContainer>; template<class KeyContainer, class MappedContainer, class Compare, class Allocator> flat_map(KeyContainer, MappedContainer, Compare, Allocator)-> flat_map<typename KeyContainer::value_type, typename MappedContainer::value_type,
Compare, KeyContainer, MappedContainer>; template<class KeyContainer, class MappedContainer, class Compare = less<typename KeyContainer::value_type>> flat_map(sorted_unique_t, KeyContainer, MappedContainer, Compare = Compare())-> flat_map<typename KeyContainer::value_type, typename MappedContainer::value_type,
Compare, KeyContainer, MappedContainer>; template<class KeyContainer, class MappedContainer, class Allocator> flat_map(sorted_unique_t, KeyContainer, MappedContainer, Allocator)-> flat_map<typename KeyContainer::value_type, typename MappedContainer::value_type,
less<typename KeyContainer::value_type>, KeyContainer, MappedContainer>; template<class KeyContainer, class MappedContainer, class Compare, class Allocator> flat_map(sorted_unique_t, KeyContainer, MappedContainer, Compare, Allocator)-> flat_map<typename KeyContainer::value_type, typename MappedContainer::value_type,
Compare, KeyContainer, MappedContainer>; template<class InputIterator, class Compare = less<*iter-key-type*<InputIterator>>> flat_map(InputIterator, InputIterator, Compare = Compare())-> flat_map<*iter-key-type*<InputIterator>, *iter-mapped-type*<InputIterator>, Compare>; template<class InputIterator, class Compare = less<*iter-key-type*<InputIterator>>> flat_map(sorted_unique_t, InputIterator, InputIterator, Compare = Compare())-> flat_map<*iter-key-type*<InputIterator>, *iter-mapped-type*<InputIterator>, Compare>; template<ranges::[input_range](range.refinements#concept:input_range "25.4.6Other range refinements[range.refinements]") R, class Compare = less<*range-key-type*<R>>, class Allocator = allocator<byte>> flat_map(from_range_t, R&&, Compare = Compare(), Allocator = Allocator())-> flat_map<*range-key-type*<R>, *range-mapped-type*<R>, Compare,
vector<*range-key-type*<R>, *alloc-rebind*<Allocator, *range-key-type*<R>>>,
vector<*range-mapped-type*<R>, *alloc-rebind*<Allocator, *range-mapped-type*<R>>>>; template<ranges::[input_range](range.refinements#concept:input_range "25.4.6Other range refinements[range.refinements]") R, class Allocator> flat_map(from_range_t, R&&, Allocator)-> flat_map<*range-key-type*<R>, *range-mapped-type*<R>, less<*range-key-type*<R>>,
vector<*range-key-type*<R>, *alloc-rebind*<Allocator, *range-key-type*<R>>>,
vector<*range-mapped-type*<R>, *alloc-rebind*<Allocator, *range-mapped-type*<R>>>>; template<class Key, class T, class Compare = less<Key>> flat_map(initializer_list<pair<Key, T>>, Compare = Compare())-> flat_map<Key, T, Compare>; template<class Key, class T, class Compare = less<Key>> flat_map(sorted_unique_t, initializer_list<pair<Key, T>>, Compare = Compare())-> flat_map<Key, T, Compare>; template<class Key, class T, class Compare, class KeyContainer, class MappedContainer, class Allocator>struct uses_allocator<flat_map<Key, T, Compare, KeyContainer, MappedContainer>, Allocator>: bool_constant<uses_allocator_v<KeyContainer, Allocator> && uses_allocator_v<MappedContainer, Allocator>> { };}
[1](#defn-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17411)
The member type containers has the data members and special members
specified above[.](#defn-1.sentence-1)
It has no base classes or members other than those specified[.](#defn-1.sentence-2)
#### [23.6.8.3](#cons) Constructors [[flat.map.cons]](flat.map.cons)
[🔗](#lib:flat_map,constructor)
`constexpr flat_map(key_container_type key_cont, mapped_container_type mapped_cont,
const key_compare& comp = key_compare());
`
[1](#cons-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17425)
*Effects*: Initializes*c*.keys with std::move(key_cont),*c*.values with std::move(mapped_cont), and*compare* with comp;
sorts the range [begin(), end()) with respect to value_comp(); and
finally erases the duplicate elements as if by:auto zv = views::zip(*c*.keys, *c*.values);auto it = ranges::unique(zv, *key-equiv*(*compare*)).begin();auto dist = distance(zv.begin(), it);*c*.keys.erase(*c*.keys.begin() + dist, *c*.keys.end());*c*.values.erase(*c*.values.begin() + dist, *c*.values.end());
[2](#cons-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17441)
*Complexity*: Linear in N if the container arguments are already sorted
with respect to value_comp() and otherwise NlogN,
where N is the value of key_cont.size() before this call[.](#cons-2.sentence-1)
[🔗](#lib:flat_map,constructor_)
`constexpr flat_map(sorted_unique_t, key_container_type key_cont, mapped_container_type mapped_cont,
const key_compare& comp = key_compare());
`
[3](#cons-3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17455)
*Effects*: Initializes*c*.keys with std::move(key_cont),*c*.values with std::move(mapped_cont), and*compare* with comp[.](#cons-3.sentence-1)
[4](#cons-4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17462)
*Complexity*: Constant[.](#cons-4.sentence-1)
#### [23.6.8.4](#cons.alloc) Constructors with allocators [[flat.map.cons.alloc]](flat.map.cons.alloc)
[1](#cons.alloc-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17469)
The constructors in this subclause shall not participate in overload resolution
unless uses_allocator_v<key_container_type, Alloc> is true and uses_allocator_v<mapped_container_type, Alloc> is true[.](#cons.alloc-1.sentence-1)
[🔗](#lib:flat_map,constructor__)
`template<class Alloc>
constexpr flat_map(const key_container_type& key_cont, const mapped_container_type& mapped_cont,
const Alloc& a);
template<class Alloc>
constexpr flat_map(const key_container_type& key_cont, const mapped_container_type& mapped_cont,
const key_compare& comp, const Alloc& a);
`
[2](#cons.alloc-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17485)
*Effects*: Equivalent to flat_map(key_cont, mapped_cont) andflat_map(key_cont, mapped_cont, comp), respectively,
except that *c*.keys and *c*.values are constructed with
uses-allocator construction ([[allocator.uses.construction]](allocator.uses.construction "20.2.8.2Uses-allocator construction"))[.](#cons.alloc-2.sentence-1)
[3](#cons.alloc-3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17492)
*Complexity*: Same as flat_map(key_cont, mapped_cont) andflat_map(key_cont, mapped_cont, comp), respectively[.](#cons.alloc-3.sentence-1)
[🔗](#lib:flat_map,constructor___)
`template<class Alloc>
constexpr flat_map(sorted_unique_t, const key_container_type& key_cont,
const mapped_container_type& mapped_cont, const Alloc& a);
template<class Alloc>
constexpr flat_map(sorted_unique_t, const key_container_type& key_cont,
const mapped_container_type& mapped_cont, const key_compare& comp,
const Alloc& a);
`
[4](#cons.alloc-4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17510)
*Effects*: Equivalent to flat_map(sorted_unique, key_cont, mapped_cont) andflat_map(sorted_unique, key_cont, mapped_cont, comp), respectively,
except that *c*.keys and *c*.values are constructed
with uses-allocator construction ([[allocator.uses.construction]](allocator.uses.construction "20.2.8.2Uses-allocator construction"))[.](#cons.alloc-4.sentence-1)
[5](#cons.alloc-5)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17517)
*Complexity*: Linear[.](#cons.alloc-5.sentence-1)
[🔗](#lib:flat_map,constructor____)
`template<class Alloc>
constexpr explicit flat_map(const Alloc& a);
template<class Alloc>
constexpr flat_map(const key_compare& comp, const Alloc& a);
template<class Alloc>
constexpr flat_map(const flat_map&, const Alloc& a);
template<class Alloc>
constexpr flat_map(flat_map&&, const Alloc& a);
template<class InputIterator, class Alloc>
constexpr flat_map(InputIterator first, InputIterator last, const Alloc& a);
template<class InputIterator, class Alloc>
constexpr flat_map(InputIterator first, InputIterator last, const key_compare& comp,
const Alloc& a);
template<class InputIterator, class Alloc>
constexpr flat_map(sorted_unique_t, InputIterator first, InputIterator last, const Alloc& a);
template<class InputIterator, class Alloc>
constexpr flat_map(sorted_unique_t, InputIterator first, InputIterator last,
const key_compare& comp, const Alloc& a);
template<[container-compatible-range](container.intro.reqmts#concept:container-compatible-range "23.2.2.1Introduction[container.intro.reqmts]")<value_type> R, class Alloc>
constexpr flat_map(from_range_t, R&& rg, const Alloc& a);
template<[container-compatible-range](container.intro.reqmts#concept:container-compatible-range "23.2.2.1Introduction[container.intro.reqmts]")<value_type> R, class Alloc>
constexpr flat_map(from_range_t, R&& rg, const key_compare& comp, const Alloc& a);
template<class Alloc>
constexpr flat_map(initializer_list<value_type> il, const Alloc& a);
template<class Alloc>
constexpr flat_map(initializer_list<value_type> il, const key_compare& comp, const Alloc& a);
template<class Alloc>
constexpr flat_map(sorted_unique_t, initializer_list<value_type> il, const Alloc& a);
template<class Alloc>
constexpr flat_map(sorted_unique_t, initializer_list<value_type> il,
const key_compare& comp, const Alloc& a);
`
[6](#cons.alloc-6)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17558)
*Effects*: Equivalent to the corresponding non-allocator constructors
except that *c*.keys and *c*.values are constructed
with uses-allocator construction ([[allocator.uses.construction]](allocator.uses.construction "20.2.8.2Uses-allocator construction"))[.](#cons.alloc-6.sentence-1)
#### [23.6.8.5](#capacity) Capacity [[flat.map.capacity]](flat.map.capacity)
[🔗](#lib:size,flat_map)
`constexpr size_type size() const noexcept;
`
[1](#capacity-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17573)
*Returns*: *c*.keys.size()[.](#capacity-1.sentence-1)
[🔗](#lib:max_size,flat_map)
`constexpr size_type max_size() const noexcept;
`
[2](#capacity-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17584)
*Returns*: min<size_type>(*c*.keys.max_size(), *c*.values.max_size())[.](#capacity-2.sentence-1)
#### [23.6.8.6](#access) Access [[flat.map.access]](flat.map.access)
[🔗](#lib:operator%5b%5d,flat_map)
`constexpr mapped_type& operator[](const key_type& x);
`
[1](#access-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17597)
*Effects*: Equivalent to: return try_emplace(x).first->second;
[🔗](#lib:operator%5b%5d,flat_map_)
`constexpr mapped_type& operator[](key_type&& x);
`
[2](#access-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17608)
*Effects*: Equivalent to: return try_emplace(std::move(x)).first->second;
[🔗](#lib:operator%5b%5d,flat_map__)
`template<class K> constexpr mapped_type& operator[](K&& x);
`
[3](#access-3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17619)
*Constraints*: The [*qualified-id*](expr.prim.id.qual#nt:qualified-id "7.5.5.3Qualified names[expr.prim.id.qual]") Compare::is_transparent is valid and
denotes a type[.](#access-3.sentence-1)
[4](#access-4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17624)
*Effects*: Equivalent to: return try_emplace(std::forward<K>(x)).first->second;
[🔗](#lib:at,flat_map)
`constexpr mapped_type& at(const key_type& x);
constexpr const mapped_type& at(const key_type& x) const;
`
[5](#access-5)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17636)
*Returns*: A reference to the mapped_type corresponding
to x in *this[.](#access-5.sentence-1)
[6](#access-6)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17641)
*Throws*: An exception object of type out_of_range if
no such element is present[.](#access-6.sentence-1)
[7](#access-7)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17646)
*Complexity*: Logarithmic[.](#access-7.sentence-1)
[🔗](#lib:at,flat_map_)
`template<class K> constexpr mapped_type& at(const K& x);
template<class K> constexpr const mapped_type& at(const K& x) const;
`
[8](#access-8)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17658)
*Constraints*: The [*qualified-id*](expr.prim.id.qual#nt:qualified-id "7.5.5.3Qualified names[expr.prim.id.qual]") Compare::is_transparent is valid and denotes a type[.](#access-8.sentence-1)
[9](#access-9)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17663)
*Preconditions*: The expression find(x) is well-formed and has well-defined behavior[.](#access-9.sentence-1)
[10](#access-10)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17667)
*Returns*: A reference to the mapped_type corresponding tox in *this[.](#access-10.sentence-1)
[11](#access-11)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17672)
*Throws*: An exception object of type out_of_range if no such element is present[.](#access-11.sentence-1)
[12](#access-12)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17677)
*Complexity*: Logarithmic[.](#access-12.sentence-1)
#### [23.6.8.7](#modifiers) Modifiers [[flat.map.modifiers]](flat.map.modifiers)
[🔗](#lib:emplace,flat_map)
`template<class... Args> constexpr pair<iterator, bool> emplace(Args&&... args);
`
[1](#modifiers-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17690)
*Constraints*: is_constructible_v<pair<key_type, mapped_type>, Args...> is true[.](#modifiers-1.sentence-1)
[2](#modifiers-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17694)
*Effects*: Initializes an object t of type pair<key_type, mapped_type> with std::forward<Args>(args)...;
if the map already contains an element
whose key is equivalent to t.first,*this is unchanged[.](#modifiers-2.sentence-1)
Otherwise, equivalent to:auto key_it = ranges::upper_bound(*c*.keys, t.first, *compare*);auto value_it = *c*.values.begin() + distance(*c*.keys.begin(), key_it);*c*.keys.insert(key_it, std::move(t.first));*c*.values.insert(value_it, std::move(t.second));
[3](#modifiers-3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17709)
*Returns*: The bool component of the returned pair is true if and only if the insertion took place, and
the iterator component of the pair points to
the element with key equivalent to t.first[.](#modifiers-3.sentence-1)
[🔗](#lib:insert,flat_map)
`template<class P> constexpr pair<iterator, bool> insert(P&& x);
template<class P> constexpr iterator insert(const_iterator position, P&& x);
`
[4](#modifiers-4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17724)
*Constraints*: is_constructible_v<pair<key_type, mapped_type>, P> is true[.](#modifiers-4.sentence-1)
[5](#modifiers-5)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17728)
*Effects*: The first form is equivalent to return emplace(std::forward<P>(x));[.](#modifiers-5.sentence-1)
The second form is equivalent toreturn emplace_hint(position, std::forward<P>(x));[.](#modifiers-5.sentence-2)
[🔗](#lib:insert,flat_map_)
`template<class InputIterator>
constexpr void insert(InputIterator first, InputIterator last);
`
[6](#modifiers-6)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17742)
*Effects*: Adds elements to *c* as if by:for (; first != last; ++first) { value_type value = *first; *c*.keys.insert(*c*.keys.end(), std::move(value.first)); *c*.values.insert(*c*.values.end(), std::move(value.second));}
Then, sorts the range of newly inserted elements
with respect to value_comp();
merges the resulting sorted range and
the sorted range of pre-existing elements into a single sorted range; and
finally erases the duplicate elements as if by:auto zv = views::zip(*c*.keys, *c*.values);auto it = ranges::unique(zv, *key-equiv*(*compare*)).begin();auto dist = distance(zv.begin(), it);*c*.keys.erase(*c*.keys.begin() + dist, *c*.keys.end());*c*.values.erase(*c*.values.begin() + dist, *c*.values.end());
[7](#modifiers-7)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17765)
*Complexity*: N + MlogM,
where N is size() before the operation andM is distance(first, last)[.](#modifiers-7.sentence-1)
[8](#modifiers-8)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17771)
*Remarks*: Since this operation performs an in-place merge, it may allocate memory[.](#modifiers-8.sentence-1)
[🔗](#lib:insert,flat_map__)
`template<class InputIterator>
constexpr void insert(sorted_unique_t, InputIterator first, InputIterator last);
`
[9](#modifiers-9)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17783)
*Effects*: Adds elements to *c* as if by:for (; first != last; ++first) { value_type value = *first; *c*.keys.insert(*c*.keys.end(), std::move(value.first)); *c*.values.insert(*c*.values.end(), std::move(value.second));}
Then, merges the sorted range of newly added elements and
the sorted range of pre-existing elements into a single sorted range; and
finally erases the duplicate elements as if by:auto zv = views::zip(*c*.keys, *c*.values);auto it = ranges::unique(zv, *key-equiv*(*compare*)).begin();auto dist = distance(zv.begin(), it);*c*.keys.erase(*c*.keys.begin() + dist, *c*.keys.end());*c*.values.erase(*c*.values.begin() + dist, *c*.values.end());
[10](#modifiers-10)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17804)
*Complexity*: Linear in N, where N is size() after the operation[.](#modifiers-10.sentence-1)
[11](#modifiers-11)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17808)
*Remarks*: Since this operation performs an in-place merge, it may allocate memory[.](#modifiers-11.sentence-1)
[🔗](#lib:insert_range,flat_map)
`template<[container-compatible-range](container.intro.reqmts#concept:container-compatible-range "23.2.2.1Introduction[container.intro.reqmts]")<value_type> R>
constexpr void insert_range(R&& rg);
`
[12](#modifiers-12)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17820)
*Effects*: Adds elements to *c* as if by:for (const auto& e : rg) {*c*.keys.insert(*c*.keys.end(), e.first); *c*.values.insert(*c*.values.end(), e.second);}
Then, sorts the range of newly inserted elements
with respect to value_comp();
merges the resulting sorted range and
the sorted range of pre-existing elements into a single sorted range; and
finally erases the duplicate elements as if by:auto zv = views::zip(*c*.keys, *c*.values);auto it = ranges::unique(zv, *key-equiv*(*compare*)).begin();auto dist = distance(zv.begin(), it);*c*.keys.erase(*c*.keys.begin() + dist, *c*.keys.end());*c*.values.erase(*c*.values.begin() + dist, *c*.values.end());
[13](#modifiers-13)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17842)
*Complexity*: N + MlogM,
where N is size() before the operation andM is ranges::distance(rg)[.](#modifiers-13.sentence-1)
[14](#modifiers-14)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17848)
*Remarks*: Since this operation performs an in-place merge, it may allocate memory[.](#modifiers-14.sentence-1)
[🔗](#lib:try_emplace,flat_map)
`template<class... Args>
constexpr pair<iterator, bool> try_emplace(const key_type& k, Args&&... args);
template<class... Args>
constexpr pair<iterator, bool> try_emplace(key_type&& k, Args&&... args);
template<class... Args>
constexpr iterator try_emplace(const_iterator hint, const key_type& k, Args&&... args);
template<class... Args>
constexpr iterator try_emplace(const_iterator hint, key_type&& k, Args&&... args);
`
[15](#modifiers-15)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17866)
*Constraints*: is_constructible_v<mapped_type, Args...> is true[.](#modifiers-15.sentence-1)
[16](#modifiers-16)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17870)
*Effects*: If the map already contains an element whose key is equivalent to k,*this and args... are unchanged[.](#modifiers-16.sentence-1)
Otherwise equivalent to:auto key_it = ranges::upper_bound(*c*.keys, k, *compare*);auto value_it = *c*.values.begin() + distance(*c*.keys.begin(), key_it);*c*.keys.insert(key_it, std::forward<decltype(k)>(k));*c*.values.emplace(value_it, std::forward<Args>(args)...);
[17](#modifiers-17)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17882)
*Returns*: In the first two overloads,
the bool component of the returned pair is true if and only if the insertion took place[.](#modifiers-17.sentence-1)
The returned iterator points to the map element
whose key is equivalent to k[.](#modifiers-17.sentence-2)
[18](#modifiers-18)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17890)
*Complexity*: The same as emplace for the first two overloads, and
the same as emplace_hint for the last two overloads[.](#modifiers-18.sentence-1)
[🔗](#lib:try_emplace,flat_map_)
`template<class K, class... Args>
constexpr pair<iterator, bool> try_emplace(K&& k, Args&&... args);
template<class K, class... Args>
constexpr iterator try_emplace(const_iterator hint, K&& k, Args&&... args);
`
[19](#modifiers-19)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17905)
*Constraints*:
- [(19.1)](#modifiers-19.1)
The [*qualified-id*](expr.prim.id.qual#nt:qualified-id "7.5.5.3Qualified names[expr.prim.id.qual]") Compare::is_transparent is valid and denotes a type[.](#modifiers-19.1.sentence-1)
- [(19.2)](#modifiers-19.2)
is_constructible_v<key_type, K> is true[.](#modifiers-19.2.sentence-1)
- [(19.3)](#modifiers-19.3)
is_constructible_v<mapped_type, Args...> is true[.](#modifiers-19.3.sentence-1)
- [(19.4)](#modifiers-19.4)
For the first overload,is_convertible_v<K&&, const_iterator> andis_convertible_v<K&&, iterator> are both false[.](#modifiers-19.4.sentence-1)
[20](#modifiers-20)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17921)
*Preconditions*: The conversion from k into key_type constructs
an object u,
for which find(k) == find(u) is true[.](#modifiers-20.sentence-1)
[21](#modifiers-21)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17927)
*Effects*: If the map already contains an element whose key is equivalent to k,*this and args... are unchanged[.](#modifiers-21.sentence-1)
Otherwise equivalent to:auto key_it = upper_bound(*c*.keys.begin(), *c*.keys.end(), k, *compare*);auto value_it = *c*.values.begin() + distance(*c*.keys.begin(), key_it);*c*.keys.emplace(key_it, std::forward<K>(k));*c*.values.emplace(value_it, std::forward<Args>(args)...);
[22](#modifiers-22)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17939)
*Returns*: In the first overload,
the bool component of the returned pair is true if and only if the insertion took place[.](#modifiers-22.sentence-1)
The returned iterator points to the map element
whose key is equivalent to k[.](#modifiers-22.sentence-2)
[23](#modifiers-23)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17947)
*Complexity*: The same as emplace and emplace_hint, respectively[.](#modifiers-23.sentence-1)
[🔗](#lib:insert_or_assign,flat_map)
`template<class M>
constexpr pair<iterator, bool> insert_or_assign(const key_type& k, M&& obj);
template<class M>
constexpr pair<iterator, bool> insert_or_assign(key_type&& k, M&& obj);
template<class M>
constexpr iterator insert_or_assign(const_iterator hint, const key_type& k, M&& obj);
template<class M>
constexpr iterator insert_or_assign(const_iterator hint, key_type&& k, M&& obj);
`
[24](#modifiers-24)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17965)
*Constraints*: is_assignable_v<mapped_type&, M> is true andis_constructible_v<mapped_type, M> is true[.](#modifiers-24.sentence-1)
[25](#modifiers-25)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17970)
*Effects*: If the map already contains an element e whose key is equivalent to k,
assigns std::forward<M>(obj) to e.second[.](#modifiers-25.sentence-1)
Otherwise, equivalent totry_emplace(std::forward<decltype(k)>(k), std::forward<M>(obj)) for the first two overloads ortry_emplace(hint, std::forward<decltype(k)>(k), std::forward<M>(obj)) for the last two overloads[.](#modifiers-25.sentence-2)
[26](#modifiers-26)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17985)
*Returns*: In the first two overloads, the bool component of the returned pair
is true if and only if the insertion took place[.](#modifiers-26.sentence-1)
The returned
iterator points to the map element whose key is equivalent to k[.](#modifiers-26.sentence-2)
[27](#modifiers-27)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L17991)
*Complexity*: The same as emplace for the first two overloads and
the same as emplace_hint for the last two overloads[.](#modifiers-27.sentence-1)
[🔗](#lib:insert_or_assign,flat_map_)
`template<class K, class M>
constexpr pair<iterator, bool> insert_or_assign(K&& k, M&& obj);
template<class K, class M>
constexpr iterator insert_or_assign(const_iterator hint, K&& k, M&& obj);
`
[28](#modifiers-28)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L18006)
*Constraints*:
- [(28.1)](#modifiers-28.1)
The [*qualified-id*](expr.prim.id.qual#nt:qualified-id "7.5.5.3Qualified names[expr.prim.id.qual]") Compare::is_transparent is valid and denotes a type[.](#modifiers-28.1.sentence-1)
- [(28.2)](#modifiers-28.2)
is_constructible_v<key_type, K> is true[.](#modifiers-28.2.sentence-1)
- [(28.3)](#modifiers-28.3)
is_assignable_v<mapped_type&, M> is true[.](#modifiers-28.3.sentence-1)
- [(28.4)](#modifiers-28.4)
is_constructible_v<mapped_type, M> is true[.](#modifiers-28.4.sentence-1)
[29](#modifiers-29)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L18020)
*Preconditions*: The conversion from k into key_type constructs
an object u, for which find(k) == find(u) is true[.](#modifiers-29.sentence-1)
[30](#modifiers-30)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L18025)
*Effects*: If the map already contains an element e whose key is equivalent to k,
assigns std::forward<M>(obj) to e.second[.](#modifiers-30.sentence-1)
Otherwise, equivalent totry_emplace(std::forward<K>(k), std::forward<M>(obj)) for the first overload ortry_emplace(hint, std::forward<K>(k), std::forward<M>(obj)) for the second overload[.](#modifiers-30.sentence-2)
[31](#modifiers-31)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L18040)
*Returns*: In the first overload,
the bool component of the returned pair is true if and only if the insertion took place[.](#modifiers-31.sentence-1)
The returned iterator points to the map element
whose key is equivalent to k[.](#modifiers-31.sentence-2)
[32](#modifiers-32)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L18048)
*Complexity*: The same as emplace and emplace_hint, respectively[.](#modifiers-32.sentence-1)
[🔗](#lib:swap,flat_map)
`constexpr void swap(flat_map& y) noexcept;
`
[33](#modifiers-33)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L18059)
*Effects*: Equivalent to:ranges::swap(*compare*, y.*compare*);
ranges::swap(*c*.keys, y.*c*.keys);
ranges::swap(*c*.values, y.*c*.values);
[🔗](#lib:extract,flat_map)
`constexpr containers extract() &&;
`
[34](#modifiers-34)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L18075)
*Postconditions*: *this is emptied, even if the function exits via an exception[.](#modifiers-34.sentence-1)
[35](#modifiers-35)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L18079)
*Returns*: std::move(*c*)[.](#modifiers-35.sentence-1)
[🔗](#lib:replace,flat_map)
`constexpr void replace(key_container_type&& key_cont, mapped_container_type&& mapped_cont);
`
[36](#modifiers-36)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L18090)
*Preconditions*: key_cont.size() == mapped_cont.size() is true,
the elements of key_cont are sorted with respect to *compare*, andkey_cont contains no equal elements[.](#modifiers-36.sentence-1)
[37](#modifiers-37)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L18096)
*Effects*: Equivalent to:*c*.keys = std::move(key_cont);*c*.values = std::move(mapped_cont);
#### [23.6.8.8](#erasure) Erasure [[flat.map.erasure]](flat.map.erasure)
[🔗](#lib:erase_if,flat_map)
`template<class Key, class T, class Compare, class KeyContainer, class MappedContainer,
class Predicate>
constexpr typename flat_map<Key, T, Compare, KeyContainer, MappedContainer>::size_type
erase_if(flat_map<Key, T, Compare, KeyContainer, MappedContainer>& c, Predicate pred);
`
[1](#erasure-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L18116)
*Preconditions*: Key and T meet the *Cpp17MoveAssignable* requirements[.](#erasure-1.sentence-1)
[2](#erasure-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L18120)
*Effects*: Let E be bool(pred(pair<const Key&, const T&>(e)))[.](#erasure-2.sentence-1)
Erases all elements e in c for which E holds[.](#erasure-2.sentence-2)
[3](#erasure-3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L18125)
*Returns*: The number of elements erased[.](#erasure-3.sentence-1)
[4](#erasure-4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L18129)
*Complexity*: Exactly c.size() applications of the predicate[.](#erasure-4.sentence-1)
[5](#erasure-5)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L18133)
*Remarks*: Stable ([[algorithm.stable]](algorithm.stable "16.4.6.8Requirements for stable algorithms"))[.](#erasure-5.sentence-1)
If an invocation of erase_if exits via an exception,c is in a valid but unspecified state ([[defns.valid]](defns.valid "3.67valid but unspecified state"))[.](#erasure-5.sentence-2)
[*Note [1](#erasure-note-1)*:
c still meets its invariants,
but can be empty[.](#erasure-5.sentence-3)
— *end note*]