程序示例如下:
#include
#include
using foo = void(int); // function pointer
void functional(foo f) {
f(1);
}
int foo2(int para) {
return para;
}
int foo3(int a, int b, int c) {
return 0;
}
int main() {
auto f = [](int value) {
std::cout << value << std::endl;
};
functional(f); // call by function pointer
f(1); // call by lambda expression
// std::function wraps a function that take int paremeter and returns int value
std::function func = foo2;
int important = 10;
std::function func2 = [&](int value) -> int {
return 1+value+important;
};
std::cout << func(10) << std::endl;
std::cout << func2(10) << std::endl;
// bind parameter 1, 2 on function foo, and use std::placeholders::_1 as placeholder
// for the first parameter.
auto bindFoo = std::bind(foo3, std::placeholders::_1, 1,2);
// when call bindFoo, we only need one param left
bindFoo(1);
return 0;
}
输出结果如下:
修改一些参数如下:
再看一下运行结果,是否与如下对应,是否理解这些。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)