std::multimap::swap(3)
π¦ std::multimap::swap(3) β C++ Standard Libary π¦
okay bestie, this is std::multimap::swap(3) from the C++ Standard Libary β yes, the header spells it like that, and we respect the typo π. it's the member swap for std::multimap, and it's about to trade whole container contents. let's go. β¨
π NAME π
std::multimap::swap β std::multimap::swap (the name section is literally the name twice, and honestly? iconic. π
)
β¨ Synopsis β¨
void swap( multimap& other ); (until C++17)
void swap( 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. no cap, it just trades the whole vibe. π₯
All iterators and references remain valid. The end() iterator is invalidated.
end() said βnot todayβ and dipped. π
The Compare objects must be Swappable, and they are exchanged using unqualified call to non-member swap. (yes, βunqualified call to non-member swapβ is the exact phrase β write it down, bestie. π)
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)
undefined behavior alert: if those allocators donβt match and the propagate value isnβt true, itβs giving βwe donβt know herβ and you should not go there. π
π«Ά Parameters π«Ά
otherβ container to exchange the contents with
πΈ Return value πΈ
(none) β literally no return value, bestie. π
π₯ Exceptions π₯
- Any exception thrown by the swap of the
Compareobjects. (until C++17) - noexcept specification:
noexcept(std::allocator_traits<Allocator>::is_always_equal::value && std::is_nothrow_swappable<Compare>::value)(since C++17)
β Complexity β
Constant. thatβs the whole complexity. π
π§ 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::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.
}
π 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::multimap)specializes thestd::swapalgorithm (function template)
π·οΈ Category π·οΈ
* conditionally noexceptβ yes, thatβs the category line, star and all. π
big mwah from http://cppreference.com, version/date 2024.06.10, on the std::multimap::swap(3) page β bye 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