1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <random>
#include <vector>
struct S
{
int a;
int b;
int c;
};
bool order_by_a(const S& lhs, const S& rhs)
{
return lhs.a < rhs.a;
}
std::ostream& operator<<(std::ostream& out, const S& s)
{
return out << '(' << std::setw(2) << s.a <<
", " << std::setw(2) << s.b <<
", " << std::setw(2) << s.c << ')';
}
S generate_s()
{
static std::mt19937 gen{std::random_device{}()};
static std::uniform_int_distribution<> dist(0, 10);
//Return structure filled with 3 random values
return {dist(gen), dist(gen), dist(gen)};
}
//A template helper I created to not clutter main()
template<typename T>
std::ostream& print(std::ostream& out, T cont)
{
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
clang++ -std=c++14 -O2 -Wall -Wextra -pedantic-errors main.cpp -lsupc++ && ./a.out <<< '4'
How many array elements you want?
Unsorted:
( 1,  3,  6)
( 9,  8,  4)
( 4,  3,  8)
( 5,  6,  1)

Sorted by a:
( 1,  3,  6)
( 4,  3,  8)
( 5,  6,  1)
( 9,  8,  4)

Sorted by c:
( 5,  6,  1)
( 9,  8,  4)
( 1,  3,  6)
( 4,  3,  8)