-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda.cpp
More file actions
52 lines (41 loc) · 1.1 KB
/
lambda.cpp
File metadata and controls
52 lines (41 loc) · 1.1 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
//
// main.cpp
// lambdaa
//
// Created by CodeBreaker on 31/05/25.
//
#include <iostream>
using namespace std;
//Function pointer appraoch
int isEven(int n)
{
return n%2==0;
}
//Functor approach or function object
struct isEvenFunctor{
bool operator()(int n)
{
return n%2==0;
}
};
void take_func_ptr(void (*func)(int)) {
func(100);
}
int main(int argc, const char * argv[]) {
// insert code here...
std::cout << "Hello, Lambda Understanding!\n";
vector<int> numbers{1,2,3,4,5,6,7,8};
//auto it = find_if(numbers.begin(), numbers.end(), isEven); //Function pointer
//auto it = find_if(numbers.begin(), numbers.end(), isEvenFunctor()); //FUnctor
//Lambda
auto it = find_if(numbers.begin(), numbers.end(), [](int n)->bool{
return n%2==0;
});
cout<<"first even number : "<<*it<<endl;
//printing something
for_each(numbers.begin(), numbers.end(), [](int n){
cout<<n<<" "<<endl;
});
take_func_ptr([](int x) { std::cout << "Stateless lambda called with: " << x << std::endl; });
return 0;
}