The swap() function in C++ is a standard library utility used to exchange the values of two objects. It supports most built-in data types, STL containers, and user-defined types.
- Exchanges the values of two objects without requiring a temporary variable.
- Works with primitive data types, STL containers, arrays, and custom classes.
#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;
}
Output
1 55 55 1
In the above example, we exchange the values of two variables a and b using swap function.
Syntax
The swap() function is defined in the <utility> header file (and is also available through several other standard headers).
swap(a, b);
Parameters
- a: First object to be swapped.
- b: Second object to be swapped.
Return Value: The swap() function does not return any value.
Example: The following program demonstrates how to swap two integer variables:
#include <iostream>
#include <utility>
using namespace std;
int main()
{
int a = 1, b = 55;
cout << a << " " << b << endl;
swap(a, b);
cout << a << " " << b;
return 0;
}
Output
1 55 55 1
Explanation: The swap() function exchanges the values of a and b without requiring an additional temporary variable.
Swap Two Vectors
The swap function can easily swap elements of two vectors in the same way as other variables.
#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;
}
Output
v1: 6 7 8 9 v2: 1 2 3 4
Explanation: For vectors, swap() exchanges their internal memory pointers rather than swapping elements individually.
Swap Two Arrays
The swap() function also supports raw arrays of the same size and type.
#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;
}
Output
arr1: 6 7 8 9 arr2: 1 2 3 4
Explanation: Unlike STL containers, arrays do not swap their memory addresses. Instead, each element is swapped individually.
swap() for User-Defined Classes
Custom classes can provide their own specialized swap() function to improve efficiency and support STL algorithms.
#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;
}
Output
99 8 1 22
Explanation: The custom swap() function exchanges the data members of the objects while preserving the same interface as std::swap().
Important Notes About swap()
The swap() function provides an efficient and generic way to exchange values in C++.
- Works with most built-in types, STL containers, and user-defined classes.
- The time complexity depends on the type being swapped.
- STL containers such as vector usually swap in constant time.
- Arrays are swapped element by element, resulting in linear time complexity.
Advantages of Using swap()
Using swap() provides several benefits over manual swapping.
- Eliminates the need for temporary variables.
- Improves code readability.
- Provides optimized implementations for STL containers.
- Supports generic programming and custom types.