Swap element in a sequence with algorithm swap - C++ STL Algorithm

C++ examples for STL Algorithm:swap

Description

Swap element in a sequence with algorithm swap

Demo Code

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

int main() //from  ww w  .j a  va  2 s.  c om
{ 
    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 at locations 0 and 1 of array a 
    swap( a[ 0 ], a[ 1 ] ); 

    cout << "\nArray a after swapping a[0] and a[1] using swap:\n                   "; 
    copy( a, a + SIZE, output ); // display array a 

}

Result


Related Tutorials