std::swap(std::multiset)(3)

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

πŸ’– NAME πŸ’–

std::swap(std::multiset) - std::swap(std::multiset)

hiii bestie 🫢 this is the std::swap(std::multiset)(3) page from the C++ Standard Libary β€” manual name folded in near the top, spelling and all, no boring header-line copy-paste, we ate πŸ’….

✨ Synopsis ✨

okay so first things first: it’s defined in header <set>, and the signatures are giving β€œtwo eras of C++ walked in” 😭. The synopsis is exactly this, bestie:

Defined in header <set>
template< class Key, class Compare, class Alloc >

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

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

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

           noexcept(/* see below */);

so what’s the tea β˜• β€” this specializes the std::swap algorithm for std::multiset. It swaps the contents of lhs and rhs. And it does that by calling lhs.swap(rhs). periodt πŸ’….

🌸 Parameters 🌸

  • lhs, rhs β€” containers whose contents to swap

that’s the whole parameter list, bestie: two containers and a dream πŸŽ€.

πŸŽ€ Return value πŸŽ€

(none) β€” literally none, bestie. it returns nothing and it’s iconic for it πŸ’….

πŸ¦‹ Complexity πŸ¦‹

Constant. no cap, that’s the whole complexity note β€” constant, periodt ✨.

πŸ’… Exceptions πŸ’…

the noexcept specification is this, and it’s since C++17 bestie:

noexcept specification:           (since C++17)
noexcept(noexcept(lhs.swap(rhs)))

πŸ‘‘ Example πŸ‘‘

okay this example is lowkey the moment πŸ‘‘ β€” Alice and Bob are both std::multiset<int>, and we’re swapping them. Copy it exactly, bestie:

// Run this code

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

int main()
{
    std::multiset<int> alice{1, 2, 3};
    std::multiset<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

🫢 See also 🫢

  • swap β€” swaps the contents (public member function)

🧚 Category 🧚

  • conditionally noexcept

okay that’s the page, bestie 🫢 source/version: http://cppreference.com; date: 2024.06.10; footer page name: std::swap(std::multiset)(3). mwah πŸ’‹

πŸ“¦ where this page comes from