std::flat_m...rt_or_assign(3)
π NAME π
std::flat_map::insert_or_assign β literally std::flat_map::insert_or_assign, no cap. Bestie, this page header is giving std::flat_m...rt_or_assign(3) from the C++ Standard Libary manual, and we're about to get into it. π«Ά
β¨ Synopsis β¨
template< class M >
std::pair<iterator, bool> insert_or_assign( const key_type& k, M&&
(1) (since C++23)
obj );
template< class M >
std::pair<iterator, bool> insert_or_assign( key_type&& k, M&& obj
(2) (since C++23)
);
template< class K, class M >
(3) (since C++23)
std::pair<iterator, bool> insert_or_assign( K&& k, M&& obj );
template< class M >
iterator insert_or_assign( const_iterator hint, const key_type& k,
(4) (since C++23)
M&& obj );
template< class M >
iterator insert_or_assign( const_iterator hint, key_type&& k, M&&
(5) (since C++23)
obj );
template< class K, class M >
(6) (since C++23)
iterator insert_or_assign( const_iterator hint, K&& k, M&& obj );
1,2) okay so if a key equivalent to k already exists in the container, it assigns std::forward<M>(obj) to the mapped_type corresponding to the key k. If the key does not exist, it inserts the new value as if by:
- (1,2)
try_emplace(std::forward<decltype(k)>(k), std::forward<M>(obj)), - (4,5)
try_emplace(hint, std::forward<decltype(k)>(k), std::forward<M>(obj)).
The program is ill-formed if either std::is_assignable_v<mapped_type&, M> or std::is_constructible_v<mapped_type, M> is false. periodt.
3,6) if a key equivalent to k already exists in the container, it assigns std::forward<M>(obj) to the mapped_type corresponding to the key k. Otherwise, it's equivalent to:
- (3)
try_emplace(std::forward<K>(k), std::forward<M>(obj)), - (6)
try_emplace(hint, std::forward<K>(k), std::forward<M>(obj)).
The conversion from k into key_type must construct an object u, for which find(k) == find(u) is true. Otherwise, the behavior is undefined β and not in a cute way, bestie. π
These overloads participate in overload resolution only if:
- The qualified-id
Compare::is_transparentis valid and denotes a type. std::is_constructible_v<key_type, K>is true.std::is_assignable_v<mapped_type&, M>is true.std::is_constructible_v<mapped_type, M>is true.
Information on iterator invalidation is copied from here. π
π«Ά Parameters π«Ά
kβ the key used both to look up and to insert if not foundhintβ iterator to the position before which the new element will be insertedobjβ the value to insert or assign
π Return value π
- 1-3) The
boolcomponent istrueif the insertion took place andfalseif the assignment took place. The iterator component is pointing at the element that was inserted or updated. - 4-6) Iterator pointing at the element that was inserted or updated.
π₯ Complexity π₯
- 1-3) Same as for
emplace. - 4-6) Same as for
emplace_hint.
π Notes π
insert_or_assign returns more information than operator[] and does not require default-constructibility of the mapped type. slay for not making you default-construct stuff for no reason. π
π§ Example π§
Run this code, bestie β it's giving main character energy:
// Run this code
#include <flat_map>
#include <iostream>
#include <string>
void print_node(const auto& node)
{
std::cout << '[' << node.first << "] = " << node.second << '\n';
}
void print_result(auto const& pair)
{
std::cout << (pair.second ? "inserted: " : "assigned: ");
print_node(*pair.first);
}
int main()
{
std::flat_map<std::string, std::string> map;
print_result(map.insert_or_assign("a", "apple"));
print_result(map.insert_or_assign("b", "banana"));
print_result(map.insert_or_assign("c", "cherry"));
print_result(map.insert_or_assign("c", "clementine"));
for (const auto& node : map)
print_node(node);
}
Output:
inserted: [a] = apple
inserted: [b] = banana
inserted: [c] = cherry
assigned: [c] = clementine
[a] = apple
[b] = banana
[c] = clementine
π See also π
operator[]β access or insert specified element (public member function)atβ access specified element with bounds checking (public member function)insertβ inserts elements (public member function)emplaceβ constructs element in-place (public member function)try_emplaceβ inserts in-place if the key does not exist, does nothing if the key exists (public member function)
source/version/date sign-off, because we keep every detail: http://cppreference.com, 2024.06.10, page std::flat_m...rt_or_assign(3) β mwah πβ¨
π¦ 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