The swap() function in C++ With Examples


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:



  • 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,



  • val1 and val2 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.



String Swapping

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



Swapping Arrays

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.


Hence, for avoiding this type of conflict, we must make sure that the data types of the variables are the same.


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.


By admin

Leave a Reply

%d bloggers like this: