C++ std::bind用法详解

 更新时间:2021年09月13日 11:03:40   作者:物随心转  
这篇文章主要介绍了C++ std::bind用法详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下

一、介绍

C++11中提供了std::bind。bind()函数的意义就像它的函数名一样,是用来绑定函数调用的某些参数的。

bind的思想实际上是一种延迟计算的思想,将可调用对象保存起来,然后在需要的时候再调用。而且这种绑定是非常灵活的,不论是普通函数、函数对象、还是成员函数都可以绑定,而且其参数可以支持占位符,比如你可以这样绑定一个二元函数:

auto f = bind(&func, std::placeholders::_1, std::placeholders::_2);调用的时候通过f(1,2)实现调用。所以,我们可简单的认为std::bind就是std::bind1ststd::bind2nd的加强版。

std::bind函数有两种函数原型,定义如下:

template< class F, class... Args >
/*unspecified*/ bind( F&& f, Args&&... args );
 
template< class R, class F, class... Args >
/*unspecified*/ bind( F&& f, Args&&... args );

Parameters

f - Callable object (function object, pointer to function, reference to function, pointer to member function, or pointer to data member) that will be bound to some arguments
args - list of arguments to bind, with the unbound arguments replaced by the placeholders _1, _2, _3... of namespace std::placeholders

二、实例

这里要先学习仿函数。请参考仿函数的使用

实例1

#include <iostream>
#include <functional>
using namespace std;
 
int TestFunc(int a, char c, float f)
{
    cout << a << endl;
    cout << c << endl;
    cout << f << endl;
 
    return a;
}
 
int main()
{
    auto bindFunc1 = bind(TestFunc, std::placeholders::_1, 'A', 100.1);
    bindFunc1(10); //等于TestFunc(10,'A', 100.1)
 
    cout << "=================================\n";
 
    auto bindFunc2 = bind(TestFunc, std::placeholders::_2, std::placeholders::_1, 100.1);
    bindFunc2('B', 10); //等于TestFunc(10,'B', 100.1)
 
    cout << "=================================\n";
 
    auto bindFunc3 = bind(TestFunc, std::placeholders::_2, std::placeholders::_3, std::placeholders::_1);
    bindFunc3(100.1, 30, 'C'); //等于TestFunc(30,'C', 100.1)
 
    return 0;
}

 

上面这段代码主要说的是bind中std::placeholders的使用。 std::placeholders是一个占位符。当使用bind生成一个新的可调用对象时,std::placeholders表示新的可调用对象的第 几个参数和原函数的第几个参数进行匹配。

auto bindFunc3 = bind(TestFunc, std::placeholders::_2, std::placeholders::_3, std::placeholders::_1);
 
bindFunc3(100.1, 30, 'C');

可以看到,在bind的时候,第一个位置是TestFunc,除了这个,参数的第一个位置为占位符std::placeholders::_2,这就表示,调用bindFunc3的时候,它的第二个参数——即30,和TestFunc的第一个参数匹配,所以std::placeholders::_2为30,以此类推,最后,实际执行的是TestFunc(30,'C', 100.1)。 

实例2

#include <random>
#include <iostream>
#include <memory>
#include <functional>
 
void f(int n1, int n2, int n3, const int& n4, int n5)
{
	std::cout << n1 << ' ' << n2 << ' ' << n3 << ' ' << n4 << ' ' << n5 << '\n';
}
 
int g(int n1)
{
	return n1;
}
 
struct Foo {
	void print_sum(int n1, int n2)
	{
		std::cout << n1 + n2 << '\n';
	}
	int data = 10;
};
 
int main()
{
	using namespace std::placeholders;  // for _1, _2, _3...
 
	// demonstrates argument reordering and pass-by-reference
	int n = 7;
	// (_1 and _2 are from std::placeholders, and represent future
	// arguments that will be passed to f1)
 
	auto f1 = std::bind(f, _2, 42, _1, std::cref(n), n);
	n = 10;
	f1(1, 2, 1001); // 1 is bound by _1, 2 is bound by _2, 1001 is unused
					// makes a call to f(2, 42, 1, n, 7)
 
	// nested bind subexpressions share the placeholders
	auto f2 = std::bind(f, _3, std::bind(g, _3), _3, 4, 5);
	f2(10, 11, 12); // makes a call to f(12, g(12), 12, 4, 5);
 
	// bind to a pointer to member function
	Foo foo;
	auto f3 = std::bind(&Foo::print_sum, &foo, 95, _1);
	f3(5);
 
	// bind to a pointer to data member
	auto f4 = std::bind(&Foo::data, _1);
	std::cout << f4(foo) << '\n';
 
	std::cout << f4(std::make_shared<Foo>(foo)) << '\n'
		<< f4(std::make_unique<Foo>(foo)) << '\n';
 
        return 0;
}

参考:

https://en.cppreference.com/w/cpp/utility/functional/bind

https://blog.csdn.net/qq_37653144/article/details/79285221

https://blog.csdn.net/u013654125/article/details/100140328#commentBox

到此这篇关于C++ std::bind用法详解的文章就介绍到这了,更多相关C++ std::bind用法内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • C++空指针void*的使用方法

    C++空指针void*的使用方法

    C++空指针void是一种通用指针类型,可以指向任何类型的数据或对象。它不关心指向的数据或对象的类型,只关心指针本身的地址,在使用void指针时,需要将其转换为特定类型的指针,以便对其进行操作或访问其值,本文就给大家介绍一下C++空指针void的使用方法
    2023-06-06
  • 带你粗略了解C++中的深浅拷贝

    带你粗略了解C++中的深浅拷贝

    这篇文章主要给大家介绍了关于c++中深浅拷贝以及写时拷贝实现的相关资料,文中通过示例代码介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面跟着小编来一起学习学习吧
    2021-08-08
  • C语言实现最小生成树构造算法

    C语言实现最小生成树构造算法

    这篇文章主要为大家详细介绍了C语言实现最小生成树构造算法,利用Prim算法或kruskal算法求解,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-01-01
  • C++内存四区之代码区、全局区、栈区和堆区

    C++内存四区之代码区、全局区、栈区和堆区

    C++编译器会把代码直接分为四个小区,弄懂这四小区对我们理解内存有所帮助,所以下面这篇文章主要给大家介绍了关于C++内存四区之代码区、全局区、栈区和堆区的相关资料,需要的朋友可以参考下
    2021-07-07
  • openCV中meanshift算法查找目标的实现

    openCV中meanshift算法查找目标的实现

    本文主要介绍了openCV中meanshift算法查找目标的实现,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-11-11
  • C++17结构化绑定的实现

    C++17结构化绑定的实现

    这篇文章主要介绍了C++17结构化绑定的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-05-05
  • Qt QChart实现折线图的绘制

    Qt QChart实现折线图的绘制

    QChart是常用的图表,这篇文章主要为大家详细介绍了Qt如何利用QChart实现折线图的绘制,文中的示例代码讲解详细,感兴趣的可以了解一下
    2023-04-04
  • C++关于构造函数可向父类或者本类传参的讲解

    C++关于构造函数可向父类或者本类传参的讲解

    今天小编就为大家分享一篇关于C++关于构造函数可向父类或者本类传参的讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-12-12
  • 关于C/C++中可变参数的详细介绍(va_list,va_start,va_arg,va_end)

    关于C/C++中可变参数的详细介绍(va_list,va_start,va_arg,va_end)

    可变参数的函数原理其实很简单,而va系列是以宏定义来定义的,实现跟堆栈相关.我们写一个可变函数的C函数时,有利也有弊,所以在不必要的场合,我们无需用到可变参数。如果在C++里,我们应该利用C++的多态性来实现可变参数的功能,尽量避免用C语言的方式来实现
    2013-10-10
  • C语言中feof函数和ferror函数示例详解

    C语言中feof函数和ferror函数示例详解

    在C语言中feof函数用于检查文件流的结束标志,判断文件在读取时是否已经到达了文件的末尾,这篇文章主要给大家介绍了关于C语言中feof函数和ferror函数的相关资料,需要的朋友可以参考下
    2024-09-09

最新评论