std::map<Key,T,Compare,Allocator>:: operator=
|
map
&
operator
=
(
const
map
&
other
)
;
|
(1) | (constexpr since C++26) |
| (2) | ||
|
map
&
operator
=
(
map
&&
other
)
;
|
(C++11以降)
(C++17まで) |
|
|
map
&
operator
=
(
map
&&
other
)
noexcept ( /* see below */ ) ; |
(C++17以降)
(constexpr since C++26) |
|
|
map
&
operator
=
(
std::
initializer_list
<
value_type
>
ilist
)
;
|
(3) |
(C++11以降)
(constexpr since C++26) |
コンテナの内容を置き換えます。
traits
を
std::
allocator_traits
<
allocator_type
>
とします:
|
traits :: propagate_on_container_copy_assignment :: value が true の場合、 * this のアロケータは other のコピーで置き換えられる。代入後の * this のアロケータが古い値と等しくないと比較される場合、古いアロケータを使用してメモリを解放した後、新しいアロケータを使用して要素をコピーする前にメモリを確保する。それ以外の場合、 * this が所有するメモリは可能な場合に再利用される。いずれの場合も、元々 * this に属していた要素は、破棄されるか、要素ごとのコピー代入で置き換えられる可能性がある。 |
(C++11以降) |
目次パラメータ
戻り値* this 計算量
1)
*
this
と
other
のサイズに対して線形。
2)
アロケータが等価ではなく伝播しない場合を除き、
*
this
のサイズに対して線形。その場合、
*
this
と
other
のサイズに対して線形。
3)
O(N·log(N))
一般の場合、ここで
N
は
size
(
)
+
ilist.
size
(
)
です。
ilist
が
value_comp()
に関してソートされている場合は線形時間です。
例外
注記コンテナのムーブ代入(オーバーロード (2))後、非互換なアロケータによって要素単位のムーブ代入が強制されない限り、元々 例以下のコードは operator = を使用して、ある std::map を別のものに代入します:
このコードを実行
#include <initializer_list> #include <iostream> #include <iterator> #include <map> #include <utility> void print(const auto comment, const auto& container) { auto size = std::size(container); std::cout << comment << "{ "; for (const auto& [key, value] : container) std::cout << '{' << key << ',' << value << (--size ? "}, " : "} "); std::cout << "}\n"; } int main() { std::map<int, int> x{{1,1}, {2,2}, {3,3}}, y, z; const auto w = {std::pair<const int, int>{4,4}, {5,5}, {6,6}, {7,7}}; std::cout << "Initially:\n"; print("x = ", x); print("y = ", y); print("z = ", z); std::cout << "Copy assignment copies data from x to y:\n"; y = x; print("x = ", x); print("y = ", y); std::cout << "Move assignment moves data from x to z, modifying both x and z:\n"; z = std::move(x); print("x = ", x); print("z = ", z); std::cout << "Assignment of initializer_list w to z:\n"; z = w; print("w = ", w); print("z = ", z); } 出力: Initially:
x = { {1,1}, {2,2}, {3,3} }
y = { }
z = { }
Copy assignment copies data from x to y:
x = { {1,1}, {2,2}, {3,3} }
y = { {1,1}, {2,2}, {3,3} }
Move assignment moves data from x to z, modifying both x and z:
x = { }
z = { {1,1}, {2,2}, {3,3} }
Assignment of initializer_list w to z:
w = { {4,4}, {5,5}, {6,6}, {7,7} }
z = { {4,4}, {5,5}, {6,6}, {7,7} }
関連項目
|