std::map::try_emplace(3)
π NAME π
okay so this is the manual page for std::map::try_emplace(3) from the C++ Standard Library, and it is literally the std::map::try_emplace member function. NAME says std::map::try_emplace - std::map::try_emplace, and the whole vibe is: insert only if the key is not already there. periodt. π
β¨ Synopsis β¨
Hereβs the signature lineup, bestie β keep these exactly as written, no cap:
(1)β since C++17
template< class... Args >
std::pair<iterator, bool> try_emplace( const Key& k, Args&&... args );
(2)β since C++17
template< class... Args >
std::pair<iterator, bool> try_emplace( Key&& k, Args&&... args );
(3)β since C++26
template< class K, class... Args >
std::pair<iterator, bool> try_emplace( K&& k, Args&&... args );
(4)β since C++17
template< class... Args >
iterator try_emplace( const_iterator hint, const Key& k, Args&&... args );
(5)β since C++17
template< class... Args >
iterator try_emplace( const_iterator hint, Key&& k, Args&&... args );
(6)β since C++26
template< class K, class... Args >
iterator try_emplace( const_iterator hint, K&& k, Args&&... args );
So like, if a key equivalent to k already exists in the container, it does nothing. Otherwise, it inserts a new element into the container with key k and value constructed with args. In such case:
- Behaves like
emplaceexcept that the element is constructed as
value_type(std::piecewise_construct,
std::forward_as_tuple(k),
std::forward_as_tuple(std::forward<Args>(args)...))
- Behaves like
emplaceexcept that the element is constructed as
value_type(std::piecewise_construct,
std::forward_as_tuple(std::move(k)),
std::forward_as_tuple(std::forward<Args>(args)...))
- Behaves like
emplaceexcept that the element is constructed as
value_type(std::piecewise_construct,
std::forward_as_tuple(std::forward<K>(k)),
std::forward_as_tuple(std::forward<Args>(args)...))
- Behaves like
emplace_hintexcept that the element is constructed as
value_type(std::piecewise_construct,
std::forward_as_tuple(k),
std::forward_as_tuple(std::forward<Args>(args)...))
- Behaves like
emplace_hintexcept that the element is constructed as
value_type(std::piecewise_construct,
std::forward_as_tuple(std::move(k)),
std::forward_as_tuple(std::forward<Args>(args)...))
- Behaves like
emplace_hintexcept that the element is constructed as
value_type(std::piecewise_construct,
std::forward_as_tuple(std::forward<K>(k)),
std::forward_as_tuple(std::forward<Args>(args)...))
β οΈ 1-6) Ifvalue_typeis notEmplaceConstructibleintomapfrom the corresponding expression, the behavior is undefined. bestie please respect this π
Overload (3) is picky: it participates in overload resolution only if all following conditions are satisfied:
std::is_convertible_v<K&&, const_iterator>andstd::is_convertible_v<K&&, iterator>are both false.- The qualified-id
Compare::is_transparentis valid and denotes a type.
If equal_range(u.first) == equal_range(k) is false, the behavior is undefined, where u is the new element to be inserted.
Overload (6) is also picky: it participates in overload resolution only if the qualified-id Compare::is_transparent is valid and denotes a type.
If equal_range(u.first) == equal_range(k) is false, the behavior is undefined, where u is the new element to be inserted.
No iterators or references are invalidated. iconic, honestly. β¨
π 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 insertedargsβ arguments to forward to the constructor of the element
π Return value π
- 1-3) Same as for
emplace. - 4-6) Same as for
emplace_hint.
β¨ Complexity β¨
- 1-3) Same as for
emplace. - 4-6) Same as for
emplace_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::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.
π¦ Feature-test macro tea π¦
__cpp_lib_map_try_emplaceβ value201411L, std(C++17), feature:std::map::try_emplace,std::map::insert_or_assign__cpp_lib_associative_heterogeneous_insertionβ value202311L, std(C++26), feature: Heterogeneous overloads for the remaining member functions in ordered and unordered associative containers. Overloads(3)and(6).
π Example π
// Run this code
#include <iostream>
#include <string>
#include <map>
#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;(C++11); public member functionemplace_hintβ constructs elements in-place using a hint;(C++11); public member functioninsertβ inserts elements or nodes;(since C++17); public member function
mwah π source/version/date sign-off: http://cppreference.com Β· 2024.06.10 Β· std::map::try_emplace(3)
π¦ 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