std::swap(s...rity_queue)(3)

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

πŸ’… std::swap(std::priority_queue) πŸ’…

okay bestie, welcome to the C++ Standard Libary manual page for std::swap(std::priority_queue) β€” yes, the spelling in the header is Libary, not me πŸ’€. This one is all about swapping two std::priority_queues and it's lowkey iconic.

✨ NAME ✨

std::swap(std::priority_queue) β€” std::swap(std::priority_queue). That's the name, periodt.

πŸ¦‹ Synopsis πŸ¦‹

Defined in header <queue>

First overload β€” (since C++11), (until C++17):

template< class T, class Container, class Compare >
void swap( std::priority_queue<T, Container, Compare>& lhs,
           std::priority_queue<T, Container, Compare>& rhs );

Second overload β€” (since C++17):

template< class T, class Container, class Compare >
void swap( std::priority_queue<T, Container, Compare>& lhs,
           std::priority_queue<T, Container, Compare>& rhs )
    noexcept(/* see below */);

Specializes the std::swap algorithm for std::priority_queue. Swaps the contents of lhs and rhs. Calls lhs.swap(rhs). That's the whole vibe.

Highkey important: this overload participates in overload resolution only if std::is_swappable_v<Container> and std::is_swappable_v<Compare> are both true. (since C++17)

πŸŽ€ Parameters πŸŽ€

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

🫢 Return value 🫢

none.

πŸ”₯ Complexity πŸ”₯

Same as swapping the underlying container. Simple as that.

🌈 Exceptions 🌈

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

πŸ’œ Notes πŸ’œ

Okay history tea: although the overloads of std::swap for container adaptors are introduced in C++11, container adaptors can already be swapped by std::swap in C++98. Such calls to std::swap usually have linear time complexity, but better complexity may be provided.

no cap, that C++98 note is the lil plot twist 🫢

🌸 Example 🌸

The original has the lil // Run this code marker, and here's the exact code:

#include <algorithm>
#include <iostream>
#include <queue>

int main()
{
    std::priority_queue<int> alice;
    std::priority_queue<int> bob;

    auto print = [](const auto& title, const auto& cont)
    {
        std::cout << title << " size=" << cont.size();
        std::cout << " top=" << cont.top() << '\n';
    };

    for (int i = 1; i < 4; ++i)
        alice.push(i);
    for (int i = 7; i < 11; ++i)
        bob.push(i);

    // Print state before swap
    print("Alice:", alice);
    print("Bob  :", bob);

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

    // Print state after swap
    print("Alice:", alice);
    print("Bob  :", bob);
}

πŸ“ Output πŸ“

Alice: size=3 top=3
Bob  : size=4 top=10
-- SWAP
Alice: size=4 top=10
Bob  : size=3 top=3

πŸ‘‘ See also πŸ‘‘

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

🏷️ Category 🏷️

  • * conditionally noexcept

🌸 okay that's the whole page! source/version/date: http://cppreference.com Β· 2024.06.10 Β· page std::swap(s...rity_queue)(3) β€” bye bestie, hydrate and swap responsibly πŸ’–

πŸ“¦ where this page comes from