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

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

Definition

In C++11, swap() exchanges everything between two std::queue objects. After the call, each queue holds what the other one held before, in the same order.

The swap is fast because the queues only exchange their internal containers, so no elements are copied one by one. A queue has no clear(), so swapping with an empty queue is also a common way to empty one.

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

Syntax

Call swap() on one queue and pass the other queue. Both queues must have exactly the same type.

C++
std::queue<int> a;
std::queue<int> b;
a.push(1);
b.push(9);

a.swap(b);        // member form
std::swap(a, b);  // non-member form, swaps back
CallWhat it does
a.swap(b)Exchanges the contents of a and b. Both must be the same queue<T, Container> type.
std::swap(a, b)The non-member form. It calls a.swap(b) and works in generic code.

swap() takes constant time and does not move individual elements. Both queues must use the same element type and the same underlying container, so a queue<int> cannot swap with a queue<long>. It needs C++11 or newer.

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

template<class T, class Container>
void swap(queue<T, Container>& a,
          queue<T, Container>& b) noexcept(/* see below */);   // C++11

// queue<T, Container> from <queue>
// It swaps the underlying containers: swap(c, other.c).

Parameters

ParameterDescription
otherRequired. The queue to exchange with. It must have the same element type and the same underlying container type. It is changed too.

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 sizes, swap them, and see that sizes and fronts move together.

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

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

    a.swap(b);

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

How It Works

  1. Before the call, a holds 1, 2, 3 and b holds just 9.
  2. a.swap(b) exchanges the two internal containers. Now a has the single element 9, so its size is 1 and its front is 9.
  3. b now owns the old three-element line. Its size is 3 and its front is 1, so the order inside was kept as it was.

Example 2 — Use std::swap() With Strings

The non-member std::swap() does the same job. This is the form to use in generic code and with other standard containers.

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

int main() {
    std::queue<std::string> a;
    std::queue<std::string> b;
    a.push("red");
    a.push("green");
    b.push("blue");

    std::swap(a, b);

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

How It Works

  1. a holds red, green and b holds blue. Both are std::queue<std::string>, so they have the same type.
  2. std::swap(a, b) calls the queue’s own swap() behind the scenes. No strings are copied one by one.
  3. Now a has one element, so front() and back() are both blue. In b, the order red then green is kept, so front() is red and back() is green.

Example 3 — Empty a Queue by Swapping With an Empty One

A queue has no clear(). Swapping with a temporary empty queue removes every element at once.

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

int main() {
    std::queue<int> q;
    q.push(1);
    q.push(2);
    q.push(3);
    q.push(4);
    std::cout << "Before: " << q.size() << '\n';

    std::queue<int>().swap(q);

    std::cout << "After: " << q.size() << '\n';
    std::cout << std::boolalpha << "Empty: " << q.empty() << '\n';
    return 0;
}

How It Works

  1. The queue holds four numbers, so size() prints 4.
  2. std::queue<int>() creates a brand new empty queue. Its swap(q) trades places with q, so q becomes empty at once.
  3. The temporary queue now holds the four old numbers and is destroyed at the end of the line. After that, size() is 0 and empty() is true.

Frequently Asked Questions

It exchanges the contents of two queues. After the call, each queue holds what the other one held, in the same order. It was added in C++11.
No. Both queues must have the same element type and the same underlying container. A queue<int> cannot swap with a queue<double>.
No. It swaps the internal containers, which is a constant-time operation, so it is fast even for large queues.
A queue has no clear() function. Use std::queue<int>().swap(q); or assign q = std::queue<int>(); or pop() in a loop until empty() is true.
They do the same thing. The member form is called on one queue. The non-member form takes both queues and calls the member form, which is useful in generic code.

Back to the function index

See all 9 std::queue functions grouped by what they do.

Function index →

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.

15 people found this page helpful