About anonymous function in C ++

Anonymous function: In computer programming refers to a class without having to define an identifier (function name) of the function or subroutine.

Anonymous function has the following characteristics:

  • It did not name (and therefore anonymous)
  • Defined inline
  • When you do not want to use the normal function of the cost / form
  • Unless passed as a parameter to another function, it would not explicitly reference many times
 1 //all_off example
 2 #include <iostream>
 3 #include <algorithm>
 4 #include <array>
 5 using namespace std;
 6 
 7 int main()
 8 {
 9     std::array<int, 8> foo = { 3, 5, 7, 11, 13, 17, 19, 23 };
10     if (all_of(foo.begin(), foo.end(), [](int i) {return i % 2; }))
11     {
12         std::cout << "All elements are odd number\n";
13     }
14     else 
15     {
16         std::cout << "All elements are not odd number\n";
17     }
18     return 0;
19 }

Described above can be equivalently as follows anonymous functions commonly used form:

1 bool is_odd(int i)
2 {
3     return i % 2;
4 }

 

Guess you like

Origin www.cnblogs.com/jeapwu/p/11414755.html