forked from teseoch/CPP-Fall-2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path109-lambda1.cpp
64 lines (53 loc) · 1.67 KB
/
109-lambda1.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
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <vector>
#include <functional>
bool all_of(std::vector<int> const &V, std::function<bool(int)> condition)
{
for (auto x : V)
if (!condition(x))
return false;
return true;
}
int main()
{
std::vector<int> V0{0, 0, 0, 0};
std::vector<int> V1{0, 2, 4, 6, 8, 10};
std::vector<int> V2{-2, -4, 0, 6, 10};
std::vector<int> V3{6, 10, 17, 187};
// auto is_even = [](int x) {
// return x % 2 == 0;
// };
// auto is_even = 3.4;
auto is_even{[](int x) -> bool {
return x % 2 == 0;
}};
// auto is_even { 3.4 };
// auto is_even { "Asdasd" };
// auto is_even { [](...){...} };
auto is_positive{[](int x) -> bool {
return x > 0;
}};
std::cout << "V0: is_even = " << all_of(V0, is_even) << std::endl;
std::cout << "V1: is_even = " << all_of(V1, is_even) << std::endl;
std::cout << "V2: is_even = " << all_of(V2, is_even) << std::endl;
std::cout << "V3: is_even = " << all_of(V3, is_even) << std::endl;
std::cout << "V0: is_positive = " << all_of(V0, is_positive) << std::endl;
std::cout << "V1: is_positive = " << all_of(V1, is_positive) << std::endl;
std::cout << "V2: is_positive = " << all_of(V2, is_positive) << std::endl;
std::cout << "V3: is_positive = " << all_of(V3, is_positive) << std::endl;
auto order{[](int x, int y) {
return x < y;
}};
std::sort(V1.begin(), V1.end(), order);
//int x{5}; foo(x);
// std::sort(V1.begin(), V1.end(), [](int x, int y) {
// return x < y;
// });
//foo(5);
for (auto v : V1)
{
std::cout << v << " ";
}
std::cout << std::endl;
return 0;
}