-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLambdaFunctions.cpp
More file actions
54 lines (49 loc) · 1.2 KB
/
LambdaFunctions.cpp
File metadata and controls
54 lines (49 loc) · 1.2 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
// even_lambda.cpp
// compile with: cl /EHsc /nologo /W4 /MTd
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main()
{
// Create a vector object that contains 9 elements.
vector<int> v;
for (int i = 1; i <= 20; ++i) {
v.push_back(i);
}
// Count the number of even numbers in the vector by
// using the for_each function and a lambda.
int evenCount = 0;
for_each(v.begin(), v.end(), [&evenCount] (int n)
{
cout << n;
if (n % 2 == 0) {
cout << " is even " << endl;
++evenCount;
} else {
cout << " is odd " << endl;
}
}
);
// Print the count of even numbers to the console.
cout << "There are " << evenCount
<< " even numbers in the vector." << endl;
int factor = 5;
vector<int>::iterator ptr;
int Count = 0;
int i = 0;
for_each(v.begin(), v.end(), [&Count] (int n)
{
cout << n;
//cout << v[i++];
if (n % 2 == 0) {
cout << " is even " << endl;
++Count;
} else {
cout << " is odd " << endl;
}
}
);
}
auto upperBound = 42;
//[upperBound](int value) { return 0 < value && value < upperBound; }