Lambda expression in C++

Level 3103 keystrokes

Stores a lambda (an unnamed function) that adds two ints in the variable add, then calls it like a normal function and prints 8 and 30.

C++ code

// [] is the (empty) capture list; auto is needed because each lambda has its own unnamed type
1auto add = [](int a, int b) {
// The return type int is deduced from this return
2 return a + b;
3};
4
// Calls the lambda like a function: prints 8
5cout << add(3, 5) << endl;
// Prints 30
6cout << add(10, 20) << endl;

Output

8
30
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this C++ code and how often they appear.

  • <× 8
  • ;× 4
  • ,× 3
  • (× 3
  • )× 3
  • [× 1
  • ]× 1
  • {× 1
  • }× 1
  • +× 1

More C++ examples at level 3

← C++ typing practice