C++ std::vector swap() Function

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

Definition

In C++, std::vector::swap() exchanges the whole content of two std::vector objects. After the call, each vector 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 <vector> to use it.

This is a member of std::vector<T>, a dynamic contiguous array. Elements sit in one block, so v[i] and data() are fast. Growing past capacity() reallocates and can invalidate iterators, references, and pointers. Prefer push_back / pop_back at the end; middle inserts shift elements.

Syntax

Call swap on one vector and pass the other vector. 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 vector, use std::swap(d[0], d[1]) or std::iter_swap.

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

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

Parameters

ParameterDescription
otherRequired. The vector to swap with. It must have the same type, such as std::vector<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 vectors with different sizes.

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

int main() {
    std::vector<int> a = {1, 2, 3};
    std::vector<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 <vector>
#include <iostream>
#include <string>
#include <utility>

int main() {
    std::vector<std::string> a = {"x", "y"};
    std::vector<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 Vector to Release Memory

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

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

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

    std::vector<int>().swap(v);

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

How It Works

  1. std::vector<int>() creates a temporary, empty vector.
  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 vectors of the same type. Each vector 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::vector 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 vector. The old end() iterators are invalidated.

Back to the function index

See all 32 std::vector 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.

11 people found this page helpful