std::unorde...t::operator=(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… std::unordered_set::operator=

Okay so bestie, this is page (3) from the C++ Standard Library section of the manual, and it's all about std::unordered_set::operator= β€” the assignment operator. Iconic, lowkey underrated, and yes it does exactly what you think: it replaces the contents of the container. Slay. Let's get into it 🫢


🌸 NAME 🌸

std::unordered_set::operator= β€” yep, that's literally the whole name, it's giving "I contain multitudes but my name is just my job title" πŸ’€


✨ SYNOPSIS ✨

unordered_set& operator=( const unordered_set& other );        (1) (since C++11)

unordered_set& operator=( unordered_set&& other );             (2) (since C++11)
                                                                   (until C++17)

unordered_set& operator=( unordered_set&& other )
    noexcept(/* see below */);                                 (2) (since C++17)

unordered_set& operator=( std::initializer_list<value_type> ilist );  (3) (since C++11)

Replaces the contents of the container. That's the whole vibe. Three flavors, one job πŸ’–

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

Okay so here's the tea 🍡 β€” if std::allocator_traits<allocator_type>::propagate_on_container_copy_assignment::value is true, then the allocator of *this gets replaced by a copy of other. And if the allocator of *this after assignment would compare unequal to its old value, then the old allocator is used to deallocate the memory, and then the new allocator is used to allocate it before copying the elements. If none of that drama applies, the memory owned by *this may be reused when possible. Either way, in any case, the elements originally belonging to *this may be either destroyed or replaced by element-wise copy-assignment. No cap.

  1. Move assignment operator. Replaces the contents with those of other using move semantics (i.e. the data in other is moved from other into this container). other is in a valid but unspecified state afterwards. So like… it's alive, it's just not okay 😭

And here's the allocator lore: if std::allocator_traits<allocator_type>::propagate_on_container_move_assignment::value is true, the allocator of *this is replaced by a copy of that of other. If it is false and the allocators of *this and other do not compare equal, then *this cannot take ownership of the memory owned by other and must move-assign each element individually, allocating additional memory using its own allocator as needed. In any case, all elements originally belonging to *this are either destroyed or replaced by element-wise move-assignment. Fr fr, that's the whole clause.

  1. Replaces the contents with those identified by initializer list ilist. Short, sweet, aesthetic πŸŽ€

πŸ¦‹ PARAMETERS πŸ¦‹

  • other β€” another container to use as data source
  • ilist β€” initializer list to use as data source

πŸ’œ RETURN VALUE πŸ’œ

*this

Not me returning myself. Main character energy πŸ‘‘


πŸ”₯ COMPLEXITY πŸ”₯

  1. Linear in the size of *this and other.
  2. Linear in the size of *this unless the allocators do not compare equal and do not propagate, in which case linear in the size of *this and other.
  3. Linear in the size of *this and ilist.

🌈 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<Hash>::value
         && std::is_nothrow_move_assignable<Pred>::value)

Look at that boolean chain. She's gorgeous, she's conditionally noexcept, she knows it ✨


⭐ 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.

So yeah β€” your iterators survive the move, they just moved house 🏠 iykyk


🧚 EXAMPLE 🧚

The following code uses operator= to assign one std::unordered_set to another:

#include <initializer_list>
#include <iostream>
#include <iterator>
#include <unordered_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::unordered_set<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);
}

Possible output:

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 }

Notice how x gets absolutely emptied out by the move β€” not me watching x = { } and feeling a little bit bad for her πŸ˜­πŸ’€ unordered_set ordering is unspecified though, so don't come for me if your numbers shuffle, that's just the hash table doing her thing 🌸


πŸ‘‘ SEE ALSO πŸ‘‘

  • constructor β€” constructs the unordered_set (public member function)

πŸŽ€ CATEGORY πŸŽ€

  • * conditionally noexcept

That's the whole page bestie, every clause, every allocator clause, every true/false, nothing skipped πŸ’– This adorable little rewrite comes to you courtesy of http://cppreference.com, version 2024.06.10, dated 2024.06.10 β€” std::unordered_set::operator= page (3), signing off πŸ«Άβœ¨πŸ’…

πŸ“¦ where this page comes from