Algorithm Swap Ranges Function

C++ Algorithm swap_ranges exchanges the elements in the range [first1, last2) with the elements present in the range starting from first2.

In short we can say that, swap_ranges swaps the elements of two sequence, that is each element at a position in the first sequence is replaced by the element at the same position in the second sequence and vice-versa is also possible.

Syntax

Example

template <class ForwardIterator1, class ForwardIterator2>
  ForwardIterator2 swap_ranges (ForwardIterator1 first1, ForwardIterator1 last1,
                                ForwardIterator2 first2);

Parameter

first1 : A forward iterator pointing to the first position of the first range whose elements is to be swapped.

last1 : A forward iterator pointing to one past the final position of the first range whose elements are to be swapped.

first2 : A forward iterator pointing to the first position of the second range whose elements is to be swapped.

Return value

swap_ranges returns an iterator pointing to the end of the second range whose elements are to be exchanged.

Complexity

The complexity is linear between first and last. Perform a swap operation for each element in the range.

Data races

The objects in both ranges are modified.

Exception safety

Exception throws if either an element assignment or an operation on iterators throws.

Example 1

Let's see the simple example to swap the element of two vectors with the given range:

Example

#include <algorithm>
#include <iostream>
#include <vector>
#include <iterator>

using namespace std;

int main() {
  vector<int> v1 = { 1, 2, 3 };
  vector<int> v2 = { 4, 5, 6, 7, 8 };

  swap_ranges(v1.begin(), v1.end(), v2.begin());

  cout << "v1: ";
  copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, ", "));
  cout << endl;

  cout << "v2: ";
  copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, ", "));
  cout << endl;
}

Output:

Output

v1: 4, 5, 6, 
v2: 1, 2, 3, 7, 8,

In the above example, elements of vector v1 is swapped by vector v2 from range begin to end of v1.

Example 2

Let's see a simple example to exchange the contents of two vectors:

Example

#include <iostream>     
#include <utility>
#include <vector>

using namespace std;

int main ()
{
   vector<int> v, c;
   for(int j=0; j < 10; j++)
   {
       v.push_back(j);
       c.push_back(j+1);
   }

   swap_ranges(v.begin(), v.begin()+5, c.begin());

   /* swaps the first five element of 
   vector v by the elements of vector c */

   for(vector<int>::iterator i = v.begin() ; i!= v.end() ; i++)
   cout<< *i <<" ";

   cout<<endl;

   for(vector<int>::iterator k = c.begin() ; k!= c.end() ; k++)
   cout<<*k<<" ";
}

Output:

Output

1 2 3 4 5 5 6 7 8 9 
0 1 2 3 4 6 7 8 9 10

In the above example, swap_range function swaps the first five element of vector v by the elements of vector c.

Example 3

Let's see a simple example to swap the contents of vector and deque:

Example

#include <vector>  
#include <deque>  
#include <algorithm>  
#include <iostream>  
  
int main( )   
{  
   using namespace std;  
   
   vector <int> v1;  
   deque <int> d1;  
   vector <int>::iterator v1Iter1;  
   deque<int>::iterator d1Iter1;  
  
   int i;  
   for ( i = 0 ; i <= 5 ; i++ )  
   {  
      v1.push_back( i );  
   }  
  
   int ii;  
   for ( ii =4 ; ii <= 9 ; ii++ )  
   {  
      d1.push_back( 6 );  
   }  
  
   cout << "Vector v1 is: ( " ;  
   for ( v1Iter1 = v1.begin( ) ; v1Iter1 != v1.end( ) ;v1Iter1 ++ )  
      cout << *v1Iter1  << " ";  
   cout << ")." << endl;  
  
   cout << "Deque d1 is:  ( " ;  
   for ( d1Iter1 = d1.begin( ) ; d1Iter1 != d1.end( ) ;d1Iter1 ++ )  
      cout << *d1Iter1  << " ";  
   cout << ")." << endl;  
  
   swap_ranges ( v1.begin ( ) , v1.end ( ) , d1.begin ( ) );  
  
   cout << "After the swap_range vector v1 is: ( " ;  
   for ( v1Iter1 = v1.begin( ) ; v1Iter1 != v1.end( ) ;v1Iter1 ++ )  
      cout << *v1Iter1 << " ";  
   cout << ")." << endl;  
  
   cout << "After the swap_range deque d1 is:   ( " ;  
   for ( d1Iter1 = d1.begin( ) ; d1Iter1 != d1.end( ) ;d1Iter1 ++ )  
      cout << *d1Iter1 << " ";  
   cout << ")." << endl;  
   
   return 0;
}

Output:

Output

Vector v1 is: ( 0 1 2 3 4 5 ).
Deque d1 is:  ( 6 6 6 6 6 6 ).
After the swap_range vector v1 is: ( 6 6 6 6 6 6 ).
After the swap_range deque d1 is:  ( 0 1 2 3 4 5 ).

In the above example, elements of vector and deque are exchanged to each other.

Example 4

Let's see another simple example:

Example

#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

int main()
{
	string s1("Betty Botter bought some butter") ;
	string s2("But she said the butter was bitter") ;
	string s3("So she got some better butter") ;
	string s4("to make the bitter butter better") ;

	cout << "s1 = " << s1 << endl ;
	cout << "s2 = " << s2 << endl ;
	cout << "s3 = " << s3 << endl ;
	cout << "s4 = " << s4 << endl ;
	
	cout << "\nJumble them up!!!" << std::endl ;

	//swap_ranges
	swap_ranges(s1.begin(), s1.begin()+15, s2.begin()) ;
	swap_ranges(s3.begin(), s3.begin()+15, s4.begin()) ;

	cout << "s1 = " << s1 << endl ;
	cout << "s2 = " << s2 << endl ;
	cout << "s3 = " << s3 << endl ;
	cout << "s4 = " << s4 << endl ;

	return 0 ;
}

Output:

Output

s1 = Betty Botter bought some butter
s2 = But she said the butter was bitter
s3 = So she got some better butter
s4 = to make the bitter butter better

Jumble them up!!!
s1 = But she said thught some butter
s2 = Betty Botter boe butter was bitter
s3 = to make the bit better butter
s4 = So she got someter butter better

Input Required

This code uses input(). Please provide values below: