Sort the entire container : sort « STL Algorithms Sorting « C++






Sort the entire container

  
#include <cstdlib>
#include <iostream>
#include <vector>
#include <functional>
#include <algorithm>

using namespace std;

void show(const char *msg, vector<int> vect);

int main()
{
  vector<int> v(10);
  for(unsigned i=0; i < v.size(); i++)
    v[i] = rand() % 100;

  show("Original order:", v);

  sort(v.begin(), v.end());

  show("Order after sorting into natural order:", v);

  return 0;
}

void show(const char *msg, vector<int> vect) {
  cout << msg << endl;
  for(unsigned i=0; i < vect.size(); ++i)
    cout << vect[i] << endl;
}
  
    
  








Related examples in the same category

1.Using an in-place generic sort algorithm
2.Sort a vector and print out the sorted elements
3.Sort all element in an array
4.Sort part of the elements in an array
5.Sort a vector into ascending order of id members
6.Sort elements in deque
7.Sort elements reversely with custom function
8.Using the generic sort algorithm with a binary predicate: greater
9.Use custom function and sort to sort strings by length
10.Sorting user-defined class
11.Sort into descending order by using greater
12.Sort a subset of the container
13.Sort random number
14.Sort a string array with sort()