Home » C++ Tutorial » C++ Swap with Examples

C++ Swap with Examples

All You Need to Know About C++ Swap. In the world of programming languages, it is often very easy to get lost in translation, literally, in the ocean of technical jargon, systems, functions, ubiquitous code, and other confusing factors.

Given the fact that most programming languages are different and none are the exact same, a programmer, developer, or even a beginner needs to constantly upgrade their knowledge about the different coding scripts so as to be proficient in most of them, if not all.

When talking about versatile programming languages, C++ is easily at the top of the list, with its extensive list of valuable functions and keys that make it that much easier to learn. One of the key characteristics of C++ is that it has multiple functions that make tasks easier than if they were to be executed with a different coding script. In this article, we will be talking about one such function, the C++ swap function

Before we dive into the details of how to use it, let us look at how we can define swap and how this function can help us.

C++ Swap Definition:

The implication of the term ‘swap’ is the same as it is in the spoken English language. The swap function in C++ referred to as std::swap, simply swaps, or exchanges the values between two variables. Keep in mind that the variables and their values can be of any data type.

This function cuts down the process in half, as compared to if it were being done without the function itself. Let us see an example of exchanging values of variables without the std::swap function.

Example of code without C++ swap function:

#include <iostream>
using namespace std;
int main()
{
int x= 4;
int y= 8;

x= y;
y= x;
cout << “The value of X is:” << x << endl;
cout << “The value of Y is:” << y << endl;
}

OUTPUT:

The value of X is: 8
The value of Y is: 8

You may think that this much may be enough in order to exchange values between variables without the C++ swap function, however, it is a little more complex than that. The reason that the previous example did not work is that as soon as we mentioned the statement “x = y” x took up the same value as y, which is 8. By doing so, we are effectively destroying the original value of x (4) and replacing it with that of y (8). After that, when we mention “y = x” we are adapting the value of y (8) to the changed value of x (8). This is why the output displays both the values of x and y to be 8.

As a workaround, we can create a third variable that holds the original value of x. This might be done by working with the following method:

#include <iostream>
using namespace std;
int main()
{
int x= 4;
int y= 8;
int z = x
x= y;
y= z;
cout << “The value of X is:” << x << endl;
cout << “The value of Y is:” << y << endl;
}

OUTPUT:

The value of X is: 8
The value of Y is: 4

Working like this is futile as using the std::swap function makes this entire process a lot easier and quicker. The base syntax of the C++ swap function is as follows:

swap (x, y)

Therefore, considering the same example as before, let us try executing it with the swap function in C++ instead.

#include <iostream>
using namespace std;
int main()
{
int x= 4;
int y= 8;
cout << “The original value of X is:” << x << endl;
cout << “The original value of Y is:” << y << endl;
std::swap (x, y);
cout << “The swapped value of X is:” << x << endl;
cout << “The swapped value of Y is:” << y << endl;
}

OUTPUT:

The original value of X is: 4
The original value of Y is: 8
The swapped value of X is: 8
The swapped value of Y is: 4

As you can see, the std::swap function effectively swapped the values of the selected variables without all the unnecessary complications. Keep in mind that the swap function will require both of its parameters or variables to be non-const references. Simply put, if you wanted to perform the following:

std::swap (x, 8)

If the aforementioned example is typed, the program will not be compiled and your variables will fail to be swapped

Another factor to note is that even though it is normal to use the terms ‘swap’ and ‘exchange’ interchangeably in the spoken English language, it does not have the same implications with C++.

There is a separate function which is called the exchange (std::exchange) function, completely different from the C++ swap (std::swap) function that we learned about today. It is easy to get confused between the two but if switched, you will not be able to acquire the results that you desire out of your C++ program.

Conclusion:

As you may have already witnessed, most of the functions in C++ are fairly simple to understand and are extremely easy to learn. As simple as they may be, they cut the time taken to do tedious tasks in half by offering quick and convenient solutions to most of the problems that developers, programmers, and coders tend to face.

This is also one of the reasons why C++ is deemed to be one of the most versatile programming languages of all since it has numerous shortcuts and tricks that the coders can use to maximize efficiency and minimize time and effort spent, as compared to if they were to use any other coding script. Let the C++ swap (std::swap) function be one of the many things you breeze through on your way to mastering C++. Keep learning, practicing, creating, and growing!

Pin It on Pinterest