zoukankan      html  css  js  c++  java
  • c++新特性---lambda表达式

    #include <algorithm>
    #include <iostream>
    #include <functional>
    #include <vector>
    #include <numeric>
    #include <array>
    #include <cstring>
    #include <cstdio>
    using namespace  std;
    //解决函数嵌套问题
    int main1()
    {
        //[]{cout << "hello lambda"; }();//匿名lambda表达式,()最后的括号起到调用作用
    
        auto fun = []{cout << "hello lambda"; };//函数指针
        fun();//调用
    
    }
    int main2()
    {
        //{} 函数体,执行
        //()参数列表,{}函数体,()调用
        //[](char* str){cout << str << endl; }("huahaua");
        auto fun = [](char* str){cout << str << endl; };
        fun("fangfang");
        cout << typeid(fun).name() << endl;//本质对类的封装
        cout << (void*)fun << endl;//lambda 不可以直接取地址,无法当做函数指针
    
    }
    int main3()
    {
        auto fun = [](double a, double b){return a + b; };
        cout << fun(10,19.1)<<endl;
        //->在(){}之间,指定返回值类型
        auto fun1 = [](double a, double b)->int{return a + b; };
        cout << fun1(10, 19.1) << endl;
        //内联展开,无法取出地址
    
    
        //->decltype(a+b)类型推理
        auto fun2 = [](double a, double b)->decltype(a+b) {return a + b; };
        cout << fun2(10, 19.1) << endl;
    
    }
    int main()
    {
    
        int num = 100;
        auto fun = [](int num){num = 5; cout << num << endl; };
        fun(num);//遵循副本机制,是复制
        cout << "main:" << num << endl;
    }

    例2:

    #include <algorithm>
    #include <iostream>
    #include <functional>
    #include <vector>
    #include <numeric>
    #include <array>
    #include <cstring>
    #include <cstdio>
    using namespace  std;
    //lambda表达式主要解决代码的内嵌,CPP支持lambda表达式
    //[]()multable->int{}();//匿名表达式
    //[] =引用,只能读,不能改 =mutable读原本改副本,&读写原本
    //()参数,int a,int b
    //{}语句实现,()调用
    //->指定返回值
    //c11不支持auto.c14支持
    int main1()
    {
        int num1 = 100, num2 = 99;
        //=只能读外部变量,不可以写
        //[=](){num1 = 20, num2 = 30; cout << num1 << num2 << endl; }();
        
        //& 可以读写外部变量
        [&](){num1 = 20, num2 = 30; cout << num1 << num2 << endl; }();    
        
        //=加mutable,把外部变量作为副本进行修改,不影响外部变量
        [=]()mutable{num1 = 20, num2 = 30; cout << num1 << num2 << endl; }();
        cout << "main:"<<num1 << "   " << num2 << endl;
        system("pause");
        
    }
    int main2()
    {
    
        int a = 10, b = 9, c = 8;
        //&a,可读可写,b,c只能读
        //[&a,b,c](){a=111,cout << a << b << c << endl; }();
        //a,b,c 只能读
        //[a, b, c](){a = 111, cout << a << b << c << endl; }()
        //mutable副本,能读写,读的是原本,写的是副本
        [a, b, c]()mutable{a = 111, b = 123, c = 321, cout << a << b << c << endl; }();
        //[=]只能指定全部
    
    }
    int main3()
    {
        //只适用于vs2015
        [](auto a, auto b){cout << a + b << endl; }(10,11);//c++14推理数据类型
        [](auto a, auto b){cout << a + b << endl; }(10.8, 11);
        [](auto a=0, auto b=0){cout << a + b << endl; }(10.8, 11);
        [](int a = 0, int b = 0){cout << a + b << endl; }(10.8, 11);
    
    }
    int main()
    {
        array<int, 10> myint{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };//cpp风格数值
        for_each(myint.begin(), myint.end(), [](int num){cout << num << " "; });//显示数值
        for_each(myint.begin(), myint.end(), [](int& num){num + 1, cout << num << " "; });//修改
        for_each(myint.begin(), myint.end(), [](int num){cout << num << " "; });//显示修改后的值
    }
  • 相关阅读:
    201275判断joomla首页的方法
    phpcms添加视频模块(未完)
    Joomla学习总结
    Joomla资源
    2012725 K2组件学习
    Apache Commons configuration使用入门
    linux学习(7)压缩与解压缩
    linux学习(6)redhat安装xwindow环境
    linux学习(5)iptables防火墙设置
    java实现的一个分页算法
  • 原文地址:https://www.cnblogs.com/bwbfight/p/11299100.html
Copyright © 2011-2022 走看看