std::map<Key,T,Compare,Allocator>::try_emplace (3) - Linux Manuals
std::map<Key,T,Compare,Allocator>::try_emplace: std::map<Key,T,Compare,Allocator>::try_emplace
Command to display std::map<Key,T,Compare,Allocator>::try_emplace
manual in Linux: $ man 3 std::map<Key,T,Compare,Allocator>::try_emplace
NAME
std::map<Key,T,Compare,Allocator>::try_emplace - std::map<Key,T,Compare,Allocator>::try_emplace
Synopsis
template <class... Args> (1) (since C++17)
pair<iterator, bool> try_emplace(const key_type& k, Args&&... args);
template <class... Args> (2) (since C++17)
pair<iterator, bool> try_emplace(key_type&& k, Args&&... args);
template <class... Args> (3) (since C++17)
iterator try_emplace(const_iterator hint, const key_type& k, Args&&... args);
template <class... Args> (4) (since C++17)
iterator try_emplace(const_iterator hint, key_type&& k, Args&&... args);
1) If a key equivalent to k already exists in the container, does nothing. Otherwise, behaves like emplace except that the element is constructed as value_type(std::piecewise_construct, std::forward_as_tuple(k), std::forward_as_tuple(std::forward<Args>(args)...))
2) If a key equivalent to k already exists in the container, does nothing. Otherwise, behaves like emplace except 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)...))
3) If a key equivalent to k already exists in the container, does nothing. Otherwise, behaves like emplace_hint except that the element is constructed as value_type(std::piecewise_construct, std::forward_as_tuple(k), std::forward_as_tuple(std::forward<Args>(args)...))
4) If a key equivalent to k already exists in the container, does nothing. Otherwise, behaves like emplace_hint except 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)...))
No iterators or references are invalidated.
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,2) Same as for emplace
3,4) Same as for emplace_hint
Complexity
1,2) Same as for emplace
3,4) 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)
Example
// Run this code
#include <iostream>
#include <utility>
#include <string>
#include <map>
int main()
{
using namespace std::literals;
std::map<std::string, std::string> m;
m.try_emplace("a", "a"s);
m.try_emplace("b", "abcd");
m.try_emplace("c", 10, 'c');
m.try_emplace("c", "Won't be inserted");
for (const auto &p : m) {
std::cout << p.first << " => " << p.second << '\n';
}
}
Output:
a => a
b => abcd
c => cccccccccc
See also
emplace constructs element in-place
(public member function)
(C++11)
emplace_hint constructs elements in-place using a hint
(public member function)
(C++11)
inserts elements
or nodes
insert (since C++17)
(public member function)
Pages related to std::map<Key,T,Compare,Allocator>::try_emplace
- std::map<Key,T,Compare,Allocator>::at (3) - std::map<Key,T,Compare,Allocator>::at
- std::map<Key,T,Compare,Allocator>::begin,std::map<Key,T,Compare,Allocator>::cbegin (3) - std::map<Key,T,Compare,Allocator>::begin,std::map<Key,T,Compare,Allocator>::cbegin
- std::map<Key,T,Compare,Allocator>::clear (3) - std::map<Key,T,Compare,Allocator>::clear
- std::map<Key,T,Compare,Allocator>::contains (3) - std::map<Key,T,Compare,Allocator>::contains
- std::map<Key,T,Compare,Allocator>::count (3) - std::map<Key,T,Compare,Allocator>::count
- std::map<Key,T,Compare,Allocator>::emplace (3) - std::map<Key,T,Compare,Allocator>::emplace
- std::map<Key,T,Compare,Allocator>::emplace_hint (3) - std::map<Key,T,Compare,Allocator>::emplace_hint
- std::map<Key,T,Compare,Allocator>::empty (3) - std::map<Key,T,Compare,Allocator>::empty
- std::map<Key,T,Compare,Allocator>::end,std::map<Key,T,Compare,Allocator>::cend (3) - std::map<Key,T,Compare,Allocator>::end,std::map<Key,T,Compare,Allocator>::cend
- std::map<Key,T,Compare,Allocator>::equal_range (3) - std::map<Key,T,Compare,Allocator>::equal_range
- std::map<Key,T,Compare,Allocator>::erase (3) - std::map<Key,T,Compare,Allocator>::erase
- std::map<Key,T,Compare,Allocator>::extract (3) - std::map<Key,T,Compare,Allocator>::extract
- std::map<Key,T,Compare,Allocator>::find (3) - std::map<Key,T,Compare,Allocator>::find
- std::map<Key,T,Compare,Allocator>::get_allocator (3) - std::map<Key,T,Compare,Allocator>::get_allocator
- std::map<Key,T,Compare,Allocator>::insert (3) - std::map<Key,T,Compare,Allocator>::insert
- std::map<Key,T,Compare,Allocator>::insert_or_assign (3) - std::map<Key,T,Compare,Allocator>::insert_or_assign
- std::map<Key,T,Compare,Allocator>::key_comp (3) - std::map<Key,T,Compare,Allocator>::key_comp
- std::map<Key,T,Compare,Allocator>::lower_bound (3) - std::map<Key,T,Compare,Allocator>::lower_bound
- std::map<Key,T,Compare,Allocator>::map (3) - std::map<Key,T,Compare,Allocator>::map
- std::map<Key,T,Compare,Allocator>::max_size (3) - std::map<Key,T,Compare,Allocator>::max_size
- std::map<Key,T,Compare,Allocator>::merge (3) - std::map<Key,T,Compare,Allocator>::merge
- std::map<Key,T,Compare,Allocator>::operator= (3) - std::map<Key,T,Compare,Allocator>::operator=
- std::map<Key,T,Compare,Allocator>::operator[] (3) - std::map<Key,T,Compare,Allocator>::operator[]
- std::map<Key,T,Compare,Allocator>::rbegin,std::map<Key,T,Compare,Allocator>::crbegin (3) - std::map<Key,T,Compare,Allocator>::rbegin,std::map<Key,T,Compare,Allocator>::crbegin
- std::map<Key,T,Compare,Allocator>::rend,std::map<Key,T,Compare,Allocator>::crend (3) - std::map<Key,T,Compare,Allocator>::rend,std::map<Key,T,Compare,Allocator>::crend
- std::map<Key,T,Compare,Allocator>::size (3) - std::map<Key,T,Compare,Allocator>::size
- std::map<Key,T,Compare,Allocator>::swap (3) - std::map<Key,T,Compare,Allocator>::swap
- std::map<Key,T,Compare,Allocator>::upper_bound (3) - std::map<Key,T,Compare,Allocator>::upper_bound
- std::map<Key,T,Compare,Allocator>::value_comp (3) - std::map<Key,T,Compare,Allocator>::value_comp
- std::map<Key,T,Compare,Allocator>::value_compare (3) - std::map<Key,T,Compare,Allocator>::value_compare
- std::map<Key,T,Compare,Allocator>::~map (3) - std::map<Key,T,Compare,Allocator>::~map
- std::map (3) - std::map