std::swap(std::set)(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… NAME

std::swap(std::set) β€” ok bestie, this is literally the std::swap that's custom-built for std::set ✨ (like, the C++ Standard Library one, section 3 of the manual, iykyk πŸŽ€)


🌸 Synopsis

So the way this works: it lives in the header <set>, and there's a version for before C++17 and a version for after. Both slay, they just have different vibes 😌

template< class Key, class Compare, class Alloc >

void swap( std::set<Key, Compare, Alloc>& lhs,     (until C++17)

           std::set<Key, Compare, Alloc>& rhs );
template< class Key, class Compare, class Alloc >

void swap( std::set<Key, Compare, Alloc>& lhs,     (since C++17)
           std::set<Key, Compare, Alloc>& rhs )

               noexcept(/* see below */);

What it does, fr fr: it specializes the std::swap algorithm for std::set, and it swaps the contents of lhs and rhs πŸ”₯

And underneath? It just calls lhs.swap(rhs). That's it. That's the whole personality πŸ’–


🫢 Parameters

  • lhs, rhs β€” the containers whose contents to swap. Yes, both of them. You need two πŸ’…

⭐ Return value

(none) β€” nothing, nada, it returns void, it's giving "my job here is done" energy πŸ™ˆ


🌈 Complexity

Constant. O(1) babe, it's iconic, it does not care how big your sets are πŸ‘‘


πŸ¦‹ Exceptions

The noexcept specification (since C++17, of course ✨):

noexcept(noexcept(lhs.swap(rhs)))

So it's noexcept exactly when lhs.swap(rhs) is noexcept. Vibe check passed βœ…


πŸŽ€ Example

Run this code, bestie, it's a whole little story about Alice and Bob 😭

// Run this code

#include <algorithm>
#include <iostream>
#include <set>

int main()
{
    std::set<int> alice{1, 2, 3};
    std::set<int> bob{7, 8, 9, 10};

    auto print = [](const int& n) { std::cout << ' ' << n; };

    // Print state before swap
    std::cout << "Alice:";
    std::for_each(alice.begin(), alice.end(), print);
    std::cout << "\n" "Bob  :";
    std::for_each(bob.begin(), bob.end(), print);
    std::cout << '\n';

    std::cout << "-- SWAP\n";
    std::swap(alice, bob);

    // Print state after swap
    std::cout << "Alice:";
    std::for_each(alice.begin(), alice.end(), print);
    std::cout << "\n" "Bob  :";
    std::for_each(bob.begin(), bob.end(), print);
    std::cout << '\n';
}

✨ Output:

Alice: 1 2 3
Bob  : 7 8 9 10
-- SWAP
Alice: 7 8 9 10
Bob  : 1 2 3

Look at them go πŸ₯Ή Alice got Bob's numbers, Bob got Alice's, everybody ate πŸ’…


πŸ‘€ See also

  • swap β€” swaps the contents (public member function) 🧚 β€” the member-function version of the same idea, if you're feeling classy

🌸 Category:

  • conditionally noexcept πŸŽ€

ngl this one's short and sweet and lowkey perfect β€” swap your sets, keep your complexity constant, and never look back πŸ’œ

ok that's the whole page bestie, mwah 🫢 β€” this gem comes from http://cppreference.com, version 2024.06.10, and it's std::swap(std::set)(3), signing off βœ¨πŸ’–πŸŽ€

πŸ“¦ where this page comes from