+ 12
How to create a definition for a function with a lambda as an argument, using it in the body of the function?
I want to make a function to fill values in an array according to a condition, that can be specified by a lambda... [&](int n)->int{return n++;} //Something like this... Now, how shall the function fill be defined, so that it can input the lambda as well as alter the values of the array? I am unable to create such a function's definition, so please help! I could make a similar function which used pointer to a function, but I now want to use lambdas... Also, can a lambda be generic? //use template<>?
3 Respostas
+ 11
@Glozi30
I actually want to know how can I create a function that works like erase()...
As my need is of a function to use a lambda...
Please tell me how to make such a function?
+ 11
Its done, finally:
Thanks @Glozi30 !
template<typename FX>
void Fill(FX f)
{ for(int i=0;i<5;i++)
f(i);
}
//main:
auto f=[](int i){ cout<<i*i<<endl;}
Fill(f);
+ 6
Interesting subject, after 2 minutes on cppreference i got this :
#include <vector>
#include <iostream>
#include <algorithm>
#include <functional>
int main()
{
std::vector<int> c { 1,2,3,4,5,6,7 };
int x = 5;
c.erase(std::remove_if(c.begin(), c.end(), [x](int n) { return n < x; } ), c.end());
std::cout << "c: ";
for (auto i: c)
{
std::cout << i << ' ';
}
std::cout << '\n';
std::function<int (int)> func = [](int i) {return i+4;};
std::cout << "func: " << func(6) << '\n';
}