template <class... Args>
pair<iterator, bool>
try_emplace(const key_type& k, Args&&... args); // (1) C++17
template <class... Args>
pair<iterator, bool>
try_emplace(key_type&& k, Args&&... args); // (2) C++17
template <class... Args>
iterator
try_emplace(const_iterator hint,
const key_type& k,
Args&&... args); // (3) C++17
template <class... Args>
iterator
try_emplace(const_iterator hint,
key_type&& k,
Args&&... args); // (4) C++17
template <class K, class... Args>
pair<iterator, bool>
try_emplace(K&& k, Args&&... args); // (5) C++26
template <class K, class... Args>
iterator
try_emplace(const_iterator hint,
K&& k,
Args&&... args); // (6) C++26
概要
指定されたキーが存在しない場合のみ要素を直接構築で挿入する。
引数 k と等価のキーを持つ要素が存在しない場合、コンテナに新しい要素を挿入する。要素は引数からコンテナ内に直接構築されるため、構築されたオブジェクトはコピーもムーブもされない。
なお、本メンバ関数は emplace や emplace_hint 等と異なり、引数 k と等価のキーを持つ要素が既に存在する場合には、k や args がムーブされてしまうことはない。
引数 hint は、k を検索する際のヒントに使用される。
- (1) : 指定されたキーが存在しない場合に、要素を直接構築で挿入する
- (2) : 指定された一時オブジェクトのキーが存在しない場合に、要素を直接構築で挿入する
- (3) : キーを検索するヒントを指定し、指定されたキーが存在しない場合に、要素を直接構築で挿入する
- (4) : キーを検索するヒントを指定し、指定された一時オブジェクトのキーが存在しない場合に、要素を直接構築で挿入する
- (5) :
key_type型と比較可能なキーが存在しない場合に、要素を直接構築で挿入する - (6) : キーを検索するヒントを指定し、指定された
key_type型と比較可能なキーが存在しない場合に、要素を直接構築で挿入する
テンプレートパラメータ制約
- (1), (3), (5), (6) :
value_typeは、piecewise_construct,forward_as_tuple(k),forward_as_tuple(forward<Args>(args)...)からmapに直接構築可能であること - (2), (4) :
value_typeは、piecewise_construct,forward_as_tuple(move(k)),forward_as_tuple(forward<Args>(args)...)からmapに直接構築可能であること - (5), (6) :
key_compare::is_transparentが妥当な式であることis_convertible_v<K&&, const_iterator> == falseであることis_convertible_v<K&&, iterator> == falseであること
なお、規格に記載はないが、hint は emplace_hint と同様、コンテナの有効な読み取り専用イテレータである必要があるものと思われる。
効果
- (1), (3), (5), (6) :
mapがkと同値のキーを持つ要素を持っている場合、何もしない(引数への副作用もない)。そうでなければ、piecewise_construct,forward_as_tuple(k),forward_as_tuple(forward<Args>(args)...)から構築したvalue_type型のオブジェクトを挿入する。 - (2), (4) :
mapがkと同値のキーを持つ要素を持っている場合、何もしない(引数への副作用もない)。そうでなければ、piecewise_construct,forward_as_tuple(move(k)),forward_as_tuple(forward<Args>(args)...)から構築したvalue_type型のオブジェクトを挿入する。
戻り値
- (1), (2), (5) : イテレータと
bool値のpairを返す。- 挿入された場合には、
firstに挿入された要素へのイテレータ、secondにtrueが設定される。 - 挿入されなかった場合には、
firstにkと等価のキーを持つ既存の要素へのイテレータ、secondにfalseが設定される。
- 挿入された場合には、
- (3), (4), (6) :
- 挿入された場合には、挿入された要素へのイテレータを返す。
- 挿入されなかった場合には、
kと等価のキーを持つ既存の要素へのイテレータを返す。
計算量
- (1), (2), (5) :
emplaceと同じ。 - (3), (4), (6) :
emplace_hintと同じ。
備考
- 概要に記載されているように、本メンバ関数は指定されたキーと等価の要素が既に存在する場合には、引数に副作用が発生しない。
- 一方、
emplace、emplace_hint、insertにはそのような規定は無く、挿入がされなかった場合でも引数に副作用(引数からのムーブ)が発生してしまう可能性があるため、注意が必要である。
- 一方、
- (5), (6) :
is_transparentは、標準ライブラリのstd::less、std::greaterといった関数オブジェクトの、voidに対する特殊化で定義される。それ以外のテンプレートパラメータでis_transparentが定義されないのは、互換性のためである。- これらのオーバーロードは、
map<string, int>のようなコンテナに対し、検索操作で文字列リテラルを渡した際に、キー型の一時オブジェクトが生成されるコストを減らすためにある。
例
#include <iostream>
#include <map>
#include <memory>
#include <utility>
int main()
{
std::map<int, std::unique_ptr<int>> m;
auto u1 = std::make_unique<int>(114);
auto [it1, b1] = m.try_emplace(42, std::move(u1));
std::cout << std::boolalpha << (u1.get() == nullptr) << ", " << *it1->second << ", " << b1 << '\n';
auto u2 = std::make_unique<int>(514);
auto [it2, b2] = m.try_emplace(42, std::move(u2));
std::cout << std::boolalpha << (u2.get() == nullptr) << ", " << *it2->second << ", " << b2 << '\n';
}
出力
true, 114, true
false, 114, false
バージョン
言語
- C++17
処理系
- Clang: 3.7.0 ✅
- GCC: 6.1.0 ✅
- ICC: ??
- Visual C++: ??
関連項目
| 名前 | 説明 |
|---|---|
map::insert |
要素を挿入する |
map::insert_or_assign |
要素を挿入、あるいは代入する |
map::emplace |
要素を直接構築する |
map::emplace_hint |
ヒントを使って要素を直接構築する |
参照
- N3873 Improved insertion interface for unique-key maps
- N4006 An improved emplace() for unique-key maps
- N4240 Improved insertion interface for unique-key maps (Revision 2)
- N4279 Improved insertion interface for unique-key maps (Revision 2.3)
- P2363R5: Extending associative containers with the remaining heterogeneous overloads
- C++26で
template <class K>のバージョンが追加された
- C++26で