#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
using namespace std;
// to use user-defined comparison for sorting,
// the user needs to provide a function that
// returns true when the left argument should
// go earlier than the right argument (so for
// increasing order the comparison function
// returns true if i<j);
// this function ensures 3 rules
// 1. even numbers precede odd numbers
// 2. odd numbers should decrease
// 3. even numbers should increase
bool is_less(int i, int j)
{
// even numbers go before odd numbers
if ((i%2==0) && (j%2==1))
return true;
// even numbers should increase
if ((i%2==0) && (j%2==0))
return (i<j);
// odd numbers should decrease
if ((i%2==1) && (j%2==1))
return (i>j);
// otherwise, i should not be forced to
// precede j
return false;
}
int main()
{
int i;
int n = 18;
// initialize random number generator with random seed 123
srand48(123);
// two vectors of n elements of type int
vector<int> v(n);
// generate both vectors random, each variable from
// uniform distribution over interval {0, ..., 99}
for (i=0; i<n; i++)
v[i] = drand48()*100;
// print out the vector
cout << "initial vector:" << endl;
for (i=0; i<n; i++)
cout << " " << v[i] << endl;
// sort the vector (ascending order)
// (v.begin() and v.end() indicate that the sorting
// should consider the entire vector from its start
// on the left until the end on the right)
sort( v.begin(), v.end(), is_less);
// print out the vector
cout << "sorted vector:" << endl;
for (i=0; i<n; i++)
cout << " " << v[i] << endl;
// return from main with 0
return 0;
}