std::map<Key,T,Compare,Allocator>:: try_emplace
|
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 で構築された値を持つ新しい要素をコンテナに挿入します。この場合:
emplace
と同様に動作するが、要素は以下のように構築される:
value_type ( std:: piecewise_construct ,
std::
forward_as_tuple
(
k
)
,
emplace
と同様に動作するが、要素は以下のように構築される:
value_type ( std:: piecewise_construct ,
std::
forward_as_tuple
(
std
::
move
(
k
)
)
,
emplace
と同様に動作しますが、要素は以下のように構築されます:
value_type ( std:: piecewise_construct ,
std::
forward_as_tuple
(
std::
forward
<
K
>
(
k
)
)
,
emplace_hint
と同様に動作しますが、要素は以下のように構築されます:
value_type ( std:: piecewise_construct ,
std::
forward_as_tuple
(
k
)
,
emplace_hint
と同様に動作しますが、要素は以下のように構築されます:
value_type ( std:: piecewise_construct ,
std::
forward_as_tuple
(
std
::
move
(
k
)
)
,
emplace_hint
と同様に動作しますが、要素は以下のように構築されます:
value_type ( std:: piecewise_construct ,
std::
forward_as_tuple
(
std::
forward
<
K
>
(
k
)
)
,
- 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
は挿入される新しい要素です。
イテレータおよび参照は無効化されません。
目次 |
パラメータ
| k | - | 検索に使用され、見つからない場合は挿入にも使用されるキー |
| hint | - | 新しい要素が挿入される位置の直前を指すイテレータ |
| args | - | 要素のコンストラクタに転送される引数 |
戻り値
計算量
注記
insert
や
emplace
とは異なり、これらの関数は挿入が発生しない場合にrvalue引数からムーブを行いません。これにより、
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以降)
(公開メンバ関数) |