c++

105 阅读2分钟

lambda 表达式(匿名函数)

#include <iostream>

#include <string>

#include <vector>

#include <algorithm>//算法头文件

using namespace std;

//补充知识点 lambda 表达式(匿名函数) 一般会应用在函数实参需要一个函数指针的时候

void Func02()

{

int a=10,b=20,c=3;

[]()->int { cout << "这是一个lambda表达式" << endl; }; //lambda表达式

//捕获列表:捕获的是父作用域下的属性

[]{};//空 不捕获父作用域下的属性

[=]{ cout << a << endl; int number = 156; number = 9; };//[=] 以值传递的形式捕获父作用域下的属性:捕获到的父作用域下的属性为值,而不是变量

[=,&b] {cout << a << endl; int number = 156; number = 9;};//[=,&b] 以值传递的形式捕获父作用域下的所有属性,但是引用捕获b

[&]{cout << a << endl; int number = 156; number = 9;};//[&]引用捕获父作用域下的所有属性,捕获到的是一个引用,是别名,不是直接使用的父作用域下的

[&,c] {cout << c << endl; };

//参数 :

[](int a, int b) { cout << "函数体" << endl; };//不捕获父作用域下的属性,有两个int 类型参数,无返回值的函数

[](int a, bool b=true ) { cout << "函数体" << endl; };//可以接收默认参数

  


//返回类型 -> 不再是指针的 意思 ,是返回类型的意思

[]()->int {return 0;}; //1个无参数(有返回值 即使无参数,也不能省略()。 ) 有int 类型返回值的函数

[](int i)->int {return i;};

  


//因为 在创建函数的时候,创建的是匿名函数,但是,在函数调用的时候需要名称,所以,需要借助 auto 关键字 为匿名函数命名

auto lambda = [=] {cout << a << endl; };

lambda();//无参数的函数调用

cout << "~~~~~~~~~~~" << endl;

auto lam1 = [](int i = 9)->int { cout << "aa" << endl; return i;};

lam1(10);//调用了 一个带参数 和返回值 的匿名函数

cout << "~~~~~~~~~~~" << endl;

  


}

auto Function01 = ([]{

std::cout << "Hello World!" << std::endl;

}

);

int main() {

Func02();

Function01();

}

STL编程:容器 迭代器(遍历元素)

#include <iostream>

#include <string>

#include <vector>

#include <algorithm>//算法头文件

using namespace std;

  


//STL编程:容器 迭代器(遍历元素)

//容器: vector map set list string deque queue stack


void PrintInt(int value);

void Func01()

{

//大概需要存储 4 5 0 12 78 1 6 8 12 。。。。。。

//如果需要存储同类型的多个元素,需要选择容器:STL中的容器,可以是数组,可以是动态数组

//创建容器

vector<int> numbers; //创建了一个容器,名字是numbers,容器中元素的类型是int

//向容器中存放数据

numbers.push_back(4);//vector容器采用尾插法存放元素

numbers.push_back(5);

numbers.push_back(0);

numbers.push_back(12);

numbers.push_back(78);

numbers.push_back(1);

numbers.push_back(6);

numbers.push_back(8);

numbers.push_back(12);

//容器计数

cout << "容器中存放了:" << numbers.size() << endl;

  


//取出数据

//取出单个数据

cout << numbers[0] << endl;

cout << numbers.at(0) << endl;

//取出所有数据

//1. 传统for循环遍历(在实际开发中,对STL中的容器一般不使用这种方法)

for (unsigned int i = 0; i < numbers.size(); i++) {

cout << "第" << i+1 << "个元素:" << numbers.at(i) << endl;

}

  


//2. 借助STL编程中的迭代器迭代数据

// 在使用迭代器之前,需要确定使用的容器,并需要确定容器中元素的类型 ind

vector<int>::iterator itBegin = numbers.begin();//begin是一个函数,这个函数代表的是numbers容器中第一个元素的位置 pos

vector<int>::iterator itEnd = numbers.end();//end()是一个函数,最后一个元素的下一个位置

  


for (vector<int>::iterator itBegin = numbers.begin(); itBegin != numbers.end(); itBegin++) {

cout << *itBegin << ",";

}

cout << endl << "============" << endl;

  


//3.借助算法实现数据的迭代 需要algorithm头文件

for_each(numbers.begin(),numbers.end(),PrintInt);//这个函数自带三个参数,第一个参数是开始迭代的位置,第2个参数是结束迭代的位置(不包含)第3个参数是一个无返回值有一个参数的函数,用于打印输出语句

cout << endl << "================" << endl;

//4.


//排序

sort(numbers.begin(),numbers.end());

for_each(numbers.begin(),numbers.end(),PrintInt);//这个函数自带三个参数,第一个参数是开始迭代的位置,第2个参数是结束迭代的位置(不包含)第3个参数是一个无返回值有一个参数的函数,用于打印输出语句

cout << endl << "================" << endl;

}

int main() {
Func01();
}

void PrintInt(int value) {//这个函数已经被声明过了

cout << value << ",";

}