C++ String swap() with examples

In this article, we will go through the swap() function from the C++ standard library. A string is a variable that stores a series of characters. As we know, we can use the swap function to swap the values of two strings.

We can perform swap operations in two ways: without using the built-in swap() function and using the built-in swap() function. We always prefer to use shortcuts right!!  .C++ provides us with an inbuilt swap () function, which we can simply use when we need to swap values of two string objects.

In both ways, we need to enter two input strings whose values must be swapped.

We require an extra variable for the first approach, which is a waste of memory. Here we need to take a third variable and, using that, we have to swap the values.

So, in order to save space, we may use the built-in swap() function.

Inbuilt swap() functions in C++

In C++, we have two functions named swap() that we can use.

  • swap() function with two parameters
  • swap() function with single parameter

First Approach :

The first one, this swap() built-in function, belongs to the C++ Standard Template Library (STL), and it swaps the values of two variables. The function takes two necessary parameters, a, and b, which must be swapped. Parameters such as string, int, float, and arrays, among others, can be passed. Similarly, In order to swap two strings, we can pass two string parameters to this function. There is no need to use a third variable. Since this inbuilt swap() function does not return any value. Its return type is void.

The swap function which we are going to call is void swap(string& str1, string& str2)

Syntax : 

swap( string1 , string2 );

C++ Code :

#include <iostream> 
#include<cstdlib>
using namespace std;
int main() 
{ 
    string str1 = "Speed";
    string str2 = "Code";
    cout<<"Before swapping \nstring1 is : "+str1+"\nstring2 is : "+str2<<"\n";
    swap(str1,str2);
    cout<<"After swapping \nstring1 is : "+str1+"\nstring2 is : "+str2<<"\n";
    return 0;
}

In the above code, initially, we have to include the standard library which consists of an inbuilt swap() function in CPP. Consider any two input strings. these two inputs are passed as parameters to the swap() function. Internally swaps of two string objects are performed and we can see in the output that str1 and str2 values are swapped.

Output :

Before swapping 
string1 is : Speed
string2 is : Code
After swapping 
string1 is : Code
string2 is : Speed

Second Approach :

Next is a swap() member function with only one argument. This function is used to exchange the values of two string objects. There is no return value for this function so its return type is void. This function is present in the C++ string library.

The swap function which we are going to call is void string::swap(string& str)

Syntax:

string1.swap(string2);

C++ Code :

#include <iostream> 
#include<cstring>
using namespace std;
int main() 
{ 
    string str1 = "Happy";
    string str2 = "Code";
    cout<<"Before swapping \nstring1 is : "+str1+"\nstring2 is : "+str2<<"\n";
    str1.swap(str2);
    cout<<"After swapping \nstring1 is : "+str1+"\nstring2 is : "+str2<<"\n";
    return 0;
}

Here consider two input strings. The function swaps *this(str1) and string str2. Internally swaps of two string objects are performed and we can see in the output that str1 and str2 values are swapped.

Output :

Before swapping 
string1 is : Happy
string2 is : Code
After swapping 
string1 is : Code
string2 is : Happy

I hope you have understood the whole discussion.

Leave a Reply

Your email address will not be published. Required fields are marked *