You can generate an array, print it, and then sort it (and reprint it?).
Note: if true randomness is an issue for you, rand()
+ %
is not suited.
#include <array>
#include <algorithm>
#include <iostream>
int main()
{
std::array<int, 10> data;
std::generate(begin(data), end(data), rand);
for (auto n : data) {
std::cout << n << ", ";
}
std::cout << '\n';
std::sort(begin(data), end(data));
for (auto n : data) {
std::cout << n << ", ";
}
std::cout << '\n';
}
demo: http://coliru.stacked-crooked.com/a/2e62855189995ab0
Following: how could I define an operator to be able to write std::cout << data
whatever the type of data
is?.