-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda.cpp
31 lines (25 loc) · 957 Bytes
/
lambda.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
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
//using namespace std;
/*The purpose of this code is to print each element of a vector using function pointer
and a lambda
*/
//void(*func)(int) defines the return type, function pointer and the input type to the function
void vecfunc(const std::vector<int>& values, const std::function<void(int)>& func)
{
for (int value : values)
func(value); //calling the function
}
int main()
{
int lamcheck = 6;
std::vector<int> values = {5,6,8,9,22}; //define the vector to be printed
auto greater = std::find_if(values.begin(),values.end(), [](int value) {return value > 7;}); //Finds elements greater than 7 in the vector values
std::cout << *greater << std::endl;
auto lambdafunc = [=](int value) {std::cout<<"Value of lambda check is: "<< lamcheck << std::endl;};
vecfunc(values,lambdafunc);
//defining a lambda for printing the vector values
std::cin.get();
}