std::map::swap(3)
okay bestie, this is std::map::swap(3) from the C++ Standard Libary manual β yes, that spelling is straight from the header, no cap π. It's the map-to-map content trade where the containers swap their whole vibe and the individual elements are left completely alone. Iconic.
β¨ NAME β¨
std::map::swap - std::map::swap
π Synopsis π
void swap( map& other ); (until C++17)
void swap( map& 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. Literally none, bestie π«Ά.
All iterators and references remain valid. The end() iterator is invalidated. That part is important, don't forget it π
.
The Compare objects must be Swappable, and they are exchanged using an unqualified call to non-member swap.
Since C++11: 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. π«
π Parameters π
otherβ container to exchange the contents with
π«Ά Return value π«Ά
(none) β literally nothing comes back, it just does the swap.
π Exceptions π
- Until C++17: any exception thrown by the swap of the
Compareobjects.
Since C++17, the noexcept specification is:
noexcept(std::allocator_traits<Allocator>::is_always_equal::value
&& std::is_nothrow_swappable<Compare>::value)
β‘ Complexity β‘
Constant β that's the whole complexity section, bestie β¨.
π Example π
// Run this code
#include <iostream>
#include <string>
#include <utility>
#include <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::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.
}
π€ 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::map)specializes thestd::swapalgorithm β (function template)
π·οΈ Category π·οΈ
- conditionally
noexcept
<cute>okay bye bestie</cute> π this was std::map::swap(3); source/version/date check: http://cppreference.com, 2024.06.10 π«Άβ¨
π¦ 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