Open In App

swap() in C++

Last Updated : 10 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The swap() is a built-in function in the C++ STL which swaps the value of two variables. This function supports almost every data type available in C++, whether it is a primitive type such as int, char, etc. or an STL containers such as vector, map, etc.

Example:

C++
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    int a = 1, b = 55;
    cout << a << " " << b << endl;
    
    // swapping the values of a and b
    swap(a, b);
    
    cout << a << " " << b;
    return 0;
}

Output
1 55
55 1

In the above example, we exchange the values of two variables a and b using swap function.

Syntax

The std::swap() function is defined inside <algorithm> header file.

C++
std::swap(a, b);

where a and b are two variables that are to be swapped with each other. This function does not return any value.

Examples

The following examples demonstrate how to use swap function in C++.

Swap Two Vectors

The swap function can easily swap elements of two vectors in the same way as other variables.

C++
//Driver Code Starts{
#include <bits/stdc++.h>
using namespace std;

int main() {
//Driver Code Ends }

    vector<int> v1 = {1, 2, 3, 4};
    vector<int> v2 = {6, 7, 8, 9};

    // Swapping the vectors
    swap(v1, v2);

//Driver Code Starts{

    cout << "v1: ";
    for (auto i : v1)
        cout << i << " ";
    cout << endl;
  
    cout << "v2: ";
    for (auto i : v2)
        cout << i << " ";
    return 0;
}
//Driver Code Ends }

Output
v1: 6 7 8 9 
v2: 1 2 3 4 

The swap function just swaps the internal address of the allocated memory in the vector, so, the process only requires constant time and space.

Swap Two Arrays

swap() also works for raw arrays.

C++
//Driver Code Starts{
#include <bits/stdc++.h>
using namespace std;

int main() {
//Driver Code Ends }

    int arr1[] = {1, 2, 3, 4};
    int arr2[] = {6, 7, 8, 9};

    // Swapping the vectors
    swap(arr1, arr2);

//Driver Code Starts{

    cout << "arr1: ";
    for (auto i : arr1)
        cout << i << " ";
    cout << endl;
  
    cout << "arr2: ";
    for (auto i : arr2)
        cout << i << " ";
    return 0;
}
//Driver Code Ends }

Output
arr1: 6 7 8 9 
arr2: 1 2 3 4 

Both the arrays should be of the same size and type for swap() to work. Also, arrays’ address cannot be swapped, so each element is swapped one by one leading to the linear time complexity.

swap() for Custom Class

Your custom class can also have a specialized swap function which in turn uses the inbuilt std::swap(). It helps your class in working seamlessly with STL algorithms that uses swap operation.

Example:

C++
#include <iostream>
using namespace std;

class GfG {
    int a, b;
public:
    GfG(int x, int y) : a(x), b(y) {}
    
    // Custom swap function for GfG class
    friend void swap(GfG& first, GfG& second) noexcept {
        
        // for ADL
        using std::swap;
        swap(first.a, second.a);
        swap(first.b, second.b);
    }
    
    void print() {
        cout << a << " " << b << endl;
    }
};

int main() {
    GfG gfg1(1 ,22);
    GfG gfg2(99, 8);
    
    // Swapping values
    swap(gfg1, gfg2);
    
    gfg1.print();
    gfg2.print();
    return 0;
}

Output
99 8
1 22

In the above program, the swap() function is declared as friend to keep the same API as std::swap() because member function needs to be called using one of the objects, changing the way we use swap(). It is also declared noexcept to provide string exception guarantee.



Next Article
Article Tags :
Practice Tags :

Similar Reads