Template:cpp/container/operator=

Replaces the contents of the container.

@1@ Copy assignment operator. Replaces the contents with a copy of the contents of.

@2@ Move assignment operator. Replaces the contents with those of using move semantics (i.e. the data in  is moved from  into this container). is in a valid but unspecified state afterwards. @@ If is, the allocator of  is replaced by a copy of that of. If it is and the allocators of  and  do not compare equal,  cannot take ownership of the memory owned by  and must move-assign each element individually, allocating additional memory using its own allocator as needed. In any case, all elements originally belonging to are either destroyed or replaced by element-wise move-assignment.

@3@ Replaces the contents with those identified by initializer list.

Complexity
@1@ Linear in the size of and. @2@ Linear in the size of unless the allocators do not compare equal and do not propagate, in which case linear in the size of  and.

Example
{{example }}
 * The following code uses {{c|1=operator=}} to assign one {{lc|std::}} to another:
 * code=
 * 1) include <{{cpp/container/get header| }}>
 * 2) include
 * 3) include {{cpp/container/if map||
 * 4) include
 * 5) include 

void print(auto const comment, auto const& container) {   auto size = {{#ifeq:{{{1|}}}|forward_list|std::ranges::distance(container)|std::size(container)}}; std::cout << comment << "{ "; {{cpp/container/if map| |   for (auto const& [key, value]: container) std::cout << '{' << key << ',' << value << (--size ? "}, " : "} "); |   for (auto const& element: container) std::cout << element << (--size ? ", " : " "); }}   std::cout << "}\n"; }

int main {   std::, y, z;    const auto w = { {{cpp/container/if map|{{{1|map}}}| std::pair{4,4}, {5,5}, {6,6}, {7,7} | 4, 5, 6, 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); } | p={{cpp/container/if unord|{{{1|map}}}|true|false}} | output= {{#switch: {{{1|map}}} |map|multimap= 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} } |unordered_map|unordered_multimap= 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} } |unordered_set|unordered_multiset= Initially: x = { 3, 2, 1 } y = { } z = { } Copy assignment copies data from x to y: x = { 3, 2, 1 } y = { 3, 2, 1 } Move assignment moves data from x to z, modifying both x and z: x = { } z = { 3, 2, 1 } Assignment of initializer_list w to z: w = { 4, 5, 6, 7 } z = { 7, 6, 5, 4 } |#default= Initially: x = { 1, 2, 3 } y = { } z = { } Copy assignment copies data from x to y: x = { 1, 2, 3 } y = { 1, 2, 3 } Move assignment moves data from x to z, modifying both x and z: x = { } z = { 1, 2, 3 } Assignment of initializer_list w to z: w = { 4, 5, 6, 7 } z = { 4, 5, 6, 7 } }} }}