std::swap(s...d_multimap)(3)
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
- 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