Namespaces
Variants
Views
Actions

std::swap(std::priority_queue)

From cppreference.com
Defined in header <queue>
template< class T, class Container, class Compare >

void swap( std::priority_queue<T,Container,Compare>& lhs,

           std::priority_queue<T,Container,Compare>& rhs );
(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 ) noexcept(/* see below */);
(since C++17)

Specializes the std::swap algorithm for std::priority_queue. Swaps the contents of lhs and rhs. Calls lhs.swap(rhs).

This overload only participates in overload resolution if std::is_swappable<Container>::value and std::is_swappable<Compare>::value are both true.

(since C++17)

Contents

[edit] Parameters

lhs, rhs - containers whose contents to swap

[edit] Return value

(none)

[edit] Complexity

Same as swapping the underlying container.

[edit] Exceptions

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


[edit] Example

#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

[edit] See also

swaps the contents
(public member function) [edit]