C++ features by examples
Collaboration diagram for Lambda:

Functions

static void lambda_basics (void)
 
static void lambda_capture (void)
 
void container_11 ()
 container More...
 
void algorithm_11 ()
 algorithm More...
 
void sort_11 ()
 Compare with sort_03. More...
 

Detailed Description

https://en.cppreference.com/w/cpp/language/lambda https://www.geeksforgeeks.org/lambda-expression-in-c/

Function Documentation

◆ algorithm_11()

void algorithm_11 ( )

algorithm

Definition at line 478 of file 11.cpp.

479{
480 vector<int> v = {1, 2, 3};
481 assert(find(begin(v), end(v), 0) == end(v));
482 assert(find(begin(v), end(v), 1) != end(v));
483}
Here is the caller graph for this function:

◆ container_11()

void container_11 ( )

container

forward_list

Definition at line 444 of file 11.cpp.

445{
446 // [list_initialization](https://en.cppreference.com/w/cpp/language/list_initialization)
447 vector<int> v = {1, 2, 3};
448 assert(v.data()[2] == 3);
449
450 v.shrink_to_fit();
451
452 v.emplace(v.cbegin(), 0);
453 assert(v.front() == 0);
454
455 v.emplace_back(4);
456 assert(v.back() == 4);
457
458 array<int, 1> a1, a2;
459 swap(a1, a2);
460
462 forward_list<int> fl;
463
464 fl.push_front(1);
465 assert(fl.front() == 1);
466 fl.emplace_front(2);
467 assert(fl.front() == 2);
468 fl.insert_after(fl.cbegin(), 3);
469 assert(fl.front() + 1 == 3);
470 assert(fl.front() == 2);
471 fl.erase_after(fl.cbefore_begin()); // like fl.pop_front();
472 assert(fl.front() == 3);
473 fl.pop_front();
474}
Here is the caller graph for this function:

◆ lambda_basics()

static void lambda_basics ( void  )
static

Definition at line 350 of file 11.cpp.

351{
352 auto annotated_named_lambda_expression = // optional name
353 [ ] // capture clause
354 ( ) // optional list of arguments
355 { }; // body
356
357 // Primitive named lambdas are just like closure functions:
358 // https://en.wikipedia.org/wiki/Closure_(computer_programming)
359
360 // declaration like a function:
361 // void closure() { };
362 auto closure = [] { };
363
364 closure();
365
366 // with arguments
367 auto pass = [] (int a) { return a; };
368 assert(pass(5) == 5);
369
370 // lambda captures external value
371 int c = 1;
372 auto get_i = [=] () { return c; };
373 assert(get_i() == 1);
374
375 // lambda captures external variable by reference
376 // with omitted arguments and return type
377 auto inc_get = [&] { return ++c; };
378 assert(inc_get() == 2);
379 assert(inc_get() == 3);
380
381 // annotated expanded empty inline lambda call:
382 [ ] // capture
383 ( ) // optional list of arguments
384 -> void // optional return value
385 { } // body
386 ( ); // call with arguments
387
388 // annotated expanded sample inline lambda call:
389 c = // result
390 [c] // capture
391 (int a) // an argument
392 -> int // return value
393 { return c + a; } // body
394 (1); // call with argument
395 assert(c == 4);
396
397 // inline lambda which is called in place
398 // https://en.wikipedia.org/wiki/Anonymous_function
399
400 // assert((1 + 1) == 2);
401 assert([](int a) { return a + 1; }(1) == 2);
402
403 // Actually calling lambda inline is useless
404 // and is provided only for demonstration.
405}
Here is the caller graph for this function:

◆ lambda_capture()

static void lambda_capture ( void  )
static

Definition at line 411 of file 11.cpp.

412{
413 // read only
414 int i = 2;
415 assert([=]{return i; }() == 2);
416
417 // read and write access
418 [&](int a){i = a;}(3);
419 assert(i == 3);
420
421 // explicit r/o and r/w
422 int j;
423 [i, &j](){ j = i; }();
424 assert(j == i);
425
426 // r/o by default
427 i++;
428 [=, &j](){ j = i; }();
429 assert(j == i);
430
431 // r/w by default
432 i++;
433 [&, i](){ j = i; }();
434 assert(j == i);
435
436 // can access globals anyway
437 auto inc_global = [] () { return ++glob; };
438 assert(inc_global() == 1);
439 assert(inc_global() == 2);
440}
Here is the caller graph for this function:

◆ sort_11()

void sort_11 ( )

Compare with sort_03.

array

Definition at line 487 of file 11.cpp.

488{
490
491 array<int, 10> s {5, 7, 4, 2, 8, 6, 1, 9, 0, 3};
492 sort(s.begin(), s.end(),
493 // sort using a lambda expression
494 [](int a, int b)
495 { return a > b; }
496 );
497}
Here is the caller graph for this function: