C++ std::swap_ranges() Function

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.
std::swap_ranges(a.begin(), a.end(), b.begin());
std::swap_ranges(a.begin(), a.begin() + 2, b.begin());
| Call | What 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
template<class ForwardIt1, class ForwardIt2>
ForwardIt2 swap_ranges(ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2);
Parameters
| Parameter | Description |
|---|
| first1, last1 | Required. The first range. |
| first2 | Required. Where the second range starts. It must have at least as many elements as the first range. |
Return Value
| Return Value | Description |
|---|
| Iterator | Points 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.
#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
- The range is all of
a, which has three elements. - Each element is swapped with the one at the same index in
b. - Both vectors now hold the other's values.
Example 2 — Swap only part
Swap the first two elements only.
#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
a.begin() + 2 ends the range after two elements.- Only the first two slots of each vector are exchanged.
- 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.
#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;
}
b: 1 2 30 40 50
Next in b: 30
How It Works
- The first range has two elements, so only two swaps happen.
b begins with the values from a, and a now holds 10 and 20.- 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.
About the author
Developer, cloud engineer, and technical writer
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