C++ examples for STL Algorithm:swap_ranges
Swap elements in a sequence in a range with algorithm swap_ranges
#include <iostream> #include <algorithm> // algorithm definitions #include <iterator> using namespace std; int main() // www . j av a 2 s. c o m { const int SIZE = 10; int a[ SIZE ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; ostream_iterator< int > output( cout, " " ); cout << "Array a contains:\n "; copy( a, a + SIZE, output ); // display array a // swap elements in first five elements of array a with // elements in last five elements of array a swap_ranges( a, a + 5, a + 5 ); cout << "\nArray a after swapping the first five elements\n" << "with the last five elements:\n "; copy( a, a + SIZE, output ); cout << endl; }