/* * 注意:Lambdas表达式隐式定义并构建不具名函数对象 */ ///////////////////////////////////////////////////////////////////////////////////////////////////////// /* * []操作符为lambda引导符,(int n)为lambda参数声明 * 不具名函数对象类的函数调用操作符默认返回void * {}说明函数体是复合语句 * 由以下代码可以看出lambda表达式的效果和函数对象一样 */ // 实例代码1: #include <algorithm> #include <iostream> #include <vector> using namespace std; struct LambdaFunctor { void operator() (int n) { cout << n << " "; } }; int main() { 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; for_each (v.begin(), v.end(), LambdaFunctor()); cout << endl; return 0; } ///////////////////////////////////////////////////////////////////////////////////////////////////////// /* * lambda表达式中若有{return expression;} * 则lambda的返回类型就会自动被推断成expression的类型 * 但是过于复杂的声明语句lambda函数不会自动推断返回类型 * 必须显示指定返回类型(实质是函数体中只有一个返回语句时可推断) * * 注意:-> type 指定返回值类型,必须置于()参数表后 */ // 实例代码2: #include <algorithm> #include <iostream> #include <vector> using namespace std; template <typename func> void test(func a) { if (a(1, 0)) { cout << 1 << endl; } else { cout << 0 << endl; } } int main() { test([] (int a, int b) { return a > b; }); return 0; } // 实例代码3: #include <algorithm> #include <iostream> #include <vector> using namespace std; template <typename func> void test(func a) { cout << a(1, 0) << endl; } int main() { test([] (int a, int b) -> int { if (a % 2 == 0) { return a * a * a; } else { return a / 2; } }); return 0; } ///////////////////////////////////////////////////////////////////////////////////////////////////////// /* * 通常空[]表示lambda是无状态的,即不包含数据成员 * 在lambda引导符[]中可以指定capture-list * 函数对象和lambda效果一样,函数对象存储了局部变量的拷贝 * 所以本质上“按值”传递对lambda而言就是隐式创建韩局部变量函数对象 * 注意点: * (a)在lambda中不能修改通过传递获得的拷贝,默认情况下函数调用操作符是const属性 * (b)有些对象拷贝开销大 * (c)局部变量的更新不会反应到通过传递获得的拷贝 */ // 实例代码4: #include <algorithm> #include <iostream> #include <vector> using namespace std; // 创建含局部变量的函数对象 struct LambdaFunctor { LambdaFunctor(int a, int b) : m_a(a), m_b(b) {} bool operator() () const { if (m_a % 2 == 0) { return m_a * m_a * m_a; } else { return m_a / 2; } } private: int m_a, m_b; }; template <typename func> void test(func a) { cout << a() << endl; } int main() { int a = 1, b = 0; test([a, b] () -> int { if (a % 2 == 0) { return a * a * a; } else { return a / 2; } }); test(LambdaFunctor(a, b)); return 0; } ///////////////////////////////////////////////////////////////////////////////////////////////////////// /* * 也可以利用[=]形式lambda引导符“按值传递任何东西” * 利用mutable将函数调用操作符转为non-const属性 */ // 实例代码5: #include <algorithm> #include <iostream> #include <vector> using namespace std; template <typename func> void test(func a) { cout << a() << endl; } int main() { int a = 1, b = 0; test([=] () mutable -> int { if (a % 2 == 0) { a = 2; return a * a * a; } else { a = 4; return a / 2; } }); return 0; } ///////////////////////////////////////////////////////////////////////////////////////////////////////// /* * 为了避免拷贝的开销出现通过引用传递 * 语法形式[&x, &y],隐式构建局部变量为引用变量 * 也可以用[&]来表示“按引用传递任何东西” * 可以不用mutable改变属性 * 可以在引导符[]进行混合使用传递,例如:[&,x,y],[x,&a,y,&b] */ // 实例代码6: #include <algorithm> #include <iostream> #include <vector> using namespace std; template <typename func> void test(func a) { cout << a() << endl; } int main() { int a = 1; test([&a] () -> int { if (a % 2 == 0) { a = 2; return a * a * a; } else { a = 4; return a / 2; } }); cout << a << endl; return 0; } ///////////////////////////////////////////////////////////////////////////////////////////////////////// /* * 注意有一种特别传参用法this * 但无法获得一个lambda对象他自身的this指针 * [=],[&]可以隐式传递this,[&this]不可以使用 */ ///////////////////////////////////////////////////////////////////////////////////////////////////////// /* * 当lambda不带参数时可以不加()但是同样也不能显示指定返回值类型 * 但是可以隐式推断返回值类型 * 定义lambda表达式后可以立即用()操作符使用表达式 * 可以用#define来简化定义一个lambda表达式 * 但是不可以用typedef来定义一个类型 */ // 实例代码7: #include <algorithm> #include <iostream> #include <vector> using namespace std; template <typename func> void test(func a) { cout << a() << endl; } int main() { int a = 1; test([] { return 9; }); return 0; } // 实例代码8: #include <algorithm> #include <iostream> #include <vector> using namespace std; template <typename func> void test(func a) { cout << a() << endl; } #define mengxm [] () { cout << "mengxm" << endl; } int main() { mengxm(); return 0; }
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。