The Wayback Machine - https://web.archive.org/web/20240204105132/https://www.geeksforgeeks.org/difference-between-stdswap-and-stdvectorswap/
Open In App
Related Articles

Difference between std::swap and std::vector::swap

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

The std::swap is a general function used to exchange the given values whereas the std::vector::swap is a specialized function that can swap all the contents of two different vector containers.
Below are some major key differences between std::swap and std::vector::swap, 
 

std::swap std::vector::swap
The std::swap() is a built-in function in C++ STL which swaps the value of any two variables passed to it as parameters. The std::vector::swap() function is used to swap the entire contents of one vector with another vector of same type.
If std::swap() function is used for swapping two vectors A and B, it will call specialized std::swap algorithm for std::vector which in turn calls A.swap(B) and swaps the contents. The std::vector::swap function exchanges the contents of one vector with another. It swaps the addresses(i.e. the containers exchange references to their data) of two vectors rather than swapping each element one by one which is done in constant time O(1).
The overloads of std::swap for container adaptors were introduced in C++11. Prior versions of C++ will require linear time complexity to swap vectors The std::vector::swap function will always swap the contents of vector in constant time.

In practice, both the functions will swap the contents of vectors in O(1) time and give the same performance. For consistency, it may be better to use the std::swap.

Program 1: To illustrate swapping of two vectors using std::swap(). 
 

CPP




// CPP program to illustrate swapping
// of two vectors using std::swap()
 
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    vector<int> v1 = { 1, 2, 3 };
    vector<int> v2 = { 4, 5, 6 };
 
    // swapping the above two vectors
    // by traversing and swapping each element
    for (int i = 0; i < 3; i++) {
        swap(v1[i], v2[i]);
    }
 
    // print vector v1
    cout << "Vector v1 = ";
    for (int i = 0; i < 3; i++) {
        cout << v1[i] << " ";
    }
 
    // print vector v2
    cout << "\nVector v2 = ";
    for (int i = 0; i < 3; i++) {
        cout << v2[i] << " ";
    }
 
    return 0;
}


Output: 

Vector v1 = 4 5 6 
Vector v2 = 1 2 3

 

Program 2: To illustrate swapping of two vectors using std::vector::swap(). 
 

CPP




// CPP program to illustrate swapping
// of two vectors using std::vector::swap()
 
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    vector<int> v1 = { 1, 2, 3 };
    vector<int> v2 = { 4, 5, 6 };
 
    // swapping the above two vectors
    // using std::vector::swap
    v1.swap(v2);
 
    // print vector v1
    cout << "Vector v1 = ";
    for (int i = 0; i < 3; i++) {
        cout << v1[i] << " ";
    }
 
    // print vector v2
    cout << "\nVector v2 = ";
    for (int i = 0; i < 3; i++) {
        cout << v2[i] << " ";
    }
 
    return 0;
}


Output: 

Vector v1 = 4 5 6 
Vector v2 = 1 2 3

 


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Looking for a place to share your ideas, learn, and connect? Our Community portal is just the spot! Come join us and see what all the buzz is about!


Last Updated : 15 Nov, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads