Algorithm Swap Ranges Function - C++ Programming Tutorial
C++ Course / Advanced Topics / Algorithm Swap Ranges Function

Algorithm Swap Ranges Function

BLUF: Mastering Algorithm Swap Ranges Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Algorithm Swap Ranges Function

C++ is renowned for its efficiency. Learn how Algorithm Swap Ranges Function enables low-level control and high-performance computing in the tutorial below.

C++ Algorithm swap_ranges swaps the elements within the range [first1, last2) with the elements in the range beginning from first2.

In brief, the swap_ranges function exchanges the elements between two sequences. This means that each element at a specific position in the first sequence gets replaced by the element at the corresponding position in the second sequence, and vice versa.

Syntax

Example

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

Parameter

A forward iterator indicating the initial position of the first range whose elements are intended to be interchanged.

last1: A forward iterator pointing to the element immediately following the last element of the initial range that is intended to undergo swapping.

first2: An iterator that points forwards to the initial position within the second range where the elements are intended to be exchanged.

Return value

The function swap_ranges provides an iterator that points to the end of the second range where elements are swapped.

Complexity

The complexity increases linearly from the first to the last element. Execute a swap operation for every element within the specified range.

Data races

The objects in both ranges are modified.

Exception safety

An exception is triggered if either an assignment of an element or an operation on iterators results in an error.

Example 1

Explore a basic example demonstrating how to interchange the elements of two vectors within a specified 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 example provided, the elements of vector v1 are interchanged with those of vector v2 within the range starting from the beginning to the end of v1.

Example 2

Let's examine a basic illustration demonstrating how to swap the elements 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 example provided, the swap_range function exchanges the initial five elements of vector v with the elements from vector c.

Example 3

Let's explore a basic illustration demonstrating the interchange of elements between a vector and a 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 example provided, the elements of vector and deque are swapped with 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:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience