std::swap(s...ordered_set)(3)
π std::swap(s...ordered_set)(3) β live from the C++ Standard Libary manual β¨
πΈ NAME πΈ
std::swap(std::unordered_set) β yep, thatβs the whole name: std::swap(std::unordered_set). Iconic, honestly.
β¨ Synopsis β¨
Defined in header <unordered_set>.
template< class Key, class Hash, class KeyEqual, class Alloc >
void swap( std::unordered_set<Key, Hash, KeyEqual, Alloc>& lhs,
std::unordered_set<Key, Hash, KeyEqual, Alloc>& rhs );
(since C++11) Β· (until C++17) β³
template< class Key, class Hash, class KeyEqual, class Alloc >
void swap( std::unordered_set<Key, Hash, KeyEqual, Alloc>& lhs,
std::unordered_set<Key, Hash, KeyEqual, Alloc>& rhs )
noexcept(/* see below */);
(since C++17) β¨
Okay so this is the specialized std::swap algorithm for std::unordered_set: it swaps the contents of lhs and rhs. The actual call it makes is lhs.swap(rhs). Slay.
π Parameters π
lhs,rhsβ containers whose contents to swap. Yes, both of them, bestie.
π«Ά Return value π«Ά
(none) β literally nothing comes back.
β‘ Complexity β‘
Constant. No cap.
π Exceptions π
noexcept specification: (since C++17) noexcept(noexcept(lhs.swap(rhs))).
π§ Example π§
// Run this code πββοΈπ¨
#include <algorithm>
#include <iostream>
#include <unordered_set>
int main()
{
std::unordered_set<int> alice{1, 2, 3};
std::unordered_set<int> bob{7, 8, 9, 10};
auto print = [](const int& n) { std::cout << ' ' << n; };
// 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 2 3
Bob : 7 8 9 10
-- SWAP
Alice: 7 8 9 10
Bob : 1 2 3
π See also π
swapβ swaps the contents (public member function).
π·οΈ Category π·οΈ
* conditionally noexcept
Source/version and date, folded into a cute little sign-off: http://cppreference.com π Β· 2024.06.10 π Β· manual name C++ Standard Libary Β· page std::swap(s...ordered_set)(3). Byeee, bestie! π
β¨
π¦ 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