-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextra_excercise_1.cpp
More file actions
63 lines (50 loc) · 1.62 KB
/
extra_excercise_1.cpp
File metadata and controls
63 lines (50 loc) · 1.62 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <algorithm>
#include <numeric>
#include <vector>
#include <iterator>
#include <iostream>
#include <random>
#include <ranges>
// Select elements and copy them to a new vector.
//
// This version of "select" can only run sequentially, because the output
// vector w is built consecutively during the traversal of the input vector v.
template<class UnaryPredicate>
std::vector<int> select(const std::vector<int>& v, UnaryPredicate pred)
{
std::vector<int> w;
// Reserve capacity to avoid multiple reallocations
w.reserve(v.size());
// Copy elements from v to w that satisfy the predicate
std::copy_if(v.begin(), v.end(), std::back_inserter(w), pred);
return w;
}
// Initialize vector
void initialize(std::vector<int>& v);
int main(int argc, char* argv[])
{
if (argc != 2) {
std::cerr << "ERROR: Missing length argument!" << std::endl;
return 1;
}
long long n = std::stoll(argv[1]);
auto v = std::vector<int>(n);
initialize(v);
auto predicate = [](int x) { return x % 3 == 0; };
auto w = select(v, predicate);
if (!std::all_of(w.begin(), w.end(), predicate) || w.empty()) {
std::cerr << "ERROR!" << std::endl;
return 1;
}
std::cerr << "OK!" << std::endl;
std::cout << "w = ";
std::copy(w.begin(), w.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
return 0;
}
void initialize(std::vector<int>& v)
{
auto distribution = std::uniform_int_distribution<int> {0, 100};
auto engine = std::mt19937 {1};
std::generate(v.begin(), v.end(), [&distribution, &engine]{ return distribution(engine); });
}