C++ std::deque swap() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::deque

Definition

In C++, std::deque::swap() exchanges the whole content of two std::deque objects. After the call, each deque holds what the other one held before.

It is fast because it does not copy or move the elements one by one. It only swaps the internal data. Include <deque> to use it.

This is a member of std::deque<T>, a double-ended queue. A deque is fast at adding and removing at both ends, and it allows access by index like std::vector. Unlike a vector, its elements are stored in several blocks, not in one contiguous block.

Syntax

Call swap on one deque and pass the other deque. Both must hold the same element type.

C++
a.swap(b);
std::swap(a, b);
CallWhat it does
a.swap(b)The member function. a gets the elements of b, and b gets the elements of a.
std::swap(a, b)The free function. It gives the same result and calls the member function for you.

To swap just two elements inside one deque, use std::swap(d[0], d[1]) or std::iter_swap.

Library declarations
C++
void swap(deque& other) noexcept;   // member function

template<class T, class Alloc>
void swap(deque<T, Alloc>& lhs, deque<T, Alloc>& rhs) noexcept;   // free function

Parameters

ParameterDescription
otherRequired. The deque to swap with. It must have the same type, such as std::deque<int>.

Return Value

Return ValueDescription
Nothingswap() returns void. Both deques are changed.

C++ Examples

Example 1 — Swap Two Deques with swap()

Exchange the content of two deques with different sizes.

C++
#include <deque>
#include <iostream>

int main() {
    std::deque<int> a = {1, 2, 3};
    std::deque<int> b = {7, 8};

    a.swap(b);

    std::cout << "a: ";
    for (int n : a) std::cout << n << ' ';
    std::cout << '\n';

    std::cout << "b: ";
    for (int n : b) std::cout << n << ' ';
    std::cout << '\n';
    return 0;
}

How It Works

  1. a starts with three elements, and b starts with two.
  2. a.swap(b) exchanges everything. The sizes are swapped too.
  3. Now a prints 7 8 and b prints 1 2 3. No element was copied one by one.

Example 2 — std::swap() on Deques

The free function works in the same way and reads naturally.

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

int main() {
    std::deque<std::string> a = {"x", "y"};
    std::deque<std::string> b = {"z"};

    std::swap(a, 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. std::swap(a, b) calls the member swap() for deques, so it is just as fast.
  2. a now holds the single word "z" that came from b.
  3. b now holds "x" and "y", so its size is 2 and its front is "x".

Example 3 — Swap with an Empty Deque to Release Memory

A temporary empty deque takes the old content away. The temporary is then destroyed.

C++
#include <deque>
#include <iostream>

int main() {
    std::deque<int> d = {1, 2, 3, 4, 5};

    std::deque<int>().swap(d);

    std::cout << "size: " << d.size() << '\n';
    return 0;
}

How It Works

  1. std::deque<int>() creates a temporary, empty deque.
  2. swap(d) gives the five elements to the temporary and gives its empty content to d.
  3. The temporary is destroyed at the end of the line, together with the old memory. Before shrink_to_fit() existed, this was the usual trick to free memory.

Frequently Asked Questions

It exchanges the whole content of two deques of the same type. Each deque ends up with the elements of the other.
Yes. It takes constant time, because only internal data is exchanged and no element is copied.
For std::deque they give the same result. The non-member std::swap calls the member swap for you.
Yes. After the swap, the sizes are exchanged too.
Iterators and references stay valid, but they now refer to elements that live in the other deque. The old end() iterators are invalidated.

Back to the function index

See all 31 std::deque member 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.

10 people found this page helpful