Difference between std::swap and std::vector::swap
The std::swap function is used to swap two elements where as the std::vector::swap function can swap all elements 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 and size. |
| The std::swap() is comparitively slower than std::vector::swap() function. | The std::vector::swap() is comparitively faster than std::swap() function. |
| If std::swap() function is used for swapping two vectors, it will take O(n) time because it swaps the elements of two vector one by one. | The std::vector::swap function is faster than the normal swap function as 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). |
Program 1: To illustrate swapping of two vectors using std::swap().
// 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; } |
chevron_right
filter_none
Output:
Vector v1 = 4 5 6 Vector v2 = 1 2 3
Program 2: To illustrate swapping of two vectors using std::vector::swap().
// 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; } |
chevron_right
filter_none
Output:
Vector v1 = 4 5 6 Vector v2 = 1 2 3
Recommended Posts:
- Difference between 1G and 2G
- Difference between TDM and FDM
- Difference between H.323 and SIP
- Web 1.0, Web 2.0 and Web 3.0 with their difference
- Difference between LAN, MAN and WAN
- What's difference between MMU and MPU?
- Difference between LAN and WAN
- Difference between CPU and GPU
- Difference between CRT and LCD
- Difference between LAN and MAN
- Difference between CD and DVD
- Difference between C and C++
- Difference between MAN and WAN
- Difference Between BFS and DFS
- What is the difference between GUI and CUI?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



