|
| 1 | +# insert_or_assign |
| 2 | +* flat_map[meta header] |
| 3 | +* std[meta namespace] |
| 4 | +* flat_map[meta class] |
| 5 | +* function template[meta id-type] |
| 6 | +* cpp23[meta cpp] |
| 7 | + |
| 8 | +```cpp |
| 9 | +template<class M> |
| 10 | +pair<iterator, bool> |
| 11 | + insert_or_assign(const key_type& k, |
| 12 | + M&& obj); // (1) C++23 |
| 13 | + |
| 14 | +template<class M> |
| 15 | +pair<iterator, bool> |
| 16 | + insert_or_assign(key_type&& k, |
| 17 | + M&& obj); // (2) C++23 |
| 18 | + |
| 19 | +template<class K, class M> |
| 20 | +pair<iterator, bool> |
| 21 | + insert_or_assign(K&& k, |
| 22 | + M&& obj); // (3) C++23 |
| 23 | + |
| 24 | +template<class M> |
| 25 | +iterator |
| 26 | + insert_or_assign(const_iterator hint, |
| 27 | + const key_type& k, |
| 28 | + M&& obj); // (4) C++23 |
| 29 | + |
| 30 | +template<class M> |
| 31 | +iterator |
| 32 | + insert_or_assign(const_iterator hint, |
| 33 | + key_type&& k, |
| 34 | + M&& obj); // (5) C++23 |
| 35 | + |
| 36 | +template<class K, class M> |
| 37 | +iterator |
| 38 | + insert_or_assign(const_iterator hint, |
| 39 | + K&& k, |
| 40 | + M&& obj); // (6) C++23 |
| 41 | +``` |
| 42 | +* pair[link /reference/utility/pair.md] |
| 43 | +
|
| 44 | +## 概要 |
| 45 | +引数 `k` で指定されたキーが存在しなければ対応する値を引数 `obj` の値として要素を挿入し(`insert`)、さもなければ(`or`)、そのキーに対応する値に引数 `obj` を代入する(`assign`)。 |
| 46 | +
|
| 47 | +引数 `hint` は、`k` を検索する際のヒントに使用される。 |
| 48 | +
|
| 49 | +- (1) : `key_type`型のキーをとって挿入もしくは代入する |
| 50 | +- (2) : `key_type`型の一時オブジェクトのキーをとって挿入もしくは代入する |
| 51 | +- (3) : `key_type`に変換可能な型の一時オブジェクトのキーをとって挿入もしくは代入する |
| 52 | +- (4) : 挿入位置のヒントをともない、`key_type`型のキーをとって挿入もしくは代入する |
| 53 | +- (5) : 挿入位置のヒントをともない、`key_type`型の一時オブジェクトのキーをとって挿入もしくは代入する |
| 54 | +- (6) : 挿入位置のヒントをともない、`key_type`に変換可能な型の一時オブジェクトのキーをとって挿入もしくは代入する |
| 55 | +
|
| 56 | +
|
| 57 | +## テンプレートパラメータ制約 |
| 58 | +- (1), (2), (4), (5) : |
| 59 | + - [`is_assignable_v`](/reference/type_traits/is_assignable.md)`<mapped_type&, M>` が `true` であること |
| 60 | + - [`is_constructible_v`](/reference/type_traits/is_constructible.md)`<mapped_type, M>` が `true` であること |
| 61 | +- (3), (6) : |
| 62 | + - `key_compare::is_transparent`が妥当な修飾IDであり、型を示すこと |
| 63 | + - [`is_constructible_v`](/reference/type_traits/is_constructible.md)`<key_type, K>` が `true` であること |
| 64 | + - [`is_assignable_v`](/reference/type_traits/is_assignable.md)`<mapped_type&, M>` が `true` であること |
| 65 | + - [`is_constructible_v`](/reference/type_traits/is_constructible.md)`<mapped_type, M>` が `true` であること |
| 66 | +
|
| 67 | +なお、規格に記載はないが、`hint` は [`emplace_hint`](emplace_hint.md) と同様、コンテナの有効な読み取り専用イテレータである必要があるものと思われる。 |
| 68 | +
|
| 69 | +
|
| 70 | +## 事前条件 |
| 71 | +- (3), (6) : |
| 72 | + - `key_type`に変換した`k`を`u`として、[`find`](find.md)`(k) ==` [`find`](find.md)`(u)`が`true`であること |
| 73 | +
|
| 74 | +
|
| 75 | +## 効果 |
| 76 | +- (1), (2), (3) : |
| 77 | + - `k`と等価なキーをもつ要素`e`が存在している場合、[`std::forward`](/reference/utility/forward.md)`<M>(obj)`を`e.second`に代入する |
| 78 | + - そうでなければ、以下と等価: |
| 79 | + ```cpp |
| 80 | + try_emplace(std::forward<decltype(k)>(k), std::forward<M>(obj)); |
| 81 | + ``` |
| 82 | + * try_emplace[link try_emplace.md] |
| 83 | + * std::forward[link /reference/utility/forward.md] |
| 84 | +
|
| 85 | +- (4), (5), (6) : |
| 86 | + - `k`と等価なキーをもつ要素`e`が存在している場合、[`std::forward`](/reference/utility/forward.md)`<M>(obj)`を`e.second`に代入する |
| 87 | + - そうでなければ、以下と等価: |
| 88 | + ```cpp |
| 89 | + try_emplace_hint(hint, std::forward<decltype(k)>(k), std::forward<M>(obj)); |
| 90 | + ``` |
| 91 | + * try_emplace_hint[link try_emplace.md] |
| 92 | + * std::forward[link /reference/utility/forward.md] |
| 93 | +
|
| 94 | +
|
| 95 | +## 戻り値 |
| 96 | +- (1)、(2)、(3) : イテレータと `bool` 値の [`pair`](/reference/utility/pair.md) を返す。 |
| 97 | + - 挿入された場合には、`first` に挿入された要素へのイテレータ、`second` に `true` が設定される。 |
| 98 | + - 代入された場合には、`first` に代入された要素へのイテレータ、`second` に `false` が設定される。 |
| 99 | +- (4)、(5)、(6) : |
| 100 | + - 挿入された場合には、挿入された要素へのイテレータを返す。 |
| 101 | + - 代入された場合には、代入された要素へのイテレータを返す。 |
| 102 | +
|
| 103 | +
|
| 104 | +## 計算量 |
| 105 | +- (1)、(2)、(3) : [`emplace`](emplace.md) と同じ |
| 106 | +- (4)、(5)、(6) : [`emplace_hint`](emplace_hint.md) と同じ |
| 107 | +
|
| 108 | +
|
| 109 | +## 例 |
| 110 | +```cpp example |
| 111 | +#include <iostream> |
| 112 | +#include <flat_map> |
| 113 | +#include <string> |
| 114 | +
|
| 115 | +int main() |
| 116 | +{ |
| 117 | + std::flat_map<std::string, int> fm; |
| 118 | +
|
| 119 | + auto [it1, b1] = fm.insert_or_assign("foo", 42); |
| 120 | + std::cout << '(' << it1->first << ", " << it1->second << "), " << std::boolalpha << b1 << '\n'; |
| 121 | +
|
| 122 | + auto [it2, b2] = fm.insert_or_assign("foo", 0); |
| 123 | + std::cout << '(' << it2->first << ", " << it2->second << "), " << std::boolalpha << b2 << '\n'; |
| 124 | +} |
| 125 | +``` |
| 126 | +* insert_or_assign[color ff0000] |
| 127 | + |
| 128 | +### 出力 |
| 129 | +``` |
| 130 | +(foo, 42), true |
| 131 | +(foo, 0), false |
| 132 | +``` |
| 133 | + |
| 134 | +## バージョン |
| 135 | +### 言語 |
| 136 | +- C++23 |
| 137 | + |
| 138 | +### 処理系 |
| 139 | +- [Clang](/implementation.md#clang): ?? |
| 140 | +- [GCC](/implementation.md#gcc): ?? |
| 141 | +- [Visual C++](/implementation.md#visual_cpp): ?? |
| 142 | + |
| 143 | + |
| 144 | +## 関連項目 |
| 145 | + |
| 146 | +| 名前 | 説明 | |
| 147 | +|------------------------------------------------|--------------------------------------------| |
| 148 | +| [`flat_map::insert`](insert.md) | 要素を挿入する | |
| 149 | +| [`flat_map::insert_range`](insert_range.md) | Rangeを挿入する | |
| 150 | +| [`flat_map::emplace`](emplace.md) | 要素を直接構築する | |
| 151 | +| [`flat_map::emplace_hint`](emplace_hint.md) | ヒントを使って要素を直接構築する | |
| 152 | +| [`flat_map::try_emplace`](try_emplace.md) | キーが存在しない場合のみ要素を直接構築する | |
| 153 | + |
0 commit comments