×

C++ STL Tutorial

C++ STL Algorithm

C++ STL Arrays

C++ STL String

C++ STL List

C++ STL Stack

C++ STL Set

C++ STL Queue

C++ STL Vector

C++ STL Map

C++ STL Multimap

C++ STL MISC.

std::swap() function with example in C++ STL

In this article, we are going to see, what standard library function swap() is and what's the usage of it and how to use it in a program?
Submitted by Radib Kar, on August 03, 2020

C++ STL std::swap() Function

swap() is a standard library function that swaps the value b/w any two objects. In C11, it has been moved under <utility> header. Below is the syntax details for swap().

Syntax

void swap (T& a, T& b);

Parameter(s)

T& a, T& b which are the objects to be swapped

Return value

void - It returns nothing.

Usage

Swaps value b/w two objects

Example 1: Swapping two built-in data types

In the below example, we see how to swap two integers using std::swap() function.

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int a = 10, b = 20;
    
    cout << "Before swapping:\n";
    cout << "a: " << a << ", b: " << b << endl;

    //swap now
    swap(a, b);

    cout << "After swapping:\n";
    cout << "a: " << a << ", b: " << b << endl;

    return 0;
}

Output

Before swapping:
a: 10, b: 20
After swapping:
a: 20, b: 10

Example 2: Swapping two user-defined objects

In the below example, we see how to swap two user-defined objects using std::swap() function. Here we have defined a student class as we see how swap swaps the content between them.

#include <bits/stdc++.h>
using namespace std;

class student {
public:
    int roll;
    string name;
    int marks;

    student()
    {
        roll = -1;
        name = "";
        marks = 0;
    }
    student(int r, string s, int m)
    {
        roll = r;
        name = s;
        marks = m;
    }
};

void print(student t)
{

    cout << "Roll: " << t.roll << endl;
    cout << "Name: " << t.name << endl;
    cout << "Marks: " << t.marks << endl;
}

int main()
{
    student a(1, "Alice", 80);
    student b(2, "Bob", 85);

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

    //swap now
    swap(a, b);

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

    return 0;
}

Output

Before swapping:
Student a:
Roll: 1
Name: Alice
Marks: 80
Student b:
Roll: 2
Name: Bob
Marks: 85
After swapping:
Student a:
Roll: 2
Name: Bob
Marks: 85
Student b:
Roll: 1
Name: Alice
Marks: 80

Example 3: Swapping between two vectors (containers)

In the below example we are going to see how to swap the contents of two vectors. In the same way, we can swap be/w object of any container.

#include <bits/stdc++.h>
using namespace std;

void print(vector<int> a)
{
    for (auto it : a) {
        cout << it << " ";
    }
    cout << endl;
}

int main()
{
    vector<int> a{ 10, 20, 30, 40 };
    vector<int> b{ 1, 2, 3, 4, 5, 6, 7 };
 
    cout << "Before swapping:\n";
    cout << "Vector a:\n";
    print(a);
    cout << "Vector b:\n";
    print(b);

    //swap now
    swap(a, b);

    cout << "After swapping:\n";
    cout << "Vector a:\n";
    print(a);
    cout << "Vector b:\n";
    print(b);

    return 0;
}

Output

Before swapping:
Vector a:
10 20 30 40 
Vector b:
1 2 3 4 5 6 7 
After swapping:
Vector a:
1 2 3 4 5 6 7 
Vector b:
10 20 30 40 

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.