c++11新性能测试4

来源:互联网 发布:淘宝上卖零食怎么样 编辑:程序博客网 时间:2024/05/23 22:00
#include<iostream>#include<vector>#include <algorithm>using namespace std;vector<vector<int>> func(){    vector<vector<int>> vv;    int a[] = {1,2,3,4,5};    int b[] = {6,2,3,4,5};    int c[] = {5,2,2,4,5};    vv.push_back(vector<int>(a, a+5));    vv.push_back(vector<int>(b, b+5));    vv.push_back(vector<int>(c, c+5));    return vv;}void print(vector<vector<int>> &vv){    for(auto &v : vv)    {        for_each(v.begin(), v.end(), [](int val){cout << val << " ";});        cout << endl;    }}void test(){    [](){cout <<" This is a lambda test!" << endl;}();}void lambda_test1(){    int x=0, y=0;    [&](){++x; ++y;}();    cout << "x = " << x << " y=" << y << endl;}void lambda_test2(){    int m=0, n=0;    [=](){        //++m; ++n; build error: increment of read-only variable 'm,n'        //按值捕获,这些变量在lampda内部是只读的        //其它捕获方式,[a,&], [&a, =]        cout << "m= " << m <<" n=" << n << endl;    }();    cout << "m= " << m <<" n=" << n << endl;}int main(){    auto &&vv = func();    print(vv);    test();    lambda_test1();    lambda_test2();    return 0;}

0 0
原创粉丝点击