回调函数之

回调函数就是一个被作为参数传递的函数。在C语言中,回调函数只能使用[函数指针],在C++中可以使用使用[仿函数]或[匿名函数]
代码块:

int sz = 5;
std::vector<int>nums{ 5,3,8,6,9,1,4,7,2 };
auto it2 = find_if(nums.begin(), nums.end(), [&](int i)->bool{return i > sz; });
std::cout << *it2 << std::endl; //结果为8
class biggerthan //用于仿函数,使一个类看上去像个函数
{
public:
	biggerthan(int i) :x(i) {}
	bool operator()(int n) {
		return n > x;
	}
private:
	int x;
};
//
int sz = 5;
std::vector<int>nums{ 5,3,8,6,9,1,4,7,2 };
//仿函数重载()运算符
auto it1 = find_if(nums.begin(), nums.end(), biggerthan(sz));
std::cout << *it1 << std::endl; //结果为8

想到再补充~

你可能感兴趣的:(c++,开发语言)