std::map<Key,T,Compare,Allocator>:: insert_or_assign
|
template
<
class
M
>
std:: pair < iterator, bool > insert_or_assign ( const Key & k, M && obj ) ; |
(1) | (C++17以降) |
|
template
<
class
M
>
std:: pair < iterator, bool > insert_or_assign ( Key && k, M && obj ) ; |
(2) | (C++17以降) |
|
template
<
class
K,
class
M
>
std:: pair < iterator, bool > insert_or_assign ( K && k, M && obj ) ; |
(3) | (C++26以降) |
|
template
<
class
M
>
iterator insert_or_assign ( const_iterator hint, const Key & k, M && obj ) ; |
(4) | (C++17以降) |
|
template
<
class
M
>
iterator insert_or_assign ( const_iterator hint, Key && k, M && obj ) ; |
(5) | (C++17以降) |
|
template
<
class
K,
class
M
>
iterator insert_or_assign ( const_iterator hint, K && k, M && obj ) ; |
(6) | (C++26以降) |
mapped_type
に代入する。キーが存在しない場合は、
insert
と同様に新しい値を挿入し、それを
value_type
(
k,
std::
forward
<
M
>
(
obj
)
)
から構築する。
mapped_type
に代入する。キーが存在しない場合、
value_type
のオブジェクト
u
を
std::
forward
<
K
>
(
k
)
,
std::
forward
<
M
>
(
obj
)
)
で構築し、
u
を
*
this
に挿入する。
equal_range
(
u.
first
)
==
equal_range
(
k
)
が
false
の場合、動作は未定義である。
value_type
は
std::
forward
<
K
>
(
k
)
,
std::
forward
<
M
>
(
obj
)
から
map
へ
EmplaceConstructible
でなければならない。このオーバーロードは、
Compare
が
transparent
である場合にのみオーバーロード解決に参加する。これにより、
Key
のインスタンスを構築せずにこの関数を呼び出すことができる。
動作は未定義 (C++20まで) プログラムは不適格 (C++20以降) もし std:: is_assignable_v < mapped_type & , M && > が false の場合。
イテレータや参照は無効化されません。
目次 |
パラメータ
| k | - | 検索に使用され、見つからない場合は挿入にも使用されるキー |
| hint | - | 新しい要素が挿入される位置の直前を指すイテレータ |
| obj | - | 挿入または代入する値 |
戻り値
計算量
emplace
と同様。
emplace_hint
と同様。
注記
insert_or_assign
は
operator
[
]
よりも多くの情報を返し、マップ型のデフォルト構築可能性を必要としません。
| 機能テスト マクロ | 値 | 標準 | 機能 |
|---|---|---|---|
__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> void print_node(const auto& node) { std::cout << '[' << node.first << "] = " << node.second << '\n'; } void print_result(auto const& pair) { std::cout << (pair.second ? "inserted: " : "assigned: "); print_node(*pair.first); } int main() { std::map<std::string, std::string> myMap; print_result(myMap.insert_or_assign("a", "apple")); print_result(myMap.insert_or_assign("b", "banana")); print_result(myMap.insert_or_assign("c", "cherry")); print_result(myMap.insert_or_assign("c", "clementine")); for (const auto& node : myMap) print_node(node); }
出力:
inserted: [a] = apple inserted: [b] = banana inserted: [c] = cherry assigned: [c] = clementine [a] = apple [b] = banana [c] = clementine
関連項目
|
指定された要素にアクセスまたは挿入する
(public member function) |
|
|
境界チェック付きで指定された要素にアクセスする
(public member function) |
|
|
要素を挿入する
またはノード
(since C++17)
(public member function) |
|
|
(C++11)
|
要素をその場で構築する
(public member function) |