C++ std::array swap() Function

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

Definition

In C++, std::array::swap() exchanges the contents of two arrays. After the call, the first array holds the values of the second one, and the other way round.

Both arrays must have the same element type and the same size. The swap goes element by element, so it takes time proportional to N. Include <array> to use it.

This is a member of std::array<T, N>, not of a plain C array and not of std::vector. A std::array has a fixed size N that is part of its type, and it lives on the stack like a normal variable.

Syntax

Call swap on one array and pass the other array.

C++
a.swap(b);
std::swap(a, b);
CallWhat it does
a.swap(b)Exchanges all elements of a and b.
std::swap(a, b)The non-member version. It gives the same result for arrays.

Both arrays must be the same type, for example std::array<int, 3>. A std::array<int, 4> will not compile here.

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

Parameters

ParameterDescription
otherRequired. Another std::array with the same element type and the same size N. Its contents are exchanged with the array you call swap on.

Return Value

Return ValueDescription
voidNothing is returned. Both arrays are changed.

C++ Examples

Example 1 — Basic swap()

Exchange two small arrays and print both.

C++
#include <array>
#include <iostream>

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

    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. Both arrays are std::array<int, 3>, so the swap is allowed.
  2. a.swap(b) moves the values 9 8 7 into a and 1 2 3 into b.
  3. The two loops print the new contents of each array.

Example 2 — Iterators After a Swap

See that an iterator stays with its own array.

C++
#include <array>
#include <iostream>

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

    auto it = a.begin();
    std::cout << "Before swap: " << *it << '\n';

    a.swap(b);

    std::cout << "After swap: " << *it << '\n';
    return 0;
}

How It Works

  1. it points to the first slot inside a.
  2. The swap copies new values into the slots of a. The slots themselves do not move.
  3. So it still points into a, and it now reads 9, the value that came from b.

Example 3 — Swap Arrays of double

The same function works for any element type, as long as both arrays match.

C++
#include <array>
#include <iostream>

int main() {
    std::array<double, 2> morning = {20.5, 21.0};
    std::array<double, 2> evening = {18.0, 17.5};

    morning.swap(evening);
    // std::array<double, 3> other;
    // morning.swap(other);   // error: different size, will not compile

    std::cout << "morning: " << morning[0] << " " << morning[1] << '\n';
    std::cout << "evening: " << evening[0] << " " << evening[1] << '\n';
    return 0;
}

How It Works

  1. Both arrays are std::array<double, 2>, so they have the same type.
  2. After the swap, morning holds the evening readings and the other way round.
  3. cout prints 18 and 21 without decimals. The commented lines show that a different size is a compile error.

Frequently Asked Questions

It exchanges all elements of one array with all elements of another array of the same type and size.
No. std::array<int, 3> and std::array<int, 4> are different types, so the call will not compile.
It takes time proportional to N, because every element is swapped. A std::vector swap is faster, because it only swaps internal pointers.
They stay attached to the same array object, but the values in that array are now the ones that came from the other array.
For std::array they give the same result. The non-member std::swap calls the member swap for you.

Back to the function index

See all 17 std::array 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.

14 people found this page helpful