c+ - Qu'est-ce qu'une expression de lambda en C++11

Mots clés : c++lambdac++11c++-faqc++

meilleur 5 Réponses c+ - Qu'est-ce qu'une expression de lambda en C++11

vote vote

93

#include <algorithm> #include <vector>  namespace {   struct f {     void operator()(int) {       // do something     }   }; }  void func(std::vector<int>& v) {   f f;   std::for_each(v.begin(), v.end(), f); } 
void func2(std::vector<int>& v) {   struct {     void operator()(int) {        // do something     }   } f;   std::for_each(v.begin(), v.end(), f); } 
void func3(std::vector<int>& v) {   std::for_each(v.begin(), v.end(), [](int) { /* do something here*/ }); } 
void func4(std::vector<double>& v) {   std::transform(v.begin(), v.end(), v.begin(),                  [](double d) { return d < 0.00001 ? 0 : d; }                  ); } 
void func4(std::vector<double>& v) {     std::transform(v.begin(), v.end(), v.begin(),         [](double d) {             if (d < 0.0001) {                 return 0;             } else {                 return d;             }         }); } 
void func4(std::vector<double>& v) {     std::transform(v.begin(), v.end(), v.begin(),         [](double d) -> double {             if (d < 0.0001) {                 return 0;             } else {                 return d;             }         }); } 
void func5(std::vector<double>& v, const double& epsilon) {     std::transform(v.begin(), v.end(), v.begin(),         [epsilon](double d) -> double {             if (d < epsilon) {                 return 0;             } else {                 return d;             }         }); } 
vote vote

86

[]() { } // barebone lambda 
[]() mutable -> T { } // T is the return type, still lacking throw() 
int x = 4; auto y = [&r = x, x = x+1]()->int {             r += 2;             return x+2;          }();  // Updates ::x to 6, and initializes y to 7. 
auto ptr = std::make_unique<int>(10); // See below for std::make_unique auto lambda = [ptr = std::move(ptr)] {return *ptr;}; 
auto lambda = [](auto x, auto y) {return x + y;}; 
vote vote

70

[&](){ ...your code... }(); // immediately executed lambda expression 
{ ...your code... } // simple code block 
int a = []( int b ){ int r=1; while (b>0) r*=b--; return r; }(5); // 5! 
[&]( std::function<void()> algorithm ) // wrapper section    {    ...your wrapper code...    algorithm();    ...your wrapper code...    } ([&]() // algorithm section    {    ...your algorithm code...    }); 
auto algorithm = [&]( double x, double m, double b ) -> double    {    return m*x+b;    };  int a=algorithm(1,2,3), b=algorithm(4,5,6); 
vote vote

66

auto x = [=](int arg1){printf("%i", arg1); }; void(*f)(int) = x; f(1); x(1); 
[captureVar1,captureVar2](int arg1){} 
   [&captureVar1,&captureVar2](int arg1){} 
  [=](int arg1){} // capture all not-static vars by value    [&](int arg1){} // capture all not-static vars by reference 
[=,&Param2](int arg1){}  
[&,Param2](int arg1){}  
[=](int arg1)->trailing_return_type{return trailing_return_type();} 
auto toFloat = [](int value) { return float(value);}; auto interpolate = [min = toFloat(0), max = toFloat(255)](int value)->float { return (value - min) / (max - min);}; 
vote vote

57

void apply(void (*f)(int)) {     f(10);     f(20);     f(30); } 
int col=0; void output() {     apply([](int data) {         cout << data << ((++col % 10) ? ' ' : '\n');     }); } 
void output(int n) {     int col=0;     apply([&col,n](int data) {         cout << data << ((++col % 10) ? ' ' : '\n');     }); } 
#include <functional>  
#include <functional> void apply(std::function<void(int)> f) {     f(10);     f(20);     f(30); } void output(int width) {     int col;     apply([width,&col](int data) {         cout << data << ((++col % width) ? ' ' : '\n');     }); } 

Questions similaires