std::swap(std::map)(3)
π 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 thenoexceptthing 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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms