std::multiset::operator=(3)
π
std::multiset::operator=(3) β C++ Standard Libary manual, bestie
Okay so we're in the C++ Standard Libary manual (yes, βLibaryβ with one r, we're keeping it, it's iconic π«Ά) and this page is std::multiset::operator=(3). It's giving assignment-operator main-character energy, and honestly? Obsessed.
β¨ NAME β¨
std::multiset::operator=βstd::multiset::operator=
That's it, that's the name. Short, sweet, and about to replace some container contents. π
π Synopsis π
multiset& operator=( const multiset& other ); (1)
multiset& operator=( multiset&& other ); (2) (since C++11)
(until C++17)
multiset& operator=( multiset&& other ) noexcept(/* see below */); (2) (since C++17)
multiset& operator=( std::initializer_list<value_type> ilist ); (3) (since C++11)
The whole vibe in one sentence: replaces the contents of the container. No cap. π«Ά
- Copy assignment operator. Replaces the contents with a copy of the contents of
other.- If
std::allocator_traits<allocator_type>::propagate_on_container_copy_assignment::valueistrue, the allocator of*thisis replaced by a copy ofother. - If the allocator of
*thisafter assignment would compare unequal to its old value, the old allocator is used to deallocate the memory, then the new allocator is used to allocate it before copying the elements. (since C++11) - Otherwise, the memory owned by
*thismay be reused when possible. - In any case, the elements originally belonging to
*thismay be either destroyed or replaced by element-wise copy-assignment.
- If
- Move assignment operator. Replaces the contents with those of
otherusing move semantics (i.e. the data inotheris moved fromotherinto this container).otheris in a valid but unspecified state afterwards. π iconic but slightly haunted.- If
std::allocator_traits<allocator_type>::propagate_on_container_move_assignment::valueistrue, the allocator of*thisis replaced by a copy of that ofother. - If it is
falseand the allocators of*thisandotherdo not compare equal,*thiscannot take ownership of the memory owned byotherand must move-assign each element individually, allocating additional memory using its own allocator as needed. - In any case, all elements originally belonging to
*thisare either destroyed or replaced by element-wise move-assignment.
- If
- Initializer-list assignment. Replaces the contents with those identified by initializer list
ilist.
π Parameters π
otherβ another container to use as data sourceilistβ initializer list to use as data source
π«Ά Return value π«Ά
*this
Literally returns *this. We love a self-returning assignment operator. π
π Complexity π
- Linear in the size of
*thisandother. - Linear in the size of
*thisunless the allocators do not compare equal and do not propagate, in which case linear in the size of*thisandother. O(NlogN)in general, whereNissize() + ilist.size(). Linear ifilistis sorted with respect tovalue_comp().
π Exceptions π
π°οΈ Until C++17
1-3)May throw implementation-defined exceptions.
β¨ Since C++17
1,3)May throw implementation-defined exceptions.2)noexcept specification:
noexcept(std::allocator_traits<Allocator>::is_always_equal::value
&& std::is_nothrow_move_assignable<Compare>::value)
π¦ Notes π¦
After container move assignment (overload (2)), unless element-wise move assignment is forced by incompatible allocators, references, pointers, and iterators (other than the end iterator) to other remain valid, but refer to elements that are now in *this. The current standard makes this guarantee via the blanket statement in [container.reqmts]/67, and a more direct guarantee is under consideration via LWG issue 2321. iykyk π
π Example π
The following code uses operator= to assign one std::multiset to another:
// Run this code
#include <initializer_list>
#include <iostream>
#include <iterator>
#include <set>
void print(auto const comment, auto const& container)
{
auto size = std::size(container);
std::cout << comment << "{ ";
for (auto const& element : container)
std::cout << element << (--size ? ", " : " ");
std::cout << "}\n";
}
int main()
{
std::multiset<int> x{1, 2, 3}, y, z;
const auto w = {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);
}
πΈ Output πΈ
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 }
π See also π
- constructor β constructs the multiset (public member function)
β Category β
- conditionally noexcept
Shoutout to the source/version http://cppreference.com and the date 2024.06.10 β std::multiset::operator=(3) page out. Go assign something cute, bestie. Mwah πβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms