C++ std::swap_ranges() Function

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

Definition

In C++, std::swap_ranges() exchanges each element of the first range with the element at the same position in a second range.

The ranges can be in different containers. It swaps as many elements as the first range has.

Syntax

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

C++
std::swap_ranges(a.begin(), a.end(), b.begin());
std::swap_ranges(a.begin(), a.begin() + 2, b.begin());
CallWhat it does
std::swap_ranges(a.begin(), a.end(), b.begin());Swap all of a with the start of b.
std::swap_ranges(a.begin(), a.begin() + 2, b.begin());Swap only the first two elements.
Library declarations
C++
template<class ForwardIt1, class ForwardIt2>
ForwardIt2 swap_ranges(ForwardIt1 first1, ForwardIt1 last1,
                       ForwardIt2 first2);

Parameters

ParameterDescription
first1, last1Required. The first range.
first2Required. Where the second range starts. It must have at least as many elements as the first range.

Return Value

Return ValueDescription
IteratorPoints just after the last swapped element in the second range.

C++ Examples

Example 1 — Swap two vectors by element

Exchange the contents of two vectors of the same size.

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

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

    std::swap_ranges(a.begin(), a.end(), b.begin());

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

How It Works

  1. The range is all of a, which has three elements.
  2. Each element is swapped with the one at the same index in b.
  3. Both vectors now hold the other's values.

Example 2 — Swap only part

Swap the first two elements only.

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

int main() {
    std::vector<int> a = {1, 2, 3, 4};
    std::vector<int> b = {9, 9, 9, 9};

    std::swap_ranges(a.begin(), a.begin() + 2, b.begin());

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

How It Works

  1. a.begin() + 2 ends the range after two elements.
  2. Only the first two slots of each vector are exchanged.
  3. The last two elements stay as they were.

Example 3 — Use the returned iterator

Swap a short vector into a longer one and read the next element.

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

int main() {
    std::vector<int> a = {1, 2};
    std::vector<int> b = {10, 20, 30, 40, 50};

    auto it = std::swap_ranges(a.begin(), a.end(), b.begin());

    std::cout << "b: ";
    for (int x : b) std::cout << x << ' ';
    std::cout << '\n';
    std::cout << "Next in b: " << *it << '\n';
    return 0;
}

How It Works

  1. The first range has two elements, so only two swaps happen.
  2. b begins with the values from a, and a now holds 10 and 20.
  3. The returned iterator points at index 2 of b, which holds 30.

Frequently Asked Questions

In C++, std::swap_ranges() swaps the elements of two ranges one by one. Include <algorithm> to use it.
Include <algorithm>. It has been part of C++ since C++98, so every modern compiler supports it.
It returns an iterator to the element after the last one swapped in the second range.
That is undefined behaviour. The second range must have at least as many elements as the first.
swap() exchanges two objects. swap_ranges() exchanges many pairs of elements.

Next: transform()

Applies a function to each element and stores the results.

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

16 people found this page helpful