std::flat_map<Key,T,Compare,KeyContainer,MappedContainer>::try_emplace
From cppreference.com
| 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 と等価なキーが既に存在する場合、何もしません。そうでない場合、キー k と、args で構築された値を持つ要素を、基底となるコンテナ c に挿入します。
1,2,4,5)
emplace と同等です。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)
emplace_hint と同等です。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 となる必要があります。それ以外の場合、動作は未定義です。 これらのオーバーロードは、以下の場合にのみオーバーロード解決に参加します。
- 修飾子付き ID
Compare::is_transparentが有効で、型を示している。 - std::is_constructible_v<key_type, K> が true である。
- std::is_assignable_v<mapped_type&, Args...> が true である。
- オーバーロード (3,6) の場合のみ、std::is_convertible_v<K&&, const_iterator> と std::is_convertible_v<K&&, iterator> が両方とも false である。
| イテレータ無効化に関する情報は、こちらからコピーされています。 |
目次 |
[編集] パラメータ
| k | - | 検索および見つからなかった場合に挿入するために使用されるキー |
| hint | - | 新しい要素が挿入される前の位置へのイテレータ |
| args | - | 要素のコンストラクタに転送する引数 |
[編集] 戻り値
1-3)
emplace と同じです。4-6)
emplace_hint と同じです。[編集] 計算量
1-3)
emplace と同じです。4-6)
emplace_hint と同じです。[編集] 注記
insert や emplace とは異なり、これらの関数は挿入が発生しなかった場合に右辺値引数から移動しません。これにより、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
[編集] 関連項目
| 要素を直接構築する (公開メンバ関数) | |
| ヒントを使用して要素を直接構築する (公開メンバ関数) | |
| 要素を挿入する (公開メンバ関数) | |
| 要素を挿入するか、キーが既に存在する場合は現在の要素に代入する (public member function) |