-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathstl_14_reduce_accumulate.cpp
55 lines (42 loc) · 1.18 KB
/
stl_14_reduce_accumulate.cpp
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
// code_report
// Video Link: https://youtu.be/Mcjrc2uxbKI
#include <vector>
#include <numeric>
#include <algorithm>
using namespace std;
namespace my {
template<class I, class T>
auto count(I f, I l, const T& val) {
return std::reduce(f, l, 0,
[val](auto a, auto b) { return a + (b == val); });
}
template<class I, class P>
auto any_of(I f, I l, P p) {
return std::reduce(f, l, false,
[p](auto a, auto b) { return a || p(b); });
}
}
void example1() {
vector v = { 1, 2, 3, 4 };
auto x = accumulate(begin(v), end(v), 0);
auto y = accumulate(begin(v), end(v), 1, multiplies<>());
}
void example2() {
vector v = { 1, 2, 3, 4 };
auto x = reduce(begin(v), end(v));
auto y = reduce(begin(v), end(v), 0, plus<>());
auto z = reduce(begin(v), end(v), 1, multiplies<>());
}
void example3() {
vector v = { 1, 2, 3, 1, 2 };
auto x = my::count(begin(v), end(v), 1);
auto y = std::count(begin(v), end(v), 1);
}
void example4() {
vector v = { 1, 2, 3 };
auto x = my::any_of(begin(v), end(v), [](auto e) { return e == 3; });
auto y = std::any_of(begin(v), end(v), [](auto e) { return e == 3; });
}
int main() {
return 0;
}