std::flat_map::try_emplace(3)
π std::flat_map::try_emplace(3)
Bestie, welcome to the std::flat_map::try_emplace page from the C++ Standard Libary (manual section (3)). Itβs all since C++23, and itβs giving βI will insert your key only if itβs not already thereβ energy. Periodt. β¨
πΈ NAME
std::flat_map::try_emplace β std::flat_map::try_emplace
Itβs the try_emplace member of std::flat_map, bestie. The name is literally the name, no cap. π«Ά
β¨ Synopsis
template< class... Args >
std::pair<iterator, bool> try_emplace( const key_type& k,
(1) (since C++23)
Args&&... args );
template< class... Args >
std::pair<iterator, bool> try_emplace( key_type&& k, Args&&...
(2) (since C++23)
args );
template< class K, class... Args >
(3) (since C++23)
std::pair<iterator, bool> try_emplace( K&& k, Args&&... args );
template< class... Args >
iterator try_emplace( const_iterator hint, const key_type& k,
(4) (since C++23)
Args&&... args );
template< class... Args >
iterator try_emplace( const_iterator hint, key_type&& k, Args&&...
(5) (since C++23)
args );
template< class K, class... Args >
iterator try_emplace( const_iterator hint, K&& k, Args&&... args
(6) (since C++23)
);
So hereβs the vibe check: if a key equivalent to k already exists in the container, it does nothing. Otherwise, it inserts a new element into the underlying containers c with key k and value constructed with args. π
Overloads 1,2,4,5 are equivalent to:
auto key_it = ranges::upper_bound(c.keys, k, compare);
auto value_it = c.values.begin() + std::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)...);
Overloads 3,6 are equivalent to:
auto key_it = ranges::upper_bound(c.keys, k, compare);
auto value_it = c.values.begin() + std::distance(c.keys.begin(),
key_it);
c.keys.emplace(key_it, std::forward<K>(k));
c.values.emplace(value_it, std::forward<Args>(args)...);
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. Undefined behavior, bestie β do not play with that. π
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&, Args...>is true.- For
(3)only,std::is_convertible_v<K&&, const_iterator>andstd::is_convertible_v<K&&, iterator>are both false.
Information on iterator invalidation is copied from here
π Parameters
kβ the key used both to look up and to insert if not found.hintβ iterator to the position before which the new element will be inserted.argsβ arguments to forward to the constructor of the element.
π Return value
1-3)β same as foremplace.4-6)β same as foremplace_hint.
π Complexity
1-3)β same as foremplace.4-6)β same as foremplace_hint.
π§ Notes
Unlike insert or emplace, these functions do not move from rvalue arguments if the insertion does not happen, which makes it easy to manipulate maps whose values are move-only types, such as std::flat_map<std::string, std::unique_ptr<foo>>. In addition, try_emplace treats the key and the arguments to the mapped_type separately, unlike emplace, which requires the arguments to construct a value_type (that is, a std::pair). β¨
Overloads (3,6) can be called without constructing an object of type key_type.
π Example
// Run this code
#include <flat_map>
#include <iostream>
#include <string>
#include <utility>
void print_node(const auto& node)
{
std::cout << '[' << node.first << "] = " << node.second << '\n';
}
void print_result(auto const& pair)
{
std::cout << (pair.second ? "inserted: " : "ignored: ");
print_node(*pair.first);
}
int main()
{
using namespace std::literals;
std::map<std::string, std::string> m;
print_result(m.try_emplace( "a", "a"s));
print_result(m.try_emplace( "b", "abcd"));
print_result(m.try_emplace( "c", 10, 'c'));
print_result(m.try_emplace( "c", "Won't be inserted"));
for (const auto& p : m)
print_node(p);
}
Output:
inserted: [a] = a
inserted: [b] = abcd
inserted: [c] = cccccccccc
ignored: [c] = cccccccccc
[a] = a
[b] = abcd
[c] = cccccccccc
π¦ See also
emplaceβ constructs element in-place (public member function)emplace_hintβ constructs elements in-place using a hint (public member function)insertβ inserts elements (public member function)insert_or_assignβ inserts an element or assigns to the current element if the key already exists (public member function)
Thatβs the whole vibe, bestie. Source/version: http://cppreference.com Β· 2024.06.10 Β· page std::flat_map::try_emplace(3). Go forth and emplace responsibly. π
β¨
π¦ 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