forked from zhmzhen/candcppCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimpleLambda.cpp
59 lines (53 loc) · 1.36 KB
/
simpleLambda.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
59
#include <iostream>
#include <algorithm>
#include <functional>
#include <string>
#include <vector>
#include <iostream>
using namespace std;
class Lambda
{
public:
void sayHello() {
std::cout << "Hello" << std::endl;
};
void lambda() {
auto function = (
// pass value of this
//std::cout << "lambda enter" << std::endl;
[this]{
std::cout << "lambda after this" << std::endl;
this->sayHello();
});
function();
}
};
int main() {
std::cout << "main: enter" << std::endl;
std::vector<int> data = {1,2,3,4,5};
//Define the lambda function, only define a iterater, the [](auto it){return it>3;}
//not be run
int cnt = std::count_if(
std::cbegin(data),
std::cend(data),
[] (auto it) {
return it>3;
}
);
//The [](auto it){return it>3;} is called per each item in data
std::cout << "count of data that is great than 3 is " << cnt << std::endl;
std::cout << "pass this value in lamda" << std::endl;
Lambda demo;
demo.lambda();
std::cout << "mutalble in lamda" << std::endl;
int m{0}, n{0};
auto mutable_f = [&, n] (int pI) mutable -> int {
m = ++n + pI;
return n;
};
n = mutable_f(4);
std::cout << "mutalble in lamda, m from 0 to 5? :" << m << std::endl;
std::cout << "mutalble in lamda, n from 0 to 1? :" << n << std::endl;
std::cout << "main: exit" << std::endl;
return 0;
}