std::multiset::swap(3)
π std::multiset::swap
okay so bestie, welcome to the std::multiset::swap(3) glow-up page β this is a C++ Standard Library situation and it's honestly iconic. It's the member function that lets two multisets trade lives. No cap.
β¨ NAME β¨
std::multiset::swap β literally std::multiset::swap. that's the whole name, she's not doing extra π
πΈ SYNOPSIS πΈ
watch this, because the signature got a whole era change in C++17:
void swap( multiset& other ); (until C++17)
void swap( multiset& other ) noexcept(/* see below */); (since C++17)
π¦ DESCRIPTION π¦
So what does she do. She exchanges the contents of the container with those of other. That's it, that's the job. π«Ά
And the way she does it is lowkey elegant: she does not invoke any move, copy, or swap operations on individual elements. Not one. The elements stay exactly where they are, the containers just... trade. main character energy.
All iterators and references remain valid. π Every single one. The only casualty? The end() iterator is invalidated. rip to end() specifically, everybody else is fine.
The Compare objects must be Swappable, and they are exchanged using an unqualified call to non-member swap. so the comparators get swapped too, iykyk.
Now the allocator bit β stay with me, this is the nerdy part π:
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). π That's a since C++11 rule, just so you know which era we're in.
β οΈ fr fr though β that "behavior is undefined" is not a vibe. Don't be swapping containers with unequal allocators when the propagate trait is false. That's undefined behavior territory and we do NOT go there.
π PARAMETERS π
otherβ container to exchange the contents with π
π RETURN VALUE π
(none) β she gives you nothing back, this is a pure side-effect queen β¨
π EXCEPTIONS π
Any exception thrown by the swap of the Compare objects. that's the whole list, she's tidy. (until C++17)
and since C++17, here's the noexcept specification:
noexcept(std::allocator_traits<Allocator>::is_always_equal::value
&& std::is_nothrow_swappable<Compare>::value)
(since C++17) β look at that conditional noexcept, she's gorgeous π§
β COMPLEXITY β
Constant. π«Ά Yes bestie, constant. She's not looping over your elements, remember? That's the whole flex.
π₯ EXAMPLE π₯
// Run this code β go on, run it, you know you want to π
#include <functional>
#include <iostream>
#include <set>
template<class Os, class Co>
Os& operator<<(Os& os, const Co& co)
{
os << '{';
for (auto const& i : co)
os << ' ' << i;
return os << " } ";
}
int main()
{
std::multiset<int> a1{3, 1, 3, 2}, a2{5, 4, 5};
auto it1 = std::next(a1.begin());
auto it2 = std::next(a2.begin());
const int& ref1 = *(a1.begin());
const int& ref2 = *(a2.begin());
std::cout << a1 << a2 << *it1 << ' ' << *it2 << ' ' << ref1 << ' ' << ref2 << '\n';
a1.swap(a2);
std::cout << a1 << a2 << *it1 << ' ' << *it2 << ' ' << ref1 << ' ' << ref2 << '\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.
struct Cmp : std::less<int>
{
int id{};
Cmp(int i) : id{i} {}
};
std::multiset<int, Cmp> s1{{2, 2, 1, 1}, Cmp{6}}, s2{{4, 4, 3, 3}, Cmp{9}};
std::cout << s1 << s2 << s1.key_comp().id << ' ' << s2.key_comp().id << '\n';
s1.swap(s2);
std::cout << s1 << s2 << s1.key_comp().id << ' ' << s2.key_comp().id << '\n';
// So, comparator objects (Cmp) are also exchanged after the swap.
}
Output: π
{ 1 2 3 3 } { 4 5 5 } 2 5 1 4
{ 4 5 5 } { 1 2 3 3 } 2 5 1 4
{ 1 1 2 2 } { 3 3 4 4 } 6 9
{ 3 3 4 4 } { 1 1 2 2 } 9 6
look at those iterators and references still pointing at the exact same elements after the swap β they literally did not move, the containers moved around them. and the Cmp ids went 6 9 β 9 6, so yeah, the comparators got exchanged too. we been knew π
π SEE ALSO π
std::swap(std::multiset)β specializes thestd::swapalgorithm (function template) β¨
π CATEGORY π
* conditionally noexceptβ yeah she's in that category, conditional queen π
that's the whole page bestie, mwah π«Ά β brought to you by cppreference.com, version 2024.06.10, and std::multiset::swap(3) herself. periodt. π
β¨ππ¦πΈ
π¦ 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