std::map<Key,T,Compare,Allocator>::try_emplace
From cppreference.com
| template< class... Args > std::pair<iterator, bool> try_emplace( const Key& k, Args&&... args ); |
(1) | (C++17以降) |
| template< class... Args > std::pair<iterator, bool> try_emplace( Key&& k, Args&&... args ); |
(2) | (C++17以降) |
| template< class K, class... Args > std::pair<iterator, bool> try_emplace( K&& k, Args&&... args ); |
(3) | (C++26以降) |
| template< class... Args > iterator try_emplace( const_iterator hint, const Key& k, Args&&... args ); |
(4) | (C++17以降) |
| template< class... Args > iterator try_emplace( const_iterator hint, Key&& k, Args&&... args ); |
(5) | (C++17以降) |
template< class K, class... Args > iterator try_emplace( const_iterator hint, K&& k, Args&&... args ); |
(6) | (C++26以降) |
コンテナ内に k と等価なキーがすでに存在する場合、何もしない。それ以外の場合、キー k と args で構築された値を持つ新しい要素をコンテナに挿入する。この場合、
1)
value_type(std::piecewise_construct,
std::forward_as_tuple(std::forward<Args>(args)...))
emplace のように動作するが、要素は次のように構築されるvalue_type(std::piecewise_construct,
std::forward_as_tuple(std::forward<Args>(args)...))
2)
value_type(std::piecewise_construct,
emplace のように動作するが、要素は次のように構築されるvalue_type(std::piecewise_construct,
std::forward_as_tuple(std::move(k)),
3)
value_type(std::piecewise_construct,
emplace のように動作するが、要素は次のように構築されるvalue_type(std::piecewise_construct,
std::forward_as_tuple(std::forward<K>(k)),
4)
value_type(std::piecewise_construct,
std::forward_as_tuple(std::forward<Args>(args)...))
emplace_hint のように動作するが、要素は次のように構築されるvalue_type(std::piecewise_construct,
std::forward_as_tuple(std::forward<Args>(args)...))
5)
value_type(std::piecewise_construct,
emplace_hint のように動作するが、要素は次のように構築されるvalue_type(std::piecewise_construct,
std::forward_as_tuple(std::move(k)),
6)
value_type(std::piecewise_construct,
emplace_hint のように動作するが、要素は次のように構築されるvalue_type(std::piecewise_construct,
std::forward_as_tuple(std::forward<K>(k)),
3) このオーバーロードは、以下のすべての条件が満たされている場合にのみ、オーバーロード解決に参加する。
- std::is_convertible_v<K&&, const_iterator> と std::is_convertible_v<K&&, iterator> が両方とも false である。
- 限定子付き ID Compare::is_transparent が有効であり、型を表す場合。
もし equal_range(u.first) == equal_range(k) が false の場合、動作は未定義である。ここで u は挿入される新しい要素である。
6) このオーバーロードは、限定子付き ID Compare::is_transparent が有効であり、型を表す場合にのみ、オーバーロード解決に参加する。
もし equal_range(u.first) == equal_range(k) が false の場合、動作は未定義である。ここで u は挿入される新しい要素である。
イテレータや参照は無効化されない。
目次 |
[編集] パラメータ
| k | - | 検索および見つからなかった場合に挿入するために使用されるキー |
| hint | - | 新しい要素が挿入される前の位置へのイテレータ |
| args | - | 要素のコンストラクタに転送する引数 |
[編集] 戻り値
[編集] 計算量
[編集] 備考
insert や emplace とは異なり、これらの関数は挿入が行われない場合に右辺値引数からのムーブを行わない。これにより、std::map<std::string, std::unique_ptr<foo>> のようなムーブ専用型を値とするマップを簡単に操作できる。さらに、try_emplace はキーと mapped_type への引数を個別に扱う。これに対し、emplace は value_type (すなわち std::pair) を構築するための引数を必要とする。
オーバーロード (3,6) は、Key 型のオブジェクトを構築することなく呼び出すことができる。
| 機能テストマクロ | 値 | 規格 | 機能 |
|---|---|---|---|
__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) | 順序付きおよび非順序連想コンテナの残りのメンバ関数の異種オーバーロード。オーバーロード (3) および (6)。 |
[編集] 例
このコードを実行
#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); }
出力
inserted: [a] = a inserted: [b] = abcd inserted: [c] = cccccccccc ignored: [c] = cccccccccc [a] = a [b] = abcd [c] = cccccccccc
[編集] 関連項目
| (C++11) |
要素を直接構築する (公開メンバ関数) |
| (C++11) |
ヒントを使用して要素を直接構築する (公開メンバ関数) |
| 要素 またはノード(C++17以降)を挿入する (公開メンバ関数) |