Lambda 示例支持

最简单的lambda

#include <vector>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <deque>
using namespace std;
#include <conio.h>
int _tmain()
{
 vector<int> v;
 for (int i = 0; i < 10; ++i) {
  v.push_back(i);
 }
 for_each(v.begin(), v.end(), [](int n) { cout << n << " "; });
 cout << endl;
 getch();
 return 0;
}

----------------------------------------------------------------------------------------------

但返回参数的lambda

int _tmain()
{
 vector<int> v;
 for (int i = 0; i < 10; ++i) {
  v.push_back(i);
 }
 deque<int> d;
 transform(v.begin(), v.end(), front_inserter(d), [](int n) { return n * n * n; });
 for_each(d.begin(), d.end(), [](int n) { cout << n << " "; });
 cout<<endl;
 getch();
 return 0;

----------------------------------------------------------------------------------------------

指定返回值类型的lambda

 deque<double> d;
 transform(v.begin(), v.end(), front_inserter(d), [](int n) -> double{
  if (n % 2 == 0) {
   return n * n * n;
  } else {
   return n / 2.0;
  }
 });
 for_each(d.begin(), d.end(), [](double x) { cout << x << " "; });

----------------------------------------------------------------------------------------------

有有状 lambda,如果不这样做,编译器是要报错的,在lambda实现体内,不允许出现未

外部的程员变量,除非在[]中有所说明

着,lambda 表达式式地定了一个不具名函数合声明 { return x < n && n < y; } 中被当作函数用操作符的函数体。然从构上看合声明句是在 main() 之内,但在概念上它是在 main() 之外的,因此如果不传递capture)到 lambda 中去,就不能在其中使用来自main() 中的局部量。

int x = 4;
 int y = 7;
 v.erase(remove_if(v.begin(), v.end(), [x, y](int n) { return x < n && n < y; }), v.end());
 for_each(v.begin(), v.end(), [](int n) { cout << n << " "; });

----------------------------------------------------------------------------------------------

有状态的第二种表示方法,简单

你可以 “值传递capture)任何西,而不用特指明一个你想要传递(capture)的局部量。其法是使用这种形式的 lambda 引符 [=] (默认传递capture-default应该可以你想起赋值或者拷初始化 Foo foo = bar; 里的拷贝实际上是直接初始化(通初始化列表赋值),就像上面的 m_a(a))。

 int x = 4;
 int y = 7;
 v.erase(remove_if(v.begin(), v.end(), [=](int n) { return x < n && n < y; }), v.end());
 for_each(v.begin(), v.end(), [](int n) { cout << n << " "; });

----------------------------------------------------------------------------------------------

只能修改r值(用了mutable)

 int x = 1;
 int y = 1;
 for_each(v.begin(), v.end(), [=](int& r) mutable {
  const int old = r;
  r *= x * y;
  x = y;
  y = old;
 });
 for_each(v.begin(), v.end(), [](int n) { cout << n << " "; });
 cout<<endl;
 cout << x << ", " << y << endl;

----------------------------------------------------------------------------------------------

可以修改r , x, y([&x, &y](int& r))

int x = 1;
 int y = 1;
 for_each(v.begin(), v.end(), [&x, &y](int& r) {
  const int old = r;
  r *= x * y;
  x = y;
  y = old;
 });
 for_each(v.begin(), v.end(), [](int n) { cout << n << " "; });
 cout << endl;
 cout << x << ", " << y << endl;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值