zoukankan      html  css  js  c++  java
  • 匿名函数和for_each用法

    匿名函数,C++11的 for_each 用法

    #include <iostream>
    #include <algorithm>
    #include "testClassA.h"
    
    using namespace std;
    
    
    void tiwce(int& elem)
    {
    elem = elem * 2;
    }
    // 不带参数,不带返回值
    // [](){cout << 123 << ","; };
    // []{cout << 123 << ","; };
    
    // 不带参数,带返回值
    // []()->(int){cout << 123 << ","; return 666; };
    
    
    // 带参数,带返回值的 
    //[](int elem)->(int){cout << elem << ","; return 666; };
    
    // 带参数,不带返回值, lamb 表达式
    // [](int elem){cout << elem << ","; };
    
     
    
     
    
    int main()
    {
    P px = { 77, 5 };
    int values[] {1, 2, 3, 4, 5, 6, 7, 8};
    
    //    for_each(&values[0], &values[5], tiwce);
    //    for_each(&values[0], &values[5], [](int& elem){elem = elem * 2; });
    //    for_each(&values[0], &values[5], [](int elem){cout << elem << ","; });
    //for (auto v : values)
    //{
    //    cout << v << ", ";
    //}
    
    int x = 0;
    int y = 42;
    
    
    cout << "1111111111" << endl;
    
    auto qqq = [x, &y]{
    std::cout << "X:" << x << endl;
    std::cout << "Y:" << y << endl;
    ++y;
    };
    
    cout << "22222222" << endl;
    x = y = 77;
    cout << x << endl;
    cout << "333333" << endl;
    
    qqq();
    qqq();
    cout << "Final:" << "X:" << x << ",Y:" << y << endl;
    
    cout << endl;
    cout << "C++11 standard" << endl;
    system("pause");
    return 0;
    }
  • 相关阅读:
    揺する、揺らす、揺さぶる区別
    test
    W5N1のW2D2
    微软企业库6的使用方法
    Delphi XE6 原生解析json
    delphi URL 编码的转换
    Go语言配置与开发环境配置
    Win7 IIS下启用ASP.NET
    Windows2003远程桌面单会话登录
    IIS7应用程序池集成和经典的区别
  • 原文地址:https://www.cnblogs.com/music-liang/p/12055641.html
Copyright © 2011-2022 走看看