C++ std::swap() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<algorithm> · C++98

Definition

In C++, std::swap() exchanges the values of two variables. After the call, a holds what b held and the other way round.

It works on numbers, strings, vectors and most other types. For big containers the swap is fast, because it only exchanges internal pointers.

Syntax

Add #include <algorithm>, then call std::swap(). Start with these two lines.

C++
std::swap(a, b);
std::swap(v1, v2);
CallWhat it does
std::swap(a, b);Swap two variables of the same type.
std::swap(v1, v2);Swap two whole containers.

The two-argument std::swap() is in <utility> since C++11, and in <algorithm> before that. Including <algorithm> works in all versions.

Library declarations
C++
template<class T>
void swap(T& a, T& b);

Parameters

ParameterDescription
a, bRequired. Two objects of the same type. Both must be writable variables, not constants.

Return Value

Return ValueDescription
voidNothing is returned. The two objects now hold each other's values.

C++ Examples

Example 1 — Swap two numbers

Exchange two integers.

C++
#include <algorithm>
#include <iostream>

int main() {
    int a = 3;
    int b = 7;

    std::swap(a, b);

    std::cout << "a=" << a << ", b=" << b << '\n';
    return 0;
}

How It Works

  1. a and b start with 3 and 7.
  2. std::swap() uses a temporary copy behind the scenes to exchange them.
  3. The print shows the values reversed.

Example 2 — Swap two strings

Exchange the text in two strings.

C++
#include <algorithm>
#include <iostream>
#include <string>

int main() {
    std::string s1 = "hello";
    std::string s2 = "world";

    std::swap(s1, s2);

    std::cout << s1 << ' ' << s2 << '\n';
    return 0;
}

How It Works

  1. std::string has a fast swap, so the characters are not copied one by one.
  2. s1 ends up with world and s2 with hello.
  3. We print s1 first, so world comes out first.

Example 3 — Swap two vectors

Exchange whole containers in one step.

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

int main() {
    std::vector<int> v1 = {1, 2, 3};
    std::vector<int> v2 = {9};

    std::swap(v1, v2);

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

How It Works

  1. v1 has three elements and v2 has one.
  2. Swapping vectors only exchanges their internal pointers, so it is very fast.
  3. After the call, the sizes are exchanged too.

Frequently Asked Questions

In C++, std::swap() swaps the values of two objects. Include <algorithm> to use it.
Include <algorithm> (or <utility> since C++11). std::swap() has been available since C++98.
It returns nothing (void). The two arguments exchange their values.
No. Both arguments must be the same type.
Yes. It swaps internal pointers, so the time does not depend on how many elements the vectors hold.

Next: swap_ranges()

Swaps the elements of two ranges one by one.

swap_ranges() 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.

6 people found this page helpful