Namespaces
Variants

std::map<Key,T,Compare,Allocator>:: try_emplace

From cppreference.net

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) emplace と同様に動作するが、要素は以下のように構築される:
value_type ( std:: piecewise_construct ,

std:: forward_as_tuple ( k ) ,

std:: forward_as_tuple ( std:: forward < Args > ( args ) ... ) )
2) emplace と同様に動作するが、要素は以下のように構築される:
value_type ( std:: piecewise_construct ,

std:: forward_as_tuple ( std :: move ( k ) ) ,

std:: forward_as_tuple ( std:: forward < Args > ( args ) ... ) )
3) emplace と同様に動作しますが、要素は以下のように構築されます:
value_type ( std:: piecewise_construct ,

std:: forward_as_tuple ( std:: forward < K > ( k ) ) ,

std:: forward_as_tuple ( std:: forward < Args > ( args ) ... ) )
4) emplace_hint と同様に動作しますが、要素は以下のように構築されます:
value_type ( std:: piecewise_construct ,

std:: forward_as_tuple ( k ) ,

std:: forward_as_tuple ( std:: forward < Args > ( args ) ... ) )
5) emplace_hint と同様に動作しますが、要素は以下のように構築されます:
value_type ( std:: piecewise_construct ,

std:: forward_as_tuple ( std :: move ( k ) ) ,

std:: forward_as_tuple ( std:: forward < Args > ( args ) ... ) )
6) emplace_hint と同様に動作しますが、要素は以下のように構築されます:
value_type ( std:: piecewise_construct ,

std:: forward_as_tuple ( std:: forward < K > ( k ) ) ,

std:: forward_as_tuple ( std:: forward < Args > ( args ) ... ) )
1-6) value_type が対応する式から map EmplaceConstructible でない場合、動作は未定義です。
3) このオーバーロードは、以下の全ての条件が満たされる場合にのみオーバーロード解決に参加します:
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 - 要素のコンストラクタに転送される引数

戻り値

1-3) emplace と同様:
挿入された要素(または挿入を妨げた要素)へのイテレータと、挿入が行われた場合にのみ true に設定される bool 値から構成されるペア。
4-6) emplace_hint と同様:
挿入された要素、または挿入を妨げた要素へのイテレータ。

計算量

1-3) 以下と同様: emplace
コンテナのサイズに対して対数時間。
4-6) 以下と同様: emplace_hint :
一般的にはコンテナのサイズに対して対数時間ですが、新しい要素が hint の直前に挿入される場合は償却定数時間です。

注記

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++17以降)
(公開メンバ関数)