std::flat_map<Key,T,Compare,KeyContainer,MappedContainer>:: try_emplace
|
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
で構築された値を持つ新しい要素を挿入します。
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)...);
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)...);
key_type
への変換は、オブジェクト
u
を構築しなければならず、そのオブジェクトに対して
find
(
k
)
==
find
(
u
)
が
true
でなければならない。そうでない場合、動作は未定義である。
-
修飾名
Compare::is_transparentが有効であり、型を表していること。 - std:: is_constructible_v < key_type, K > が true であること。
- std:: is_assignable_v < mapped_type & , Args... > が true であること。
- (3) の場合のみ、 std:: is_convertible_v < K && , const_iterator > および std:: is_convertible_v < K && , iterator > がともに false であること。
| イテレータの無効化に関する情報は こちら からコピーされています |
目次 |
パラメータ
| k | - | 検索に使用され、見つからない場合は挿入にも使用されるキー |
| hint | - | 新しい要素が挿入される位置の直前を指すイテレータ |
| args | - | 要素のコンストラクタに転送される引数 |
戻り値
emplace
と同じ。
emplace_hint
の場合と同様。
計算量
emplace
と同様。
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
関連項目
|
要素をその場で構築する
(公開メンバ関数) |
|
|
ヒントを使用して要素をその場で構築する
(公開メンバ関数) |
|
|
要素を挿入する
(公開メンバ関数) |
|
|
要素を挿入するか、キーが既に存在する場合は現在の要素に代入する
(公開メンバ関数) |