std::multiset::swap(3)

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

πŸ’… std::multiset::swap

okay so bestie, welcome to the std::multiset::swap(3) glow-up page β€” this is a C++ Standard Library situation and it's honestly iconic. It's the member function that lets two multisets trade lives. No cap.


✨ NAME ✨

std::multiset::swap β€” literally std::multiset::swap. that's the whole name, she's not doing extra πŸ’…


🌸 SYNOPSIS 🌸

watch this, because the signature got a whole era change in C++17:

void swap( multiset& other );                            (until C++17)
void swap( multiset& other ) noexcept(/* see below */);  (since C++17)

πŸ¦‹ DESCRIPTION πŸ¦‹

So what does she do. She exchanges the contents of the container with those of other. That's it, that's the job. 🫢

And the way she does it is lowkey elegant: she does not invoke any move, copy, or swap operations on individual elements. Not one. The elements stay exactly where they are, the containers just... trade. main character energy.

All iterators and references remain valid. πŸ‘‘ Every single one. The only casualty? The end() iterator is invalidated. rip to end() specifically, everybody else is fine.

The Compare objects must be Swappable, and they are exchanged using an unqualified call to non-member swap. so the comparators get swapped too, iykyk.

Now the allocator bit β€” stay with me, this is the nerdy part πŸ’€:

If

std::allocator_traits<allocator_type>::propagate_on_container_swap::value

is true, then the allocators are exchanged using an unqualified call to non-member swap. Otherwise, they are not swapped (and if get_allocator() != other.get_allocator(), the behavior is undefined). 😭 That's a since C++11 rule, just so you know which era we're in.

⚠️ fr fr though β€” that "behavior is undefined" is not a vibe. Don't be swapping containers with unequal allocators when the propagate trait is false. That's undefined behavior territory and we do NOT go there.

πŸŽ€ PARAMETERS πŸŽ€

  • other β€” container to exchange the contents with πŸ“

🌈 RETURN VALUE 🌈

(none) β€” she gives you nothing back, this is a pure side-effect queen ✨


πŸ’œ EXCEPTIONS πŸ’œ

Any exception thrown by the swap of the Compare objects. that's the whole list, she's tidy. (until C++17)

and since C++17, here's the noexcept specification:

noexcept(std::allocator_traits<Allocator>::is_always_equal::value
         && std::is_nothrow_swappable<Compare>::value)

(since C++17) β€” look at that conditional noexcept, she's gorgeous 🧚


⭐ COMPLEXITY ⭐

Constant. 🫢 Yes bestie, constant. She's not looping over your elements, remember? That's the whole flex.


πŸ”₯ EXAMPLE πŸ”₯

// Run this code β€” go on, run it, you know you want to πŸ‘‘

#include <functional>
#include <iostream>
#include <set>

template<class Os, class Co>
Os& operator<<(Os& os, const Co& co)
{
    os << '{';
    for (auto const& i : co)
        os << ' ' << i;
    return os << " } ";
}

int main()
{
    std::multiset<int> a1{3, 1, 3, 2}, a2{5, 4, 5};

    auto it1 = std::next(a1.begin());
    auto it2 = std::next(a2.begin());

    const int& ref1 = *(a1.begin());
    const int& ref2 = *(a2.begin());

    std::cout << a1 << a2 << *it1 << ' ' << *it2 << ' ' << ref1 << ' ' << ref2 << '\n';
    a1.swap(a2);
    std::cout << a1 << a2 << *it1 << ' ' << *it2 << ' ' << ref1 << ' ' << ref2 << '\n';

    // Note that every iterator referring to an element in one container before the swap
    // refers to the same element in the other container after the swap. Same is true
    // for references.

    struct Cmp : std::less<int>
    {
        int id{};
        Cmp(int i) : id{i} {}
    };

    std::multiset<int, Cmp> s1{{2, 2, 1, 1}, Cmp{6}}, s2{{4, 4, 3, 3}, Cmp{9}};

    std::cout << s1 << s2 << s1.key_comp().id << ' ' << s2.key_comp().id << '\n';
    s1.swap(s2);
    std::cout << s1 << s2 << s1.key_comp().id << ' ' << s2.key_comp().id << '\n';

    // So, comparator objects (Cmp) are also exchanged after the swap.
}

Output: πŸŽ€

{ 1 2 3 3 } { 4 5 5 } 2 5 1 4
{ 4 5 5 } { 1 2 3 3 } 2 5 1 4
{ 1 1 2 2 } { 3 3 4 4 } 6 9
{ 3 3 4 4 } { 1 1 2 2 } 9 6

look at those iterators and references still pointing at the exact same elements after the swap β€” they literally did not move, the containers moved around them. and the Cmp ids went 6 9 β†’ 9 6, so yeah, the comparators got exchanged too. we been knew πŸ’…


🐚 SEE ALSO 🐚

  • std::swap(std::multiset) β€” specializes the std::swap algorithm (function template) ✨

🌟 CATEGORY 🌟

  • * conditionally noexcept β€” yeah she's in that category, conditional queen πŸ’–

that's the whole page bestie, mwah 🫢 β€” brought to you by cppreference.com, version 2024.06.10, and std::multiset::swap(3) herself. periodt. πŸ’…βœ¨πŸŽ€πŸ¦‹πŸŒΈ

πŸ“¦ where this page comes from