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

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

okay bestie, this is from the C++ Standard Library manual, and we're talking about std::swap for std::unordered_multimap β€” yes, the whole entire mouthful πŸ’…

✨ NAME ✨

std::swap(std::unordered_multimap) β€” literally std::swap(std::unordered_multimap), that's the name, no cap 🫢

πŸ¦‹ SYNOPSIS πŸ¦‹

Defined in header <unordered_map>.

Overload one, (since C++11) and (until C++17):

template< class Key, class T, class Hash, class KeyEqual, class Alloc >
void swap( std::unordered_multimap<Key, T, Hash, KeyEqual, Alloc>& lhs,
           std::unordered_multimap<Key, T, Hash, KeyEqual, Alloc>& rhs );

Overload two, (since C++17):

template< class Key, class T, class Hash, class KeyEqual, class Alloc >
void swap( std::unordered_multimap<Key, T, Hash, KeyEqual, Alloc>& lhs,
           std::unordered_multimap<Key, T, Hash, KeyEqual, Alloc>& rhs )
           noexcept(/* see below */);

So what's the tea? It specializes the std::swap algorithm for std::unordered_multimap. It swaps the contents of lhs and rhs. And it calls lhs.swap(rhs) β€” that's the whole move, iconic behavior only πŸ’–

πŸŽ€ PARAMETERS πŸŽ€

  • lhs, rhs β€” containers whose contents to swap. yes, both of them, the two little unordered-multimap besties getting their contents traded πŸ’«

🌸 RETURN VALUE 🌸

(none) β€” literally nothing comes back. void energy. no return value, just vibes πŸŽ€

⭐ COMPLEXITY ⭐

Constant. no scaling drama, periodt ✨

πŸ’œ EXCEPTIONS πŸ’œ

noexcept specification: (since C++17) noexcept(noexcept(lhs.swap(rhs)))

that's the conditionally-noexcept situation, iykyk πŸ’…

πŸ“ EXAMPLE πŸ“

the page says // Run this code before the snippet, and here's the snippet exactly:

#include <algorithm>
#include <iostream>
#include <unordered_map>

int main()
{
    std::unordered_multimap<int, char> alice{{1, 'a'}, {2, 'b'}, {3,
'c'}};
    std::unordered_multimap<int, char> bob{{7, 'Z'}, {8, 'Y'}, {9,
'X'}, {10, 'W'}};

    auto print = [](std::pair<const int, char>& n)
    {
        std::cout << ' ' << n.first << ':' << n.second;
    };

    // 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';
}

Possible output:

Alice: 1:a 2:b 3:c
Bob  : 7:Z 8:Y 9:X 10:W
-- SWAP
Alice: 7:Z 8:Y 9:X 10:W
Bob  : 1:a 2:b 3:c

like look at Alice and Bob swapping their whole little collections, we love a clean handoff πŸ˜­πŸ’–

🌈 SEE ALSO 🌈

  • swap β€” swaps the contents, and it's a public member function. yup, that's the one 🫢

πŸ‘‘ CATEGORY πŸ‘‘

  • * conditionally noexcept

cute little sign-off bestie: source/version/date is http://cppreference.com, 2024.06.10, and the footer title is std::swap(s...d_multimap)(3) β€” byeeee, go swap something safely πŸ’‹βœ¨

πŸ“¦ where this page comes from