std::unorde...ltimap::swap(3)
okay so this is the std::unorde...ltimap::swap(3) page from the C++ Standard Libary manual β title and manual name folded in right here, bestie π (yes, the header is truncated, thatβs the whole vibe).
πΈ NAME πΈ
std::unordered_multimap::swap β std::unordered_multimap::swap. Iconic, repetitive, we love her. π«Ά
β¨ Synopsis β¨
Two overload eras, bestie β pay attention to the (since C++11) / (until C++17) / (since C++17) labels:
void swap( unordered_multimap& other );
(since C++11) (until C++17)
void swap( unordered_multimap& other ) noexcept(/* see below */);
(since C++17)
Exchanges the contents of the container with those of other. Does not invoke any move, copy, or swap operations on individual elements. π«Ά
All iterators and references remain valid. The end() iterator is invalidated. This is the important bit, bestie.
The Hash and KeyEqual objects must be Swappable, and they are exchanged using unqualified calls to non-member swap.
If std::allocator_traits<allocator_type>::propagate_on_container_swap::value is true, then the allocators are exchanged using an unqualified call to non-member swap. Otherwise, they are not swapped β and if get_allocator() != other.get_allocator(), the behavior is undefined. π¨ (since C++11)
π Parameters π
otherβ container to exchange the contents with. Thatβs the parameter, thatβs the whole gig. π
π Return value π
(none) β literally nothing comes back. No return value, no drama. π
π¨ Exceptions π¨
Any exception thrown by the swap of the Hash or KeyEqual objects. (until C++17)
noexcept specification (since C++17):
noexcept(std::allocator_traits<Allocator>::is_always_equal::value
&& std::is_nothrow_swappable<Hash>::value
&& std::is_nothrow_swappable<key_equal>::value)
β‘ Complexity β‘
Constant. No cap, the complexity stays constant. β‘
π§ Example π§
// 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_multimap<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 Ξ³:gamma }
m2: { Ξ΄:delta Ξ΅:epsilon Ξ΅:epsilon }
ref: Ξ±:alpha
iter: Ξ²:beta
ββββββββ after swap ββββββββ
m1: { Ξ΄:delta Ξ΅:epsilon Ξ΅:epsilon }
m2: { Ξ±:alpha Ξ²:beta Ξ³:gamma Ξ³:gamma }
ref: Ξ±:alpha
iter: Ξ²:beta
π See also π
std::swap(std::unordered_multimap)β specializes thestd::swapalgorithm(C++11)(function template). Yes bestie, function template, periodt. π
π·οΈ Category π·οΈ
- conditionally noexcept
okay thatβs the whole page β mwah π sourced from http://cppreference.com, version/date 2024.06.10, and the footer title is that same truncated std::unorde...ltimap::swap(3) vibe. Byeeee π¦β¨
π¦ 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