Swap element in a sequence using iterators with algorithm iter_swap - C++ STL Algorithm

C++ examples for STL Algorithm:iter_swap

Description

Swap element in a sequence using iterators with algorithm iter_swap

Demo Code

#include <iostream> 
#include <algorithm> // algorithm definitions 
#include <iterator> 
using namespace std; 

int main() //from  w w w  . j a  va  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 

    // use iterators to swap elements at locations 0 and 1 of array a 
    iter_swap( &a [ 0 ], &a [ 1 ] ); // swap with iterators 
    cout << "\nArray a after swapping a[0] and a[1] using iter_swap :\n                   "; 
    copy( a, a + SIZE, output ); 

    cout << endl; 
}

Result


Related Tutorials