-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathlect21-lambda.cpp
More file actions
70 lines (59 loc) · 1.52 KB
/
Copy pathlect21-lambda.cpp
File metadata and controls
70 lines (59 loc) · 1.52 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
64
65
66
67
68
69
70
// Copyright 2021 Casey A Cole
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
using std::vector;
using std::cout;
using std::cin;
using std::for_each;
// Structure of lambda
// [ capture clause ] (parameters) -> return-type
// {
// definition of method
// }
void myfunction(int i) {
cout << i << " ";
// i = i+1;
}
int main(int argc, char **argv) {
vector<int> v{2, 4, 6, 10, 2, 57};
// Printing
for (int i : v) {
cout << i << " ";
}
cout << "\n";
// for_each in algorithm
// https://www.cplusplus.com/reference/algorithm/for_each/
for_each(v.begin(), v.end(), myfunction);
cout << "\n";
// for (int i : v) {
// cout << i << " ";
// }
// cout << "\n";
// Printing with Lambda
for_each(v.begin(), v.end(), [](int & i) {
// std::cout << i << " ";
i = i+1;
});
cout << "\n";
for (int i : v) {
cout << i << " ";
}
cout << "\n";
// Another application... How would you go about
// finding the first element that is greater than a certain number?
vector<int>::iterator p = find_if(v.begin(), v.end(), [](int i) {
return i > 6 && i != 7;
// Add more logic?
});
cout << *p << "\n";
int count = count_if(v.begin(), v.end(), [](int i) {
return i > 6 && i != 7;
// Add more logic?
});
cout << count << "\n";
return 0;
}
// Learn more: https://docs.microsoft.com/en-us/cpp/cpp/lambda-expressions-in-cpp?view=msvc-170
// More examples: