Last Updated : 11 Jul, 2025
The swap() is a built-in function in the C++ STL which swaps the value of two variables. This function supports almost every data type available in C++, whether it is a primitive type such as int, char, etc. or an STL containers such as vector, map, etc.
Example:
C++
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int a = 1, b = 55;
cout << a << " " << b << endl;
// swapping the values of a and b
swap(a, b);
cout << a << " " << b;
return 0;
}
In the above example, we exchange the values of two variables a and b using swap function.
SyntaxThe std::swap() function is defined inside <algorithm> header file.
C++
where a and b are two variables that are to be swapped with each other. This function does not return any value.
ExamplesThe following examples demonstrate how to use swap function in C++.
Swap Two VectorsThe swap function can easily swap elements of two vectors in the same way as other variables.
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v1 = {1, 2, 3, 4};
vector<int> v2 = {6, 7, 8, 9};
// Swapping the vectors
swap(v1, v2);
cout << "v1: ";
for (auto i : v1)
cout << i << " ";
cout << endl;
cout << "v2: ";
for (auto i : v2)
cout << i << " ";
return 0;
}
v1: 6 7 8 9 v2: 1 2 3 4
The swap function just swaps the internal address of the allocated memory in the vector, so, the process only requires constant time and space.
Swap Two Arraysswap() also works for raw arrays.
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
int arr1[] = {1, 2, 3, 4};
int arr2[] = {6, 7, 8, 9};
// Swapping the vectors
swap(arr1, arr2);
cout << "arr1: ";
for (auto i : arr1)
cout << i << " ";
cout << endl;
cout << "arr2: ";
for (auto i : arr2)
cout << i << " ";
return 0;
}
arr1: 6 7 8 9 arr2: 1 2 3 4
Both the arrays should be of the same size and type for swap() to work. Also, arrays' address cannot be swapped, so each element is swapped one by one leading to the linear time complexity.
swap() for Custom ClassYour custom class can also have a specialized swap function which in turn uses the inbuilt std::swap(). It helps your class in working seamlessly with STL algorithms that uses swap operation.
Example:
C++
#include <iostream>
using namespace std;
class GfG {
int a, b;
public:
GfG(int x, int y) : a(x), b(y) {}
// Custom swap function for GfG class
friend void swap(GfG& first, GfG& second) noexcept {
// for ADL
using std::swap;
swap(first.a, second.a);
swap(first.b, second.b);
}
void print() {
cout << a << " " << b << endl;
}
};
int main() {
GfG gfg1(1 ,22);
GfG gfg2(99, 8);
// Swapping values
swap(gfg1, gfg2);
gfg1.print();
gfg2.print();
return 0;
}
In the above program, the swap() function is declared as friend to keep the same API as std::swap() because member function needs to be called using one of the objects, changing the way we use swap(). It is also declared noexcept to provide string exception guarantee.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4