std::unordered_map::swap(3)
π std::unordered_map::swap π
okay so welcome to std::unordered_map::swap(3) from the C++ Standard Libary manual β yes bestie, thatβs the exact spelling up top, keep it cute π. This is the page for swapping two unordered maps, and itβs lowkey iconic.
β¨ NAME β¨
std::unordered_map::swap β std::unordered_map::swap
yes, the name is literally the same on both sides. main character energy fr fr.
π¦ Synopsis π¦
two signature eras, two vibes:
void swap( unordered_map& other ); (since C++11)
(until C++17)
void swap( unordered_map& other ) noexcept(/* see below */); (since C++17)
πΈ Parameters πΈ
otherβ the container youβre exchanging contents with π«Ά
π Return value π
(none)
no return, just vibes.
π Exceptions π
- Any exception thrown by the swap of the
HashorKeyEqualobjects.(until C++17)
noexcept specification, and yes itβs giving very specific:
noexcept(std::allocator_traits<Allocator>::is_always_equal::value
&& std::is_nothrow_swappable<Hash>::value
&& std::is_nothrow_swappable<key_equal>::value)
(since C++17)
β Complexity β
Constant. periodt.
π§ Example π§
run this code and watch the swap do its little magic trick:
// Run this code
#include <iostream>
#include <string>
#include <utility>
#include <unordered_map>
// print out a std::pair
template<class Os, class U, class V>
Os& operator<<(Os& os, const std::pair<U, V>& p)
{
return os << p.first << ':' << p.second;
}
// print out a container
template<class Os, class Co>
Os& operator<<(Os& os, const Co& co)
{
os << '{';
for (auto const& i : co)
os << ' ' << i;
return os << " }\n";
}
int main()
{
std::unordered_map<std::string, std::string>
m1{{"Ξ³", "gamma"}, {"Ξ²", "beta"}, {"Ξ±", "alpha"}, {"Ξ³", "gamma"}},
m2{{"Ξ΅", "epsilon"}, {"Ξ΄", "delta"}, {"Ξ΅", "epsilon"}};
const auto& ref = *(m1.begin());
const auto iter = std::next(m1.cbegin());
std::cout << "ββββββββ before swap ββββββββ\n"
<< "m1: " << m1 << "m2: " << m2 << "ref: " << ref
<< "\niter: " << *iter << '\n';
m1.swap(m2);
std::cout << "ββββββββ after swap ββββββββ\n"
<< "m1: " << m1 << "m2: " << m2 << "ref: " << ref
<< "\niter: " << *iter << '\n';
// Note that every iterator referring to an element in one container before
// the swap refers to the same element in the other container after the swap.
// Same is true for references.
}
π Possible output π
ββββββββ before swap ββββββββ
m1: { Ξ±:alpha Ξ²:beta Ξ³:gamma }
m2: { Ξ΄:delta Ξ΅:epsilon }
ref: Ξ±:alpha
iter: Ξ²:beta
ββββββββ after swap ββββββββ
m1: { Ξ΄:delta Ξ΅:epsilon }
m2: { Ξ±:alpha Ξ²:beta Ξ³:gamma }
ref: Ξ±:alpha
iter: Ξ²:beta
π See also π
std::swap(std::unordered_map)β specializes thestd::swapalgorithm(C++11)(function template)
π Category π
* conditionally noexcept
okay bestie, real talk: this swap exchanges the contents of the container with those of other. It does not invoke any move, copy, or swap operations on individual elements. Zero touching of the little guys π
.
All iterators and references remain valid. The end() iterator is invalidated β so donβt get attached to that one π.
TheHashandKeyEqualobjects must beSwappable, and they are exchanged using unqualified calls to non-memberswap.
Ifstd::allocator_traits<allocator_type>::propagate_on_container_swap::valueis true, then the allocators are exchanged using an unqualified call to non-memberswap. Otherwise, they are not swapped (and ifget_allocator() != other.get_allocator(), the behavior is undefined)(since C++11). undefined behavior is not the vibe, bestie π.
xoxo, your local Unix bestie π β source/version/date check: http://cppreference.com, 2024.06.10, and this iconic page is std::unordered_map::swap(3) π«Άβ¨
π¦ 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