std::swap(s...d_multiset)(3)

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

🌸 NAME 🌸

std::swap(std::unordered_multiset) β€” yeah bestie, that's literally the whole name, it's std::swap(std::unordered_multiset). No notes. πŸ’…

(This is page 3 of the C++ Standard Library section, btw, iykyk.)


✨ Synopsis ✨

Ok so first thing: this whole thing lives in the header <unordered_set> 🫢

Here's the first overload, and she's been around a minute β€” (since C++11) β€” and the version without the noexcept biz has been (until C++17), so keep that in mind:

template< class Key, class Hash, class KeyEqual, class Alloc >

void swap( std::unordered_multiset<Key, Hash, KeyEqual, Alloc>& lhs,
           std::unordered_multiset<Key, Hash, KeyEqual, Alloc>& rhs
);

And then (since C++17) we got the glow-up version that tells you about exceptions up front:

template< class Key, class Hash, class KeyEqual, class Alloc >

void swap( std::unordered_multiset<Key, Hash, KeyEqual, Alloc>& lhs,
           std::unordered_multiset<Key, Hash, KeyEqual, Alloc>& rhs )

                     noexcept(/* see below */);

So what's the tea? This specializes the std::swap algorithm for std::unordered_multiset πŸ’– It swaps the contents of lhs and rhs, and under the hood it just calls lhs.swap(rhs). That's it. That's the whole vibe. ✨


πŸŽ€ Parameters πŸŽ€

  • lhs, rhs β€” the containers whose contents we're swapping. The two besties exchanging fits. πŸ‘―β€β™€οΈ

πŸ’– Return value πŸ’–

(none) β€” she gives you nothing back, because the swapping was the gift. 😌


πŸ”₯ Complexity πŸ”₯

Constant. Not me being obsessed with how fast that is. ⚑


😭 Exceptions 😭

The noexcept specification, (since C++17):

noexcept(noexcept(lhs.swap(rhs)))

That's it, that's the whole clause, fr fr. 🫢


πŸ¦‹ Example πŸ¦‹

Run this code πŸƒβ€β™€οΈ
#include <algorithm>
#include <iostream>
#include <unordered_set>

int main()
{
    std::unordered_multiset<int> alice{1, 2, 3};
    std::unordered_multiset<int> bob{7, 8, 9, 10};

    auto print = [](const int& n) { std::cout << ' ' << n; };

    // Print state before swap
    std::cout << "Alice:";
    std::for_each(alice.begin(), alice.end(), print);
    std::cout << "\n" "Bob  :";
    std::for_each(bob.begin(), bob.end(), print);
    std::cout << '\n';

    std::cout << "-- SWAP\n";
    std::swap(alice, bob);

    // Print state after swap
    std::cout << "Alice:";
    std::for_each(alice.begin(), alice.end(), print);
    std::cout << "\n" "Bob  :";
    std::for_each(bob.begin(), bob.end(), print);
    std::cout << '\n';
}

Like, Alice and Bob are literally trading whole entire containers, iconic behavior honestly. πŸ‘‘


🌈 Possible output 🌈

Alice: 1 2 3
Bob  : 7 8 9 10
-- SWAP
Alice: 7 8 9 10
Bob  : 1 2 3

Not them fully switching lives πŸ˜­πŸ’€


πŸ‘‘ See also πŸ‘‘

  • swap β€” swaps the contents (public member function)

⭐ Category ⭐

  • conditionally noexcept

and that's the whole page, bestie, you ate that πŸ“

Sign-off time πŸŽ€ This little masterpiece lives over at http://cppreference.com, dated 2024.06.10 β€” go say hi, mwah πŸ’œπŸ§š

πŸ“¦ where this page comes from