名前空間
変種
操作

std::unordered_map<Key,T,Hash,KeyEqual,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 と等価なキーがすでに存在する場合、何も行わない。そうでない場合、キー 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` が対応する式から `unordered_map` にEmplaceConstructibleでない場合、動作は未定義。
3) このオーバーロードは、以下のすべての条件が満たされている場合にのみ、オーバーロード解決に参加する。
もし hash_function()(u.first) != hash_function()(k) || contains(u.first)true の場合、動作は未定義。ここで u は挿入される新しい要素である。
6) このオーバーロードは、Hash::is_transparentKeyEqual::is_transparent の両方が有効で、それぞれ型を示す場合にのみ、オーバーロード解決に参加する。
もし hash_function()(u.first) != hash_function()(k) || contains(u.first)true の場合、動作は未定義。ここで u は挿入される新しい要素である。

操作後、要素の新しい数が古い max_load_factor() * bucket_count() より大きい場合、リハッシュが行われる。
リハッシュが発生した場合(挿入による)、すべてのイテレータは無効化される。それ以外の場合(リハッシュなし)、イテレータは無効化されない。

目次

[編集] パラメータ

k - 検索および見つからなかった場合に挿入するために使用されるキー
hint - 新しい要素が挿入される前の位置へのイテレータ
args - 要素のコンストラクタに転送する引数

[編集] 戻り値

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

[編集] 計算量

1-3) emplace と同じ
平均的には償却定数時間、最悪の場合コンテナのサイズに比例する線形時間。
4-6) emplace_hint と同じ
平均的には償却定数時間、最悪の場合コンテナのサイズに比例する線形時間。

[編集] 備考

insertemplace とは異なり、これらの関数は挿入が行われない場合に右辺値引数からムーブしないため、std::unordered_map<std::string, std::unique_ptr<foo>> のようなムーブオンリーな型の値を扱うマップの操作が容易になる。さらに、`try_emplace` はキーと `mapped_type` への引数を個別に扱うが、emplace は `value_type` (すなわち std::pair) を構築するための引数を必要とする。

オーバーロード (3,6) は、型 `Key` のオブジェクトを構築することなく呼び出すことができる。

機能テストマクロ 規格 機能
__cpp_lib_unordered_map_try_emplace 201411L (C++17) std::unordered_map::try_emplace,
std::unordered_map::insert_or_assign
__cpp_lib_associative_heterogeneous_insertion 202311L (C++26) 順序付きおよび非順序連想コンテナの残りのメンバ関数の異種オーバーロード。オーバーロード (3) および (6)

[編集]

#include <iostream>
#include <string>
#include <unordered_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::unordered_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++17以降)を挿入する
(公開メンバ関数) [編集]
English 日本語 中文(简体) 中文(繁體)