Updated June 8, 2023
Introduction to swap() in C++
The std:: swap() function swaps or exchanges the values of two variables or containers. The std::swap() function is a built-in function in the C++ STL (Standard Template Library).
template < class T > void swap(T& a, T& b);
Where a is the first variable which stores some value and b also a variable that stores some value, both a and b values are to swap. The function does not return anything it only swaps the values of a and b variables.
How does it work?
Implementation of swap( ) function in C++ SLT Lets see the C++11 implantation of std::swap is an improvement over C++03 as below:
Template < template T> void swap( T& a, T& b ) {
T temp = std::move( a ); a = std::move(b); b = std::move(temp);
}
Explanation: The T temp = std::move( a ); statement duplicate a and create copy of a element. The statement a = std:: move(b); store a with a duplicate of b and discard the original contents of a. the statement b = std:: move(temp); store a with a duplicate of temp and discard the duplicate contents of temp. once the execution of swap( ) function finishes destroy the temp variable.
Examples to Implement Swap( ) function in c++
Below are some examples mentioned:
Example #1
Swap( ) function to swap two integer number as below:
Code:
#include <iostream>
using namespace std;
int main ()
{
int a=10, b=20;
cout<< "Value of a before swap function call = " << a <<endl;
cout<< "Value of b before swap function call = " << b <<endl;
swap( a, b );
cout<< "Value of a after swap function call = " << a <<endl;
cout<< "Value of b after swap function call = " << b <<endl;
return 0;
}
Output:
Explanation: As in above code the swap(T& a, T& b) function calling as call by reference because the parameters T& a, T& b, references or stores the address of pass variable and directly referencing variable and processing directly to them and nothing returning. Overloading swap( ) function to void swap(int& a int& b).
Example #2
Next, we apply the swap( ) function on string data types to swap, so we will call swap( ) function and pass string variables:
Code:
#include<iostream>
using namespace std;
int main ()
{
string a="Hello", b="World";
cout<< "Value of a before swap function call = " << a <<endl;
cout<< "Value of b before swap function call = " << b <<endl;
swap( a, b );
cout<< "Value of a after swap function call = " << a <<endl;
cout<< "Value of b after swap function call = " << b <<endl;
return 0;
}
Output:
Explanation: As in above code the C++ overloading swap( ) function to void swap( string& a, string& b).
Example #3
Next, we rewrite the above code without using the std namespace as below:
Code:
#include<iostream>
int main ()
{
std::string a="Hello", b="World";
std::cout<< "Value of a before swap function call = " << a << std::endl;
std::cout<< "Value of b before swap function call = " << b << std::endl;
std::swap( a, b );
std::cout<< "Value of a after swap function call = " << a << std::endl;
std::cout<< "Value of b after swap function call = " << b << std::endl;
return 0;
}
Output:
Example #4
swap( ) function and pass vectors variables:
Code:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main ()
{
int a=10, b=20;
cout<< "Value of a before swap function call = " << a <<endl;
cout<< "Value of b before swap function call = " << b <<endl;
swap( a, b );
cout<< "Value of a after swap function call = " << a <<endl;
cout<< "Value of b after swap function call = " << b <<endl;
// create two vector v1 and v2, where v1 = 3x10 and v2 = 6x20
vector <int> v1 (3,a);
vector <int> v2 (6,b);
cout << "V1 elements before swap function call = " ;
for (vector <int>::iterator it = v1.begin(); it! = v1.end(); it++)
cout << ' ' << *it;
cout << endl;
// swap two vector
swap( v1, v2 );
cout << "V1 elements after swap function call = " ;
for (vector <int>::iterator it = v1.begin(); it! = v1.end(); it++)
cout << ' ' << *it;
cout << endl;
return 0;
}
Output:
Explanation: As in above code the swap(T& a, T& b) function calling as call by reference and the overloading swap( ) function to void swap(vector <int>& a, vector <int>& b).
The Complexity of swap( ) function in c++: the swap( ) function has N complexity for array because to swap each element operation of swapping is to be performed and for constant the function has constant complexity.
The Exceptions of swap( ) function: The swap( ) function throws an exception if any variables elements throw.
Conclusion
The std:: swap() function swaps the values of two variables. The std::swap() function is a built-in function in the C++ STL. The swap(T& a, T& b) function calls by reference and the C++ overloads swap( ) function based on the data types of the variables passes, if the variables pass of different data types the swap( ) function throw error or exception.
Recommended Articles
This is a guide to swap() in C++. Here we discuss an introduction to swap() in C++, how does it work, with respective examples in detailed explanation. You can also go through our other related articles to learn more –