123456789101112131415161718192021222324252627282930313233343536
#include <random> #include <iostream> #include <vector> #include <cstdint> #include <limits> #include <algorithm> int main() { std::mt19937 engine { std::random_device()() }; std::uniform_int_distribution<std::uint64_t> dist { 1, std::numeric_limits<std::uint64_t>::max() }; std::vector<std::uint64_t> vals; vals.reserve(11000000); for (unsigned i = 0; i < 11000000; ++i) vals.push_back(dist(engine)); auto val_to_check = vals[dist(engine, { 0, vals.size() - 1 })]; unsigned n_before_sort = 0; for (auto val : vals) if (val == val_to_check) ++n_before_sort; std::cout << "There are " << n_before_sort << " occurrences of " << val_to_check << " prior to the sort.\n"; std::sort(vals.begin(), vals.end()); auto it = std::lower_bound(vals.begin(), vals.end(), val_to_check); unsigned n_after_sort = 0; while (*it == val_to_check) ++n_after_sort, ++it; std::cout << "There are " << n_after_sort << " occurrences of " << val_to_check << " after the sort.\n"; }