-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path04.cpp
More file actions
51 lines (41 loc) · 1.29 KB
/
04.cpp
File metadata and controls
51 lines (41 loc) · 1.29 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
#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(), [](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";
}