-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy path10-lambda4.cpp
More file actions
33 lines (29 loc) · 728 Bytes
/
10-lambda4.cpp
File metadata and controls
33 lines (29 loc) · 728 Bytes
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
// 10-lambda4.cpp : lambda accessing scoped variables by value and reference
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector v{ 3, 5, 2, 6, 2, 4 };
int min, max, num{ 1 };
double avg;
bool first{ true };
auto l = [&](int i) {
if (first) {
min = max = avg = i;
first = false;
return;
}
if (i < min) {
min = i;
}
if (i > max) {
max = i;
}
avg = ((avg * num) + i) / (num + 1);
++num;
};
for_each(begin(v), end(v), l);
cout << "Min: " << min << " Max: " << max
<< " Avg: " << avg << " Num: " << num << '\n';
}