C++ vector::swap() Function



The C++ vector::swap() function is used to swap the elements of two vectors with each other.The fact that the vectors have varied lengths is not very problematic. Two vectors can only be switched if they are of the same type. The time complexity of the swap() function is constant.

The member function of the vector class can swap itself and another vector. The algorithm library also includes various swap functions with distinct names and for other purposes. The key distinction between the algorithm library swap functions and the vector swap() function is that while the vector function swaps its vector with another vector, the algorithm library swap functions each swap two independent vectors.

Syntax

Following is the syntax for C++ vector::swap() Function −

void swap (vector& x);

Parameters

x − It indicates the another vector of the same type whose content is to be swapped.

Example 1

Let's consider the following example, where we are going to use swap() function.

#include <iostream>
#include <vector>
using namespace std;

int main(){
   vector<int> tutorial1{11,22,33,44};
   vector<int> tutorial2{111,222,333,444};
   tutorial1.swap(tutorial2);
   cout << "vector1 Elements are : ";
   for (auto x = tutorial1.begin();
      x < tutorial1.end(); ++x)
   cout << *x << " ";
   cout << endl<< "vector2 Elements are : ";
   for (auto x = tutorial2.begin();
      x < tutorial2.end(); ++x)
   cout << *x << " ";
   return 0;
}

Output

When we compile and run the above program, this will produce the following result −

vector1 Elements are : 111 222 333 444 
vector2 Elements are : 11 22 33 44 

Example 2

Considering the another scenario, where we are going to take two different size vector and applying swap() function.

#include <iostream>
#include <vector>
using namespace std;

int main(){
   vector<int> myvector1{12,23};
   vector<int> myvector2{34,45,56,67};
   myvector1.swap(myvector2);
   cout << "The vec1 elemets are:";
   for (int i = 0; i < myvector1.size(); i++)
      cout << ' ' << myvector1[i];
   cout << '\n';
   cout << "The vec2 elemets are:";
   for (int i = 0; i < myvector2.size(); i++)
      cout << ' ' << myvector2[i];
   cout << '\n';
   return 0;
}

Output

On running the above program, it will produce the following result −

The vec1 elemets are: 34 45 56 67
The vec2 elemets are: 12 23

Example 3

In the following example, we are going to use char type and applying swap() function between iterators.

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main(){
   vector<char> myvector1 = {'T', 'P'};
   vector<char> myvector2 = {'W', 'E', 'L', 'C', 'O', 'M', 'E'};
   vector<char>::iterator x = myvector1.begin();
   vector<char>::iterator y = myvector2.begin();
   swap(x, y);
   for (x = x; x != myvector2.end(); x++) {
      cout << *x << ' ';
   }
   cout << endl;
   for (y = y; y != myvector1.end(); y++) {
      cout << *y << ' ';
   }
   cout << endl;
   return 0;
}

Output

When we execute the above program, it will produce the following result −

W E L C O M E 
T P 
Advertisements