Cpp Note3: 函数指针,函数对象,Lambda
Function Pointer
int func(int a, int b) { return a + b; } int main() { // 定义function pointer int (*f)(int,int) = func; // 使用 function pointer int res = f(1, 2); // res == 3 return 0; }
int func(int a, int b) {
return a + b;
}
int main() {
// 定义function pointer
int (*f)(int,int) = func;
// 使用 function pointer
int res = f(1, 2); // res == 3
return 0;
}struct add_x {
add_x(int x) : x(x) {}
int operator()(int y) const { return x + y; }
private:
int x;
};void func3(std::vector<int>& v) {
std::for_each(v.begin(), v.end(), [](int) { /* do something here*/ });
}