Sort elements in a sequence with algorithm sort - C++ STL Algorithm

C++ examples for STL Algorithm:sort

Description

Sort elements in a sequence with algorithm sort

Demo Code

#include <iostream> 
#include <algorithm> // algorithm definitions 
#include <vector> // vector class-template definition 
#include <iterator> 
using namespace std; 

int main() //from   ww w .j ava2  s  .  c  o m
{ 
   const int SIZE = 10; 
   int a[ SIZE ] = { 10, 2, 17, 5, 16, 8, 13, 11, 20, 7 }; 
   vector< int > v( a, a + SIZE ); // copy of a 
   ostream_iterator< int > output( cout, " " ); 

   cout << "Vector v contains: "; 
   copy( v.begin(), v.end(), output ); // display output vector 

    // sort elements of v 
    sort( v.begin(), v.end() ); 
    cout << "\n\nVector v after sort: "; 
    copy( v.begin(), v.end(), output ); 

    cout << endl; 
}

Result


Related Tutorials