an example of using STL algorithm sort on a vector<int>


👍 g++ -std=c++11 vector_sort.cpp
👍 ./a.out
2 7 1 8 2 8 1 8 2 8 
1 1 2 2 2 7 8 8 8 8 
1 1 2 2 2 7 8 8 8 8 
8 8 8 8 7 2 2 2 1 1 
8 8 8 8 7 2 2 2 1 1 
8 8 8 8 7 2 2 2 1 1

👍 cat vector_sort.cpp
#include <iostream>
#include <vector>
using namespace std;

template<typename I>
void prt(I i, I end) {
  for(; i != end; ++i)
    cout << *i << ' ';
  cout << endl;
}

bool f1(int m, int n)
  { return m > n; }

class f2 {
public:
  f2() {}
  bool operator()(int m, int n)
    { return m > n; }
};

int main() {
  vector<int> c{2,7,1,8,2,8,1,8,2,8};
  vector<int> v = c;
  prt(v.begin(), v.end());
  
  sort(v.begin(), v.end());
  prt(v.begin(), v.end());

  v = c;
  sort(v.begin(), v.end(), 
    less<int>());
  prt(v.begin(), v.end());

  v = c;
  sort(v.begin(), v.end(),
    greater<int>());
  prt(v.begin(), v.end());

  v = c;
  sort(v.begin(), v.end(), f1);
  prt(v.begin(), v.end());

  v = c;
  sort(v.begin(), v.end(), f2());
  prt(v.begin(), v.end());
}