• 匿名函数和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;
    }
  • 相关阅读:
    jstl与el学习笔记
    Subversion 安装笔记
    某公司面试
    字符集与字符编码的一些小常识,以及java web中文乱码的一些solution
    分治算法与合并排序示例
    C/C++ 笔试,难倒我哉
    HTML meat作用
    VIM配置DBGp调试PHP程序
    更新系统引导项
    PHP技术讨论群
  • 原文地址:https://www.cnblogs.com/music-liang/p/12055641.html
Copyright © 2020-2023  润新知