MCPcopy Create free account
hub / github.com/Apress/beginning-cpp20 / main

Function main

Exercises/Modules/Chapter 20/Soln20_08/Soln20_08.cpp:20–43  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

18}
19
20int main()
21{
22 const size_t num_numbers{ 25'000 };
23 std::vector<int> numbers(num_numbers);
24
25 // Why not use an algorithm as well to generate the random numbers!!
26 // (Note: technically generate() is allowed to copy a function object
27 // as often as it wants. std::ref() ensures that the algorithm
28 // at all times operates on a reference to our random-number generator)
29 auto generator{ createUniformPseudoRandomNumberGenerator(0, 10'000) };
30 std::ranges::generate(numbers, std::ref(generator));
31
32 // Algorithm number two
33 std::ranges::sort(numbers);
34
35 // And number three; this time combined with the remove-erase idiom (or unique-erase, if you will)
36 /* Option 1: iterator-based */
37 numbers.erase(std::unique(begin(numbers), end(numbers)), end(numbers));
38 /* Option 2: range-based (sadly requires two statements...) */
39 //const auto [to_erase_begin, to_erase_end] { std::ranges::unique(numbers) };
40 //numbers.erase(to_erase_begin, to_erase_end);
41
42 std::cout << "Number of unique numbers: " << numbers.size() << std::endl;
43}

Callers

nothing calls this directly

Calls 3

sortFunction · 0.50
sizeMethod · 0.45

Tested by

no test coverage detected