Difference between revisions of "Template:cpp/container/try emplace assoc"
(→Return value: description was incorrect, lets just defer like everything else on this page) |
(Include actual 'Return value' and 'Complexity' text instead of having to follow the link to emplace/emplace_hint) |
||
Line 102: | Line 102: | ||
===Return value=== | ===Return value=== | ||
− | @1-3@ Same as for {{rlpt|emplace}} | + | @1-3@ Same as for {{rlpt|emplace}}:<br>{{cpp/container/return iterator pair|{{{1|}}}|pair}} |
− | @4-6@ Same as for {{rlpt|emplace_hint}} | + | @4-6@ Same as for {{rlpt|emplace_hint}}:<br>{{cpp/container/return iterator pair|{{{1|}}}}} |
===Complexity=== | ===Complexity=== | ||
− | @1-3@ Same as for {{rlpt|emplace}}. | + | @1-3@ Same as for {{rlpt|emplace}}:<br> |
− | @4-6@ Same as for {{rlpt|emplace_hint}}. | + | {{cpp/container/if ord|{{{1|}}} |
+ | |Logarithmic in the size of the container. | ||
+ | |Amortized constant on average, worst case linear in the size of the container. | ||
+ | }} | ||
+ | @4-6@ Same as for {{rlpt|emplace_hint}}:<br> | ||
+ | {{cpp/container/if ord|{{{1|}}} | ||
+ | |Logarithmic in the size of the container in general, but amortized constant if the new element is inserted just before {{c|hint}}. | ||
+ | |Amortized constant on average, worst case linear in the size of the container. | ||
+ | Amortized constant on average, worst case linear in the size of the container. | ||
+ | }} | ||
===Notes=== | ===Notes=== |
Revision as of 12:00, 5 October 2024
template< class... Args > std::pair<iterator, bool> try_emplace( const Key& k, Args&&... args ); |
(1) | (since C++17) |
template< class... Args > std::pair<iterator, bool> try_emplace( Key&& k, Args&&... args ); |
(2) | (since C++17) |
template< class K, class... Args > std::pair<iterator, bool> try_emplace( K&& k, Args&&... args ); |
(3) | (since C++26) |
template< class... Args > iterator try_emplace( const_iterator hint, const Key& k, Args&&... args ); |
(4) | (since C++17) |
template< class... Args > iterator try_emplace( const_iterator hint, Key&& k, Args&&... args ); |
(5) | (since C++17) |
template< class K, class... Args > iterator try_emplace( const_iterator hint, K&& k, Args&&... args ); |
(6) | (since C++26) |
If a key equivalent to k already exists in the container, does nothing. Otherwise, inserts a new element into the container with key k and value constructed with args. In such case:
emplace
except that the element is constructed asvalue_type(std::piecewise_construct,
std::forward_as_tuple(std::forward<Args>(args)...))
emplace
except that the element is constructed asvalue_type(std::piecewise_construct,
std::forward_as_tuple(std::move(k)),
emplace
except that the element is constructed asvalue_type(std::piecewise_construct,
std::forward_as_tuple(std::forward<K>(k)),
emplace_hint
except that the element is constructed asvalue_type(std::piecewise_construct,
std::forward_as_tuple(std::forward<Args>(args)...))
emplace_hint
except that the element is constructed asvalue_type(std::piecewise_construct,
std::forward_as_tuple(std::move(k)),
emplace_hint
except that the element is constructed asvalue_type(std::piecewise_construct,
std::forward_as_tuple(std::forward<K>(k)),
value_type
is not EmplaceConstructible into {{{1}}}
from the corresponding expression, the behavior is undefined.- std::is_convertible_v<K&&, const_iterator> and std::is_convertible_v<K&&, iterator> are both false.
- Hash::is_transparent and KeyEqual::is_transparent are valid and each denotes a type.
Information on iterator invalidation is copied from here |
Contents |
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
emplace
:A pair consisting of an iterator to the inserted element (or to the element that prevented the insertion) and a bool value set to true if and only if the insertion took place.
emplace_hint
:An iterator to the inserted element, or to the element that prevented the insertion.
Complexity
emplace
:Amortized constant on average, worst case linear in the size of the container.
emplace_hint
:Amortized constant on average, worst case linear in the size of the container.
Amortized constant on average, worst case linear in the size of the container.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::<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 | Value | Std | Feature |
---|---|---|---|
__cpp_lib_map_try_emplace |
201411L | (C++17) | std::map::try_emplace , std::map::insert_or_assign
|
__cpp_lib_associative_heterogeneous_insertion |
202311L | (C++26) | Heterogeneous overloads for the remaining member functions in ordered and unordered associative containers. Overloads (3) and (6). |
Example
#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
constructs element in-place (public member function of std::{{{1}}} )
| |
constructs elements in-place using a hint (public member function of std::{{{1}}} )
| |
inserts elements (public member function of std::{{{1}}} )
|