名前空間
変種
操作

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

From cppreference.com
< cpp‎ | コンテナ‎ | map
 
 
 
 
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 と等価なキーがすでに存在する場合、何もしない。それ以外の場合、キー kargs で構築された値を持つ新しい要素をコンテナに挿入する。この場合、

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 が対応する式から mapEmplaceConstructible でない場合、その動作は未定義である。
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 の直前に挿入された場合は償却定数時間。

[編集] 備考

insertemplace とは異なり、これらの関数は挿入が行われない場合に右辺値引数からのムーブを行わない。これにより、std::map<std::string, std::unique_ptr<foo>> のようなムーブ専用型を値とするマップを簡単に操作できる。さらに、try_emplace はキーと mapped_type への引数を個別に扱う。これに対し、emplacevalue_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以降)を挿入する
(公開メンバ関数) [編集]
English 日本語 中文(简体) 中文(繁體)