[转]C++ std::function 和 std::bind
目录
原文链接:c++判断vector中是否存在特定元素的方法
std::function 仿函数对象#
std::function 用来声明函数对象的,换句话说,就和函数指针、Lambda表达式、函数名是一个东西 。
#include <iostream>
#include <functional>
// 传统C函数
int c_function(int a, int b)
{
return a + b;
}
int main(int argc, char** argv)
{
// 万能可调用对象类型
std::function<int(int, int)> callableObject;
callableObject = c_function; // 可以用传统C函数指针直接赋值
std::cout << callableObject(3, 3) << std::endl;
//输出结果为:6
return 0;
}
std::bind#
std::bind 就是将一个函数对象绑定成为另一个函数对象,std::bind的返回值类型是std::function 。
头文件 #include
命名空间 std、std::placeholders
bind使用时的注意细节#
- bind的第一个参数必须要加取地址符号&
- 必须加上using namespace std::placeholders,否则找不到占位符
全局函数、静态全局函数 :#
static void show(const std::string& a, const std::string& b, const std::string& c)
{
std::cout << a << "; " << b << "; " << c << std::endl;
}
int main()
{
using namespace std::placeholders; // 由于std::placeholders,可以将参数传递给绑定函数的顺序更改
auto x = std::bind(&show, _1, _2, _3);
auto y = std::bind(&show, _3, _1, _2);
auto z = std::bind(&show, "hello", _2, _1);
auto e = std::bind(&show, _3, _3, _3);
x("one", "two", "three"); // one; two; three
y("one", "two", "three"); // three; one; two
z("one", "two"); // hello; two; one
e("one", "two", "three"); // three; three; three
}
类的static成员函数#
class A
{
public:
static void show(const std::string& a, const std::string& b, const std::string& c)
{
std::cout << a << "; " << b << "; " << c << std::endl;
}
};
int main()
{
using namespace std::placeholders; // 由于std::placeholders,可以将参数传递给绑定函数的顺序更改
auto x = bind(&A::show, _1, _2, _3);
auto y = bind(&A::show, _3, _1, _2);
auto z = bind(&A::show, "hello", _2, _1);
auto e = bind(&A::show, _3, _3, _3);
x("one", "two", "three"); // one; two; three
y("one", "two", "three"); // three; one; two
z("one", "two"); // hello; two; one
e("one", "two", "three"); // three; three; three
return 0;
}
类的非static成员函数#
类的非static成员函数(注意:_1必须是某个对象的地址)
结论: 对类的非static成员函数bind时,除了需要加上作用域A::之外,还要多加一个类对象参数
class A{
public:
void show(const std::string& a, const std::string& b, const std::string& c)
{
std::cout << a << "; " << b << "; " << c << std::endl;
}
};
int main()
{
using namespace std::placeholders; // 由于std::placeholders,可以将参数传递给绑定函数的顺序更改
A aa;
auto x = bind(&A::show, aa, _1, _2, _3); //多加一个类对象
auto y = bind(&A::show, aa, _3, _1, _2);
auto z = bind(&A::show, aa, "hello", _2, _1);
auto e = bind(&A::show, aa, _3, _3, _3);
x("one", "two", "three"); // one; two; three
y("one", "two", "three"); // three; one; two
z("one", "two"); // hello; two; one
e("one", "two", "three"); // three; three; three
return 0;
}
std::function 和 std::bind 配合使用#
#include <iostream>
#include <functional>
void showAll(int a, double b, const std::string& c)
{
std::cout << a << "; " << b << "; " << c << std::endl;
}
int main(int argc, char** argv)
{
using namespace std::placeholders;
std::function<void(int, double)> output =
std::bind(&showAll, _1, _2, "Kobe");
output(1, 2); //调用函数
//输出结果为:1; 2; Kobe
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!