std::swap function in C++

Free Linux Book

Get FREE domain for 1st year and build your brand new site

In this article, we have explained the use of std::swap function in C++ with multiple C++ code examples covering primitive data types to User defined classes.

Table of contents:

  1. What is Swapping?
  2. What is std::swap in C++?
  3. C++ Code Examples of std::swap

To understand different techniques to swap two variables (beyond the basic technique), go through this article. It is a must read.

What is Swapping?

Swapping refers to the exchange of two or more objects. In programming, data may be swapped between two variables. In real-life, objects may be swapped between two people.

What is std::swap in C++?

std::swap is a built in function of C++ Standard Template Library (STL) which swaps two variables, vectors or objects.

Syntax :

std::swap(a,b)

:: is the scope resolution operator in C++.

To use swap directly instead of using std, we need to set the namespace std like:

using namespace std;
swap(a, b);

Parameters : This function requires two parameters which are mandatory. Although, the parameters can have any data type; it is necessary that both the parameters should have the same data type.

Return Value : None

C++ Code Examples of std::swap

Swapping two Variables

In this C++ code example, we have used the swap function to swap two integer variables a and b. Note the values of variables a and b are exchanged.

using namespace std;
int main()
{
	int a = 5;
	int b = 10;

	cout<<"Values before swapping: \n";
	cout<<"a = "<<a<<endl;
	cout<<"b = "<<b<<endl;

	swap(a,b);

	cout<<"Values after swapping: \n";
	cout<<"a = "<<a<<endl;
	cout<<"b = "<<b<<endl;

} 

Output:

Values before swapping: 
a = 5
b = 10
Values after swapping: 
a = 10
b = 5

Time Complexity : O(1)

Swapping two Objects

In this C++ code example, we have objects of user defined class Student with 2 data members. We have used the swap function to swap two Student objects. Note that values of all data members have been swapped.

using namespace std;
class student {
public:
    int roll, age;

    student()
    {
        roll = 0;
        age = 0;
    }
    student(int r, int a)
    {
        roll = r;
        age = a;
    }
};

void print(student s)
{

    cout << "Roll: " << s.roll<<"\t";
    cout << "Age: " << s.age<<"\n";
}

int main()
{
	Fast_IO();
	
	student a(1, 8);
    student b(2, 10);

    cout << "Object before swapping:\n";
    cout << "Student a:\n";
    print(a);
    cout << "Student b:\n";
    print(b);

    //swap now
    swap(a, b);

    cout << "\nObject after swapping:\n";
    cout << "Student a:\n";
    print(a);
    cout << "Student b:\n";
    print(b);
}

Output:

Object before swapping:
Student a:
Roll: 1	Age: 8
Student b:
Roll: 2	Age: 10

Object after swapping:
Student a:
Roll: 2	Age: 10
Student b:
Roll: 1	Age: 8

Time Complexity : O(1)

This takes constant time as the number of data members is limited in the user-defined class for this case.

Swapping two Arrays

In this C++ code example, we have used the swap function to swap two arrays. Note that values at every index have been swapped.

using namespace std;
int main()
{
	Fast_IO();
	
	int a[] = {5,4,3,2};
	int b[] = {10,11,12,13};

	cout<<"Values before swapping: \n";
	cout<<"Array a : \t";
	for(int i=0;i<4;i++){
		cout<<a[i]<<"\t";
	}
	cout<<"\nArray b : \t";
	for(int i=0;i<4;i++){
		cout<<b[i]<<"\t";
	}

	swap(a,b);

	cout<<"\n\nValues after swapping: \n";
	cout<<"Array a = \t";
	for(int i=0;i<4;i++){
		cout<<a[i]<<"\t";
	}
	cout<<"\nArray b = \t";
	for(int i=0;i<4;i++){
		cout<<b[i]<<"\t";
	}

} 

Output:

Values before swapping: 
Array a : 	5	4	3	2	
Array b : 	10	11	12	13	

Values after swapping: 
Array a = 	10	11	12	13	
Array b = 	5	4	3	2	

Time Complexity : O(N)

This takes linear time O(N) as all values in the array are swapped one by one.

Question

You are given two arrays A = {1,2,3,4,5} and B = {19,20,1}. What will be the output when you swap them using the C++ STL swap()?

error: no matching function for call to 'swap'
A = {19,20,1} and B = {1,2,3,4,5}
Segmentation Fault (SIGSEGV)
None
Since both arrays have different sizes, the smaller array cannot hold all the elements of the larger array.

With this article at OpenGenus, you will have the complete knowledge of using swap function in C++.