语法糖LambdaLamdbaLamdba

函数指针和对象函数

Lambda回调函数
调用方被调用方InvokeFunction被调用方调用方CallbackFunction
被调用方调用方
被调用模块调用模块std::sort被调用方调用模块
回调函数函数对象仿函数函数指针std::sortvectorStudentstd::sortStudentSortFunctionsort
#include <algorithm>
#include <iostream>
#include <vector>

struct Student
{
	std::string  m_strName;
	unsigned int m_uAge;
};

void PrintStudentVector(const std::vector<Student>& vecStudents)
{
	for (auto&& student : vecStudents)
	{
		std::cout << student.m_strName.c_str() << ":" << student.m_uAge << std::endl;
	}
	std::cout << std::endl;
}

bool StudentSortFunction(const Student& student1, const Student& student2)
{
	return student1.m_uAge < student2.m_uAge;
}

int main()
{
	std::vector<Student> vecStudents= {
		{"xiaoqiang", 15}, 
		{"xiaoming", 13},
		{"xiaoke", 13}
	};

	PrintStudentVector(vecStudents);

	std::sort(vecStudents.begin(), vecStudents.end(), StudentSortFunction);

	//Print after sort
	PrintStudentVector(vecStudents);
	return 0;
}

StudentSortbool operator() (const Student& student1, const Student& student2)
#include <algorithm>
#include <iostream>
#include <vector>

struct Student
{
	std::string  m_strName;
	unsigned int m_uAge;
};

void PrintStudentVector(const std::vector<Student>& vecStudents)
{
	for (auto&& student : vecStudents)
	{
		std::cout << student.m_strName.c_str() << ":" << student.m_uAge << std::endl;
	}
	std::cout << std::endl;
}

class StudentSort
{
public:
	bool operator() (const Student& student1, const Student& student2)
	{
		return student1.m_uAge < student2.m_uAge;
	}
};

int main()
{
	std::vector<Student> vecStudents= {
		{"xiaoqiang", 15}, 
		{"xiaoming", 13},
		{"xiaoke", 13}
	};

	PrintStudentVector(vecStudents);

	std::sort(vecStudents.begin(), vecStudents.end(), StudentSort());

	//Print after sort
	PrintStudentVector(vecStudents);
	return 0;
}
函数指针函数对象被调用方回调方法pCtx调用方
void CallbackFunction(Contex* pCtx, Parameter par1, Parameter par2.....)
函数指针函数对象函数指针函数对象CallbackContext callbackContext被调用方被调用方callbackContext(par1, par2)
class CallbackContex
{
public:
	bool operator() (Parameter par1, Parameter par2) { ; };
private:
	Contex* m_pCtx;
};
被调用class被调用方ContexLambda捕获变量lambda

Lambda

Lambda捕获列表
mutableconstmutable
Lambda
std::sort(vecStudents.begin(), vecStudents.end(), [](const Student& student1, const Student& student2) -> bool {
		return student1.m_uAge < student2.m_uAge;
	});
Lambdaautostd::functionLambda值捕获引用捕获
#include <iostream>

int main()
{
	unsigned int uYear = 2020;
	unsigned int uMonth = 9;

	std::cout << "uYear: " << uYear
		<< " Month: " << uMonth << std::endl << std::endl;

	auto lambda = [&uYear, uMonth]() -> bool {
		uYear = 2021;
		std::cout << "uYear: " << uYear
			<< " Month: " << uMonth << std::endl << std::endl;

		//error C3491: 'uMonth': a by copy capture cannot be modified in a non-mutable lambda
		//uMonth = 10;

		return true;
	};
	
	lambda();

	std::cout << "uYear: " << uYear
		<< " Month: " << uMonth << std::endl << std::endl;
	return 0;
}
Lambda引用捕获uYear值捕获uMonthLambda
uYearmainuYearuYear2021mainuYearuMonthmainuMonthLambdamutablemutablemainuMonth
LamdbaLambdaLambda
class LambdaClass_XXXXX
{
public:
	LambdaClass_XXXXX(unsigned int& uYear, unsigned int uMonth) :m_uYear(uYear), m_uMonth(uMonth) {}
	bool operator()() const
	{
		m_uYear = 2021;
		std::cout << "uYear: " << m_uYear
			<< " Month: " << m_uMonth << std::endl << std::endl;

		return true;
	}
private:
	unsigned int& m_uYear;
	unsigned int  m_uMonth;
};
LambdaClass_XXXXXLambda
[&, uMonth]uMonth值捕获[=, &uYear]uYear引用捕获值捕获
Stackoverflow

通过这一章节的内容,你是否能够举一反三了呢?出一道题目给读者做一做吧。

给读者的问题

Lambda
#include <iostream>

int main()
{
	int iVal = 100;
	auto lambda = [iVal]() mutable {
		iVal += 100;
		std::cout << iVal << std::endl;
	};
	lambda();
	lambda();
	return 0;
}

总结

LambdaC++Lambda捕获列表值捕获引用捕获