Introduction
In this tutorial, we are going to learn the swap() function in C++ programming language. Swapping is a simple operation in C++ which basically is the exchange of data or values among two variables of any data type. Considering both the variables are of the same data type.
So, let us get right into the function and its working.
Working of the swap() function in C++
The swap()
function in C++, from the standard library, is a function that directly swaps values between two given variables of the same types. Let us look at the syntax for using the same:
Syntax,
1 |
std::swap( a , b ); |
Here:
- a and b, both are variables of the same data type,
- We pass both of them to the
swap()
function and the values at their respective addresses are exchanged among themselves, - a now stores the value b previously had, whereas, b has the value which a prior to swapping stored.
Now, let us see how we can use the swap()
function in C++.
How to use the swap() function in C++
As we specified earlier, the swap()
function from the standard library in C++ can swap values of any data type including int, float, string, etc. and even data structures like arrays, stacks, and queues, etc.
So now we are going to look at some examples where we try to swap integers, strings as well as arrays in order to have a clear understanding.
1. Swapping two integer values using swap() in C++
First, let us consider the swapping of two integer values using the function. Look at the code given below carefully,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <iostream> using namespace std; int main() { //values before swapping int val1 = 2; int val2 = 5; //swapping using swap() swap(val1, val2); //values after swapping cout<<"New value of val1 = "<<val1<<endl; cout<<"New value of val2 = "<<val2<<endl; return 0; } |
Output:
1 2 |
New value of val1 = 5 New value of val2 = 2 |
Here,
val1
andval2
are initialized with values 2 and 5 respectively- After we pass both of them to the
std::swap()
function the values of the same are printed to confirm the swapping - As we can see, val1 now holds 5 whereas, val2 stores 2. That is, we successfully swapped both the values
2. Swapping of two strings using swap()
Now, let us see how we can swap two string objects from the string header file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> #include <string> using namespace std; int main() { //strings before swapping string string1 = "String 1"; string string2 = "String 2"; //swapping strings using swap() swap(string1, string2); //strings after swapping cout<<"New value of string1 = "<<string1<<endl; cout<<"New value of string2 = "<<string2<<endl; return 0; } |
Output:
Clearly, from the output given below, our swapping is successful. Both the strings string1
and string2
values are exchanged.
3. Swapping two arrays() using swap()
We can even swap arrays using the swap()
function. Let us see how
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> using namespace std; int main() { //arrays before swapping int array1[3] = {1,2,3}; int array2[3] = {2,4,6}; int i; //swapping arrays using swap() swap(array1, array2); //arrays after swapping cout<<"New value of array1 = "<<endl; for(i=0;i<3;i++) cout<<" "<<array1[i]; cout<<"nNew value of array2 = "<<endl; for(i=0;i<3;i++) cout<<" "<<array2[i]; return 0; } |
Output:
In the code above:
- array1 and array2 are the two given arrays
- We pass both the arrays to the
swap()
function for swapping - After swapping, as we can see from the output, the contents of both the arrays are now exchanged
Note: Here we have used examples of swapping integers, strings and arrays for clear understanding. But the functions swapping ability is not limited to these data types. We can also swap other data structures like stacks and queues and other data types
Error while using the swap() function in C++
While using the swap()
function if we try to swap the values of two variables belonging to two different data types. The following error is thrown by the compiler.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> using namespace std; int main() { //arrays before swapping int array1[3] = {1,2,3}; char array2[3] = {'1','2','3'}; int i; //swapping arrays using swap() we get error here swap( array1, array2); //arrays after swapping cout<<"New value of array1 = "<<endl; for(i=0;i<3;i++) cout<<" "<<array1[i]; cout<<"nNew value of array2 = "<<endl; for(i=0;i<3;i++) cout<<" "<<array2[i]; return 0; } |
Error:
1 |
C:UserssnehaDesktopC++.cpp [Error] no matching function for call to 'swap(int [3], char [3])' |
Hence, for avoiding this type of conflict, we must make sure that the data types of the variables are the same.
Conclusion
Hope this tutorial helps to understand the use as well as the working of the swap()
function in C++. For any questions feel free to use the comments below.