首页 > 编程语言 >c++ lambda学习举例

c++ lambda学习举例

时间:2022-08-20 20:34:31浏览次数:107  
标签:std cout int c++ 举例 using include lambda

#include <iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include<ctime>
using std::cout;
using std::vector;
using std::srand;
using std::time;
using std::generate;
using std::endl;
using std::count_if;
using std::for_each;
using std::rand;
const long Size = 390000L;
int main()
{
    std::cout << "Hello World!\n";
    vector<int> numbers(Size);
    srand(time(0));
    generate(numbers.begin(), numbers.end(), rand);
    cout << "Sample Size=" << Size << endl;
    int m = 3;
    int count3 = count_if(numbers.begin(), numbers.end(), [m](int x) { return x % m == 0; });

    cout << "mode by 3==0's count=" << count3 << endl;
    int count13 = count_if(numbers.begin(), numbers.end(), [](int x)->bool {return x % 13 == 0; });
    cout << "mode by 13==0's count=" << count13 << endl;
    count3 = 0;
    count13 = 0;
    cout << "=====================\n";
    for_each(numbers.begin(), numbers.end(), [&count3, &count13](int x) {count3 += x % 3 == 0; count13 += x % 13 == 0;  });
    cout << "mode by 3==0's count=" << count3 << endl;
    cout << "mode by 13==0's count=" << count13 << endl;
    int* p1 = new int(0);
    int* p2 = new int(0);
    cout << "=====================\n";
    for_each(numbers.begin(), numbers.end(), [=](int x) {*p1 += x % 3 == 0; *p2 += x % 13 == 0;  });
    cout << "mode by 3==0's count=" << *p1 << endl;
    cout << "mode by 13==0's count=" << *p2 << endl;
    delete p1;
    delete p2;

    int* p3= new int[2];
    p3[0] = 0;
    p3[1] = 0;
    cout << "=====================\n";
    for_each(numbers.begin(), numbers.end(), [=](int x) {*p3+= x % 3 == 0; p3[1] += x % 13 == 0;  });
    cout << "mode by 3==0's count=" << *p3 << endl;
    cout << "mode by 13==0's count=" << p3[1] << endl;
    delete[]p3;

}
在lambda中 返回类型可以根据 函数体的返回值自动确定。 也可以[](int x)->bool这样明确指出来。 返回为void 可以不写。 [=],表示表达式内部可访问外部的所有动态变量,指针 new什么创建的变量。 [&count3],表示访问count3的引用,没有创建副本,这样可以给count3赋值。 int m=3; [m] (int x) { return x % m == 0; },这种不可以在表达式内部赋值,只在表达式里做只读变量。 lambda 省略了函数名用索引代替,入参有的话还是需要要写类型 比如 int x。 需要特定访问符号才能访问到表达式外部的变量。

标签:std,cout,int,c++,举例,using,include,lambda
From: https://www.cnblogs.com/HelloQLQ/p/16608529.html

相关文章

  • C++数据类型
    整型C++标准short:>=2字节int:>=shortlong:>=4字节,>=intlonglong:>=8字节,>=longlinux系统short:2字节int:4字节long:4字节longlong:8字节int被设置为计算机......
  • 深度学习 之 模型部署【4】-libtorch入门 - pytorch部署torchscript 以及 c++ libto
    pytorch部署torchscriptfromtorchvision.modelsimportresnet34importtorch.nn.functionalasFimporttorch.nnasnnimporttorchimportcv2#读取一张图片,......
  • Wireshark使用教程举例
    入门小站 入门小站 2022-07-0422:11 发表于#Linux485#wireshark3Wireshark是非常流行的网络封包分析软件,可以截取各种网络数据包,并显示数据包详细信息。常用于开发测......
  • Effective C++ - 条款3 - 关于const的一切
    const与*符号左侧为所指对象的语义,符号右侧为指针自身语义const与iteratorconstiterator(是T*const)!=const_iterator详细原因应参考c++编译和c++设计与演化const......
  • java8中的lambda表达式实用详解
    java8中的lambda表达式实用详解1.lambda简介​ Lambda表达式(lambdaexpression)是一个匿名函数,Lambda表达式基于数学中的λ演算得名,直接对应于其中的lambda抽象(lambda......
  • C++primer练习16.1-14
    练习16.1::实例化就是模板通过实际调用而确定类型及其运算,抽象到具体练习16.2template<typenameT>intcompare(constT&v1,constT&v2){if(v1<v2)return-1;......
  • 另类数学分析 - 从lambda演算谈起
    注:这篇文章主要是写给自己和lcw看的,可能会比较混乱和难以理解默认的概念:对象:数学上一切皆可以看做对象。朴素的相等关系(\(=\not=\)):一个对象始终等于其本身,不等于其......
  • C++primer练习15.15-33
    练习15.15重新定义Bulk_quoteclassDisc_quote:publicQuote{public:Disc_quote()=default;Disc_quote(conststd::string&book,doublep......
  • C++模板(函数模板 & 类模板)
    模板编程可称范型编程,是一种忽视数据类型的编程方式,这样的好处是什么?且看下面一个例子:简单使用求解最值问题,返回两个值中的较大值:intMax(inta,intb){ returna>......
  • C++ std::transform的使用
    头文件:<algorithm>作用:std::transform在指定的范围内应用于给定的操作,并将结果存储在指定的另一个范围内  //字符串操作std::stringstr1="HelloWorld";......