std::swap(std::map)(3)

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

πŸ’… std::swap(std::map) β€” bestie edition

okay so this is the std::swap(std::map) page, straight out of the C++ Standard Library manual (section 3, obviously πŸ’…). it's a specialization of std::swap for maps, and honestly? it's giving "small function, big main character energy." let's get into it 🫢


✨ NAME ✨

std::swap(std::map) β€” literally just std::swap(std::map). that's the whole name. iconic of it tbh.


πŸ¦‹ SYNOPSIS πŸ¦‹

first things first, bestie: it's Defined in header <map> πŸ’– so don't come crying to me when it doesn't compile without it.

template< class Key, class T, class Compare, class Alloc >

void swap( std::map<Key, T, Compare, Alloc>& lhs,           (until C++17)

           std::map<Key, T, Compare, Alloc>& rhs );

and then, (since C++17), our girl got a glow-up:

template< class Key, class T, class Compare, class Alloc >

void swap( std::map<Key, T, Compare, Alloc>& lhs,           (since C++17)

           std::map<Key, T, Compare, Alloc>& rhs )

              noexcept(/* see below */);

so what does it actually do? it specializes the std::swap algorithm for std::map, and it swaps the contents of lhs and rhs. that's it. that's the whole vibe. no cap ✨

and like, the way it works internally? it just calls lhs.swap(rhs). she's not doing anything extra, she's delegating. we love a queen who knows when to hand things off πŸ‘‘

🌸 PARAMETERS 🌸

  • lhs, rhs β€” the containers whose contents to swap. yes, both of them. you need two. it's a swap, babe, that's how it works 😭

πŸŽ€ RETURN VALUE πŸŽ€

(none) β€” she returns nothing. zero. void. it's giving mysterious, it's giving "my work here is done" 🧚


⭐ COMPLEXITY ⭐

Constant. like, O(1) constant. it's literally so fast it doesn't even have time to be complicated. obsessed πŸ”₯


🌈 EXCEPTIONS 🌈

here's the noexcept specification, and this part is (since C++17):

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

so basically its noexcept-ness is inherited from whatever lhs.swap(rhs) does. iykyk πŸ’œ


πŸ“ EXAMPLE πŸ“

okay, this is the good part β€” you get to run this code:

// Run this code

#include <algorithm>
#include <iostream>
#include <map>

int main()
{
    std::map<int, char> alice{{1, 'a'}, {2, 'b'}, {3, 'c'}};
    std::map<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';
}

πŸ’– 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

look at alice and bob swapping whole entire lives 😭 the way alice now has 7:Z 8:Y 9:X 10:W and bob has 1:a 2:b 3:c? we love a mutually beneficial trade fr fr 🫢


πŸ‘‘ SEE ALSO πŸ‘‘

  • swap β€” swaps the contents. (public member function). yes, that's it, that's the cross-reference. she's got one friend and that friend is herself πŸ’€

πŸ’€ CATEGORY πŸ’€

  • conditionally noexcept β€” that's the category, bestie. conditionally. because of the noexcept thing up above. we been knew.

🌸 real talk though: keep noexcept in mind, it's literally in the category list. it matters to people who care about exception specifications. which is a lot of people. πŸ’…

ok that's the page!! source is cppreference.com, dated 2024.06.10, and the whole thing answers to the name std::swap(std::map)(3). mwah, go swap something πŸ’–βœ¨πŸŽ€

πŸ“¦ where this page comes from