首页 > 编程语言 >C++ std::function和std::bind的六种用法总结

C++ std::function和std::bind的六种用法总结

时间:2024-05-29 17:03:40浏览次数:17  
标签:std function cout int bind include 函数

一,使用funciton和bind的六种方法

1,使用function接收普通函数

2,使用function接收lambda函数

3,使用function函数来接收函数对象

4,使用bind函数绑定类中的一般函数

5,使用bind函数绑定类中的多态函数

6,使用function来实现回调。

二,代码实现

直接看代码和注释:

#include <iostream>
#include <functional>
#include <typeinfo>
#include <vector>

using namespace std;

class Foo
{
public:
	void func_1(int a, int b) {
		cout << "Foo: func_1" << endl;
		cout << a + b << endl;
	}
	void func_2(string a, string b)
	{
		cout << "Foo: func_2(string,string)" << endl;
		cout << a << " " << b << endl;
	}
	int func_2(string a, int b)
	{
		cout << "Foo: func_2(string,int)" << endl;
		cout << a << endl;
		cout << b << endl;
		return 0;
	}
};

//普通函数
int add(int a, int b) {
	return a + b;
}

//重载了操作符()的类
class Add
{
public:
	int operator()(int a, int b)
	{
		return a + b;
	}
};

class MyClass
{
public:
	void add(int n) {
		m_vec.push_back(n);
	}
	void forEach(function<void(int)> f)
	{
		for (auto item : m_vec)
		{
			f(item);
		}
	}
private:
	vector<int> m_vec;
};


int main()
{
	//用法一:接收普通函数
	function<int(int, int)> f;
	f = add;
	cout << "Method 1:" << endl;
	cout << f(1, 1) << endl;

	//用法二:接收lambda函数
	f = [](int a, int b) {return a + b; };
	cout << endl << "Method 2:" << endl;
	cout << f(2, 2) << endl;

	//用法三:接收函数对象, 对象所在类中需要实现操作符()重载。
	Add add;
	f = add;
	cout << endl << "Method 3:" << endl;
	cout << f(3, 3) << endl;

	//用法四:使用bind函数接收对象中的一般函数
	Foo foo;
	function<void(int, int)> f1 = bind(&Foo::func_1, foo, placeholders::_1,placeholders::_2);
	cout << endl << "Method 4:" << endl;
	f1(4, 4);

	//用法五:使用bind函数接收对象中的多态函数,类中有两个func_2函数,但参数不同。
	function<int(string)> f2 = bind((int(Foo::*)(string, int))& Foo::func_2, foo, placeholders::_1,16);
	cout << endl << "Method 5:" << endl;
	f2("hello world");

	//用法六:回调函数
	MyClass my;
	function<void(int)> f3 = [](int n) {cout << n << endl; };
	for (int i = 0; i < 10; i++)
	{
		my.add(i);
	}
	cout << endl << "Method 6:" << endl;
	my.forEach(f3);
}

标签:std,function,cout,int,bind,include,函数
From: https://blog.csdn.net/m0_64098026/article/details/139300501

相关文章

  • std-软件过程与管理期末复习
    软件过程与管理一、概论1.软件工程的三要素。过程、方法、工具  软件过程的定义。软件过程是用于软件开发及维护的一系列活动、方法及实践3.常见的软件过程分类。常见的软件过程。  二、软件质量管理1.软件质量的定义。  2.ISO/IEC9126的结构、ISO/......
  • 深入探讨Function Calling:实现外部函数调用的工作原理
    引言FunctionCalling是一个允许大型语言模型(如GPT)在生成文本的过程中调用外部函数或服务的功能。FunctionCalling允许我们以JSON格式向LLM模型描述函数,并使用模型的固有推理能力来决定在生成响应之前是否调用该函数。模型本身不执行函数,而是生成包含函数名称和执行函数......
  • python closure, first-class function, decorator
    闭包:closurefunctionhtml_tag(tag){ functionwrap_text(msg){  console.log('<'+tag+'>'+msg+'</'+tag+'>') } returnwrap_text}print_h1=html_tag('h1')print_h1('TestHea......
  • E117: Unknown function: acp#enable
    问题Errordetectedwhileprocessing/usr/share/vim/vim80/plugin/acp.vim:line164:E117:Unknownfunction:acp#enablePressENTERortypecommandtocontinue解释我是下载了最新版:https://www.vim.org/scripts/script.php?script_id=1879下载了的版本是2.14.1看......
  • Android跨进程通信--Binder机制及AIDL是什么?
    文章目录Binder机制Binder是什么?Binder相对于其他几种跨进程通信方式,有什么区别?谈一下BinderIPC通信过程:具体的通讯过程是什么?Binder如何处理发送请求与接收请求?Binder是通过什么方式来进行内存映射的?Binder是如何进行管理的?Binder、Socket的数据限制是多少?自己APP如......
  • C++容器之无序集(std::unordered_set)
    目录1概述2使用实例3接口使用3.1construct3.2assigns3.3iterators3.4capacity3.5find3.6count3.7equal_range3.8emplace3.9emplace_hint3.10insert3.11erase3.12clear3.13swap3.14bucket_count3.15max_bucket_count3.16bucket_s......
  • Linux系统postdrop服务进程持续增加导致无法登录
    临时解决方案:#servicepostfixstatus#servicesendmailstatus#servicepostfixstop[root@wign~]#ps-ef|grepsendmail|wc-l3038[root@wign~]#ps-ef|greppostdrop|wc-l3162停止postdrop服务,杀掉postdrop的进程#psaux|greppostdrop|grep-vgrep|cut-c9-15|xargs......
  • error: use of deleted function ‘YYSTYPE::YYSTYPE()’[解决]
    /home/zhywyt/Cmpelier/compiler2024/build/y.tab.cpp:1036:9:error:useofdeletedfunction‘YYSTYPE::YYSTYPE()’1036|YYSTYPEyylval;报错信息如下:bison文件中的union如下,发现错误为name的类型为带析构函数的类,导致出现问题。具体原因不明。修改为string*后编译......
  • 【MinIO】SpringBoot引入MinIO依赖遇到的一些问题:okhttp、kotlib-stdlib
    参考官方文档SDK:https://docs.min.io/docs/java-client-quickstart-guide.htmlMinIOJavaSDKisSimpleStorageService(akaS3)clienttoperformbucketandobjectoperationstoanyAmazonS3compatibleobjectstorageservice.MinIO依赖jar包下载地址:CentralReposi......
  • Semantic Kernel入门系列:利用Handlebars创建Prompts functions
    引言本章我们将学习通过HandlebarsPromptsTemplate来创建Promptsfunctions。什么是Handlebars?Handlebars是一个流行的JavaScript模板引擎,它允许你通过在HTML中使用简单的占位符来创建动态的HTML。它使用模板和输入对象来生成HTML或其他文本格式。Handlebars模板看......