std::unordered_map<Key,T,Hash,KeyEqual,Allocator>:: operator=
|
unordered_map
&
operator
=
(
const
unordered_map
&
other
)
;
|
(1) |
(seit C++11)
(constexpr seit C++26) |
| (2) | ||
|
unordered_map
&
operator
=
(
unordered_map
&&
other
)
;
|
(seit C++11)
(bis C++17) |
|
|
unordered_map
&
operator
=
(
unordered_map
&&
other
)
noexcept ( /* siehe unten */ ) ; |
(seit C++17)
(constexpr seit C++26) |
|
|
unordered_map
&
operator
=
(
std::
initializer_list
<
value_type
>
ilist
)
;
|
(3) |
(seit C++11)
(constexpr seit C++26) |
Ersetzt den Inhalt des Containers.
Sei
traits
gleich
std::
allocator_traits
<
allocator_type
>
:
InhaltParameter
Rückgabewert* this Komplexität
1)
Linear in der Größe von
*
this
und
other
.
2)
Linear in der Größe von
*
this
, es sei denn, die Allokatoren vergleichen sich nicht gleich und propagieren nicht, in welchem Fall linear in der Größe von
*
this
und
other
.
3)
Linear in der Größe von
*
this
und
ilist
.
Exceptions
AnmerkungenNach der Container-Verschiebungszuweisung (Überladung (2)), sofern keine elementweise Verschiebungszuweisung durch inkompatible Allokatoren erzwungen wird, bleiben Referenzen, Zeiger und Iteratoren, die ursprünglich auf Elemente in BeispielDer folgende Code verwendet den operator = , um einen std::unordered_map einem anderen zuzuweisen:
Code ausführen
#include <initializer_list> #include <iostream> #include <iterator> #include <unordered_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::unordered_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); } Mögliche Ausgabe: Initially:
x = { {3,3}, {2,2}, {1,1} }
y = { }
z = { }
Copy assignment copies data from x to y:
x = { {3,3}, {2,2}, {1,1} }
y = { {3,3}, {2,2}, {1,1} }
Move assignment moves data from x to z, modifying both x and z:
x = { }
z = { {3,3}, {2,2}, {1,1} }
Assignment of initializer_list w to z:
w = { {4,4}, {5,5}, {6,6}, {7,7} }
z = { {7,7}, {6,6}, {5,5}, {4,4} }
Siehe auch
|