template< class... Args >
iterator try_emplace( const_iterator hint, const Key& k, Args&&... args );
template< class... Args >
iterator try_emplace( const_iterator hint, Key&& k, Args&&... args );
template< class K, class... Args >
iterator try_emplace( const_iterator hint, K&& k, Args&&... args );
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:
1-6)If
value_type
is not
EmplaceConstructibleinto
map
from the corresponding expression, the behavior is undefined.
3)This overload participates in overload resolution only if all following conditions are satisfied:
If equal_range(u.first) == equal_range(k) is false, the behavior is undefined, where u is the new element to be inserted.
6) This overload 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.
[edit] 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 [edit] Return value 1-3)Same as for
emplace
:
A pair consisting of an iterator to the inserted element (or to the element that prevented the insertion) and a
boolvalue set to
trueif and only if the insertion took place.
4-6)Same as for
emplace_hint
:
An iterator to the inserted element, or to the element that prevented the insertion.
[edit] Complexity 1-3)Same as for
emplace
:
Logarithmic in the size of the container.
4-6)Same as for
emplace_hint
:
Logarithmic in the size of the container in general, but amortized constant if the new element is inserted just before
hint.
[edit] NotesUnlike 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) and (6) can be called without constructing an object of type Key
.
#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[edit] See also constructs element in-place
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4