-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc.cpp
58 lines (47 loc) · 1.53 KB
/
c.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
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> numbers { 1, 2, 3, 4, 5, 10, 15, 20, 25, 35, 45, 50 };
cout << "\nall: ";
for_each(numbers.begin(), numbers.end(), print_n);
cout << "\nodd: ";
for_each(numbers.begin(), numbers.end(), print_if_odd);
cout << "\neven: ";
for_each(numbers.begin(), numbers.end(), print_if_even);
cout << "\nall: ";
for_each(numbers.begin(), numbers.end(), [](int n){
cout << n << ' ';
}
cout << "\nodd: ";
for_each(numbers.begin(), numbers.end(), [](int n) {
if (n % 2 == 1) cout << n << ' ';
});
cout << "\neven: ";
for_each(numbers.begin(), numbers.end(), [](int n) {
if (n % 2 == 0) cout << n << ' ';
});
srand(time(NULL));
auto divisor = rand()%4+2; // Random: {2,5}
cout << "\ndiv " << divisor << ": ";
for_each(numbers.begin(), numbers.end(), [divisor](int n) {
if (n % divisor == 0) cout << n << ' ';
});
auto is_divisible_by_n = [](int n, int divisor)->bool {
return (n % divisor == 0);
};
cout << "\ndiv A: ";
for_each(numbers.begin(), numbers.end(), [divisor, is_divisible_by_n](int n) {
if (is_divisible_by_n(n, divisor)) cout << n << ' ';
});
auto is_divisible = [divisor](int n)->bool {
return (n % divisor == 0);
};
cout << "\ndiv B: ";
for_each(numbers.begin(), numbers.end(), [is_divisible](int n) {
if (is_divisible(n)) cout << n << ' ';
});
cout << "\n\n";
}