C++ std::priority_queue::swap() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
C++11

Definition

In C++11, swap() exchanges everything inside two std::priority_queue objects. After a.swap(b), queue a holds what b had, and queue b holds what a had.

The swap is fast, because the queues just trade their inner containers. No element is copied one by one. Both queues must have the same type, which includes the element type, the container, and the comparison. You can also write std::swap(a, b).

This function is a member function of std::priority_queue<T>, declared in the <queue> header. It needs C++11 or newer (-std=c++11). You call it with a dot, for example pq.swap(...).

Syntax

Call swap() on one queue and pass the other queue. Or call the non-member std::swap with both.

C++
std::priority_queue<int> a;
std::priority_queue<int> b;
a.push(3);
b.push(20);

a.swap(b);          // a now has 20, b now has 3
std::swap(a, b);    // swaps them back
CallWhat it does
a.swap(b)Exchanges the contents of a and b. Nothing is returned.
std::swap(a, b)The non-member version. It does the same thing and calls a.swap(b) inside.

Swapping does not change the order of elements inside each queue, and it does not call top() or pop(). priority_queue has no clear(), so swapping with an empty queue is a common way to empty it.

Library declarations
C++
void swap(priority_queue& other) noexcept(/* see below */);   // C++11

// Non-member overload (C++11):
template<class T, class Container, class Compare>
void swap(priority_queue<T, Container, Compare>& lhs,
          priority_queue<T, Container, Compare>& rhs);

// priority_queue<T, Container, Compare> from <queue>
// It swaps the container c and the comparison object comp.

Parameters

ParameterDescription
otherRequired. Another priority queue of the same type. It trades its contents with the queue you call swap() on.

Return Value

Return ValueDescription
Nothingswap() returns void. Both queues are changed in place.

C++ Examples

Example 1 — Swap Two Queues

Fill two queues with different numbers. After swap(), the tops and sizes are exchanged.

C++
#include <iostream>
#include <queue>

int main() {
    std::priority_queue<int> a;
    a.push(1);
    a.push(2);
    a.push(3);

    std::priority_queue<int> b;
    b.push(10);
    b.push(20);

    a.swap(b);

    std::cout << "a: top " << a.top() << ", size " << a.size() << '\n';
    std::cout << "b: top " << b.top() << ", size " << b.size() << '\n';
    return 0;
}

How It Works

  1. Before the swap, queue a has three numbers with top 3. Queue b has two numbers with top 20.
  2. a.swap(b) exchanges the whole contents. No element is copied one by one, so it is fast.
  3. Now a has the old data of b: top 20 and size 2. Queue b has top 3 and size 3.

Example 2 — Empty a Queue by Swapping

priority_queue has no clear(). Swap with a new empty queue to throw all the elements away.

C++
#include <iostream>
#include <queue>

int main() {
    std::priority_queue<int> pq;
    pq.push(7);
    pq.push(14);
    pq.push(21);
    std::cout << "Before: " << pq.size() << '\n';

    std::priority_queue<int> empty_queue;
    pq.swap(empty_queue);

    std::cout << "After pq: " << pq.size() << '\n';
    std::cout << "Old data now in empty_queue: " << empty_queue.size() << '\n';
    return 0;
}

How It Works

  1. pq holds three numbers, so the first line prints 3.
  2. empty_queue is a new queue with nothing inside. After pq.swap(empty_queue), pq has no elements.
  3. The old elements did not disappear. They moved to empty_queue, which now has size 3. They are freed when that variable goes out of scope.

Example 3 — Use std::swap on Two Min-Heaps

The non-member std::swap(a, b) works too. Both queues must have the same comparison type.

C++
#include <functional>
#include <iostream>
#include <queue>
#include <vector>

using MinHeap = std::priority_queue<int, std::vector<int>, std::greater<int>>;

int main() {
    MinHeap a;
    a.push(5);
    a.push(1);

    MinHeap b;
    b.push(9);

    std::swap(a, b);

    std::cout << "a: top " << a.top() << ", size " << a.size() << '\n';
    std::cout << "b: top " << b.top() << ", size " << b.size() << '\n';
    return 0;
}

How It Works

  1. using MinHeap = ... names the queue type once. Both a and b use it, so they have exactly the same type.
  2. std::swap(a, b) gives the same result as a.swap(b). It is the non-member way to write the same call.
  3. Queue a now holds 9 only. Queue b holds 5 and 1, and its min-heap top is 1.

Frequently Asked Questions

It exchanges the contents of two priority queues. After a.swap(b), a has the elements of b and b has the elements of a.
Yes. The element type, the container, and the comparison must all be the same. A max-heap cannot be swapped with a min-heap.
No. It only swaps the inner containers and comparison objects, so no element is copied one by one. This is why it is fast.
Yes. A priority_queue has no clear(), so swap it with an empty queue, or assign a new one: pq = std::priority_queue<int>();.
The member swap() and the non-member std::swap for priority_queue were added in C++11. Compile with -std=c++11 or newer.

Next: top()

Read the element with the highest priority without removing it with std::priority_queue.

top() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful