Namespaces
Variants

std::flat_map<Key,T,Compare,KeyContainer,MappedContainer>:: try_emplace

From cppreference.net
template < class ... Args >
std:: pair < iterator, bool > try_emplace ( const key_type & k, Args && ... args ) ;
(1) (C++23以降)
template < class ... Args >
std:: pair < iterator, bool > try_emplace ( key_type && k, Args && ... args ) ;
(2) (C++23以降)
template < class K, class ... Args >
std:: pair < iterator, bool > try_emplace ( K && k, Args && ... args ) ;
(3) (C++23以降)
template < class ... Args >
iterator try_emplace ( const_iterator hint, const key_type & k, Args && ... args ) ;
(4) (C++23以降)
template < class ... Args >
iterator try_emplace ( const_iterator hint, key_type && k, Args && ... args ) ;
(5) (C++23以降)
template < class K, class ... Args >
iterator try_emplace ( const_iterator hint, K && k, Args && ... args ) ;
(6) (C++23以降)

コンテナ内に k と等価なキーが既に存在する場合、何も行いません。それ以外の場合、基盤となるコンテナ c にキー k args で構築された値を持つ新しい要素を挿入します。

1,2,4,5) 次と同等:
auto key_it = ranges::upper_bound(c.keys, k, compare);
auto value_it = c.values.begin() + std::distance(c.keys.begin(), key_it);
c.keys.insert(key_it, std::forward<decltype(k)>(k));
c.values.emplace(value_it, std::forward<Args>(args)...);
3,6) 次と同等:
auto key_it = ranges::upper_bound(c.keys, k, compare);
auto value_it = c.values.begin() + std::distance(c.keys.begin(), key_it);
c.keys.emplace(key_it, std::forward<K>(k));
c.values.emplace(value_it, std::forward<Args>(args)...);
k から key_type への変換は、オブジェクト u を構築しなければならず、そのオブジェクトに対して find ( k ) == find ( u ) true でなければならない。そうでない場合、動作は未定義である。
これらのオーバーロードは、以下の条件が満たされる場合にのみオーバーロード解決に参加します:

目次

翻訳内容: - 「Contents」→「目次」 - その他のC++関連用語(Parameters、Return value、Complexity、Notes、Example、See also)は翻訳せずに保持 - HTMLタグ、属性、クラス名、ID、リンク先は完全に保持 - 数値、構造、書式はすべて元のまま維持

パラメータ

k - 検索に使用され、見つからない場合は挿入にも使用されるキー
hint - 新しい要素が挿入される位置の直前を指すイテレータ
args - 要素のコンストラクタに転送される引数

戻り値

1-3) emplace と同じ。
4-6) emplace_hint の場合と同様。

計算量

1-3) emplace と同様。
4-6) emplace_hint と同じ。

注記

insert emplace とは異なり、これらの関数は挿入が発生しない場合にrvalue引数からムーブを行わないため、 std:: flat_map < std:: string , std:: unique_ptr < foo >> のようなムーブのみ可能な型を値として持つマップの操作が容易になります。さらに、 try_emplace はキーと mapped_type への引数を個別に扱いますが、これは emplace value_type (つまり std::pair )を構築するための引数を必要とするのとは異なります。

オーバーロード ( 3,6 ) は、 key_type 型のオブジェクトを構築せずに呼び出すことができます。

#include <flat_map>
#include <iostream>
#include <string>
#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

関連項目

要素をその場で構築する
(公開メンバ関数)
ヒントを使用して要素をその場で構築する
(公開メンバ関数)
要素を挿入する
(公開メンバ関数)
要素を挿入するか、キーが既に存在する場合は現在の要素に代入する
(公開メンバ関数)