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.
Syntax
The 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.
Examples
The following examples demonstrate how to use swap function in C++.
Swap Two Vectors
The 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;
}
Outputv1: 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 Arrays
swap() 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;
}
Outputarr1: 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 Class
Your 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.
Similar Reads
vector swap() in C++ In C++, std::vector::swap() is a built-in function used to exchange the contents to two vectors of same type. This function does not copy, move or swap the individual elements, instead, it swaps the internal pointers to the dynamically allocated array of both vectors and updates the size accordingly
3 min read
set::swap() in C++ STL Sets are a type of associative containers in which each element has to be unique, because the value of the element identifies it. The value of the element cannot be modified once it is added to the set, though it is possible to remove and add the modified value of that element. set::swap() This func
2 min read
list::swap() in C++ STL Lists are containers used in C++ to store data in a non contiguous fashion, Normally, Arrays and Vectors are contiguous in nature, therefore the insertion and deletion operations are costlier as compared to the insertion and deletion option in Lists. list::swap()This function is used to swap the con
2 min read
queue::swap() in C++ STL Queue is also an abstract data type or a linear data structure, which follows a particular order in which the operations are performed. The order is First In First Out (FIFO). In a FIFO data structure, the first element added to the queue will be the first one to be removed. queue::swap() swap() fun
2 min read
stack swap() in C++ STL Stacks are a type of container adaptors with LIFO(Last In First Out) type of work, where a new element is added at one end and (top) an element is removed from that end only.stack::swap()This function is used to swap the contents of one stack with another stack of same type but the size may vary. Sy
2 min read