Lambda expressions in C ++

  1. A simple Lambda expression is as follows:
  
  [] {};
  
  This defines an object, this object is anonymous, remember, Lambda expression is an object, not a type, this is very important! In this example, the type of the object is 'anonymous-namespace' :: <lambda0>, which is a type name given to it by the compiler.
  
  2. Generally, when a compiler implements a Lambda expression, it converts it into a function object (functor). For example, the above example will be transformed into
  
  struct A {
  
  public:
  
  void operator () () const {}
  
  };
  
  A ();
  
  where A () is a function object and A is a type, these should be clearly distinguished.
  
  3. Functors and Lambda expressions can be stateful, which means that they can contain data members, and function pointers are stateless. For example, in the following cases, function pointers cannot be used, only functors or Lambda expression.
  
  / ************************************************* *******************
  
  created: 2014/05/07 22:31
  
  filename: main.cpp
  
  author: Justme0 (http://blog.csdn.net/justme0)
  
  purpose: When it comes to state, you can only use functors or Lambda expressions, not function pointers
  
  *********************************************************************/
  
  #include <iostream>
  
  #include <vector>
  
  #include <algorithm>
  
  using namespace std;
  
  /*
  
  ** 输出 vec 中大于 standard 的元素
  
  */
  
  void print_use_lambda(const vector<int> &vec, int standard) {
  
  for_each(vec.begin(), vec.end(), [standard](int elem) {
  
  if (standard < elem) {
  
  cout << elem << ‘ ‘;
  
  }
  
  });
  
  }
  
  struct Printor {
  
  private:
  
  int m_standard;
  
  public:
  
  Printor(int standard) : m_standard(standard) {}
  
  void operator()(int elem) const {
  
  if (m_standard < elem) {
  
  cout <<item << '';
  
  }
  
  }
  
  };
  
  / *
  
  ** Output elements greater than standard in vec
  
  * /
  
  void print_use_functor (const vector <int> & vec, int standard) {
  
  for_each (vec.begin (), vec.end (), Printor (standard)) ;
  
  }
  
  int main (int argc, char ** argv) {
  
  int arr [] = {5, 2, 1, 4, 5, 6, 7, 2};
  
  int size = sizeof arr / sizeof * arr;
  
  vector <int > vec (arr, arr + size);
  
  cout << "need to output more than what number?" << endl;
  
  int standard;
  
  cin >> standard;
  
  print_use_functor (vec, standard);
  
  cout << endl;
  
  print_use_lambda (vec, standard);
  
  cout << endl;
  
  system ("PAUSE");
  
  return 0;
  
  }
  
  When calling for_each in the program, only output elements greater than a certain number, and this number is not known in advance, this number represents the state of the functor, and if you use a function pointer, you can not get this number (state), pay attention to the third of for_each The parameters are unary.
  
  4. Passing the capture list by value is different from passing the list by reference. Take the following program as an example:
  
  / ************************** ******************************************
  
  created: 2014/05/06 21 : 01
  
  filename: main2.cpp
  
  author: Justme0 (http://blog.csdn.net/justme0)
  
  purpose: Lambda's capture list
  
  ********************* ************************************************ /
  
  # include <iostream>
  
  using namespace std;
  
  int main (int argc, char ** argv) {
  
  int i = 0;
  
  auto by_val_lambda = [i] {
  
  return i;
  
  };
  
  auto by_ref_lambda = [& i] {
  
  return i;
  
  };
  
  cout << by_val_lambda () << endl; // 0
  
  cout << by_ref_lambda () << endl; // 0
  
  ++ i;
  
  cout << by_val_lambda () << endl; // 0
  
  cout << by_ref_lambda () << endl; // 1
  
  system ("PAUSE");
  
  return 0;
  
  }
  
  To explain this problem, turn them into functors (compiler implementation) at a glance:
  
  / ************************ ************************************************
  
  created: 2014/05 / 06 20:39
  
  filename: main.cpp
  
  author: Justme0 (http://blog.csdn.net/justme0)
  
  purpose: use Functor instead of Lambda
  
  ******************* ************************************************** /
  
  #include <iostream>
  
  using namespace std;
  
  struct Value {
  
  private:
  
  int m_i;
  
  public:
  
  Value (int i):m_i(i) {}
  
  int operator()() const {
  
  return m_i;
  
  }
  
  };
  
  struct Reference {
  
  private:
  
  int &m_i;
  
  public:
  
  Reference(int &i) : m_i(i) {} // 必须用初始化列表
  
  int operator()() const {
  
  return m_i;
  
  }
  
  };
  
  struct Pointer {
  
  private:
  
  int *m_pi;
  
  public:
  
  Pointer(int *pi) : m_pi(pi) {}
  
  int operator()() const {
  
  return *m_pi;
  
  }
  
  };
  
  int main(int argc, char **argv) {
  
  int i = 0;
  
  auto by_val_functor = Value(i);
  
  auto by_ref_functor = Reference(i);
  
  auto by_ptr_functor = Pointer(&i);
  
  cout << by_val_functor () << endl; // 0
  
  cout << by_ref_functor () << endl; // 0
  
  cout << by_ptr_functor () << endl; // 0
  
  ++ i;
  
  cout << by_val_functor () < <endl; // 0
  
  cout << by_ref_functor () << endl; // 1
  
  cout << by_ptr_functor () << endl; // 1
  
  system ("PAUSE");
  
  return 0;
  
  }
  
  Lambda saved one when the value was passed The copy has nothing to do with the formal parameters. When the reference is passed, Lambda saves the reference of the formal parameter, so when the formal parameter ++ i is changed later, the i pointed to by the reference saved in Lambda changes (the same i). Note that the pointer is similar to the reference. Use the pointer to explain the reference It could not be better.
  
  5. The Lambda capture list can only capture automatic variables in the parent scope.
  
  The standard is defined in this way, but some compilers do not comply, and the author has doubts about the specific rules. But in general, Lambda's idea is limited to a local scope, if you need global sharing, use function pointers (stateless) or functors (stateful).

Guess you like

Origin www.cnblogs.com/aquariusunny/p/12729793.html