# std::swap(std::priority_queue)

> C++ Atlas: `cppatlas.com/w/cpp/container/priority_queue/swap2` | Source: https://en.cppreference.com/w/cpp/container/priority_queue/swap2 | Domain: Containers | Standards: C++11, C++17

Same as swapping the underlying containers.

## Official Specification Summary (cppreference)

```cpp
// 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 ); // (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 )
               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 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 containers.

### Exceptions

(since C++17):

`noexcept` specification:

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

### Notes

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.

### Example

```cpp
#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("Bobby:", bob);

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

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

Output:

```text
Alice: size=3 top=3
Bobby: size=4 top=10
-- SWAP
Alice: size=4 top=10
Bobby: size=3 top=3
```

### See also

- [`swap`](https://en.cppreference.com/w/cpp/container/priority_queue/swap) (C++11): swaps the contents (public member function)

---
Attribution: content from https://en.cppreference.com/w/cpp/container/priority_queue/swap2 (cppreference.com), licensed under CC-BY-SA 3.0 and GFDL. Modified: converted to markdown, study notes added. Offline snapshot of 9 February 2025.