iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >C++ std::bind用法详解
  • 571
分享到

C++ std::bind用法详解

2024-04-02 19:04:59 571人浏览 薄情痞子
摘要

一、介绍 c++11中提供了std::bind。bind()函数的意义就像它的函数名一样,是用来绑定函数调用的某些参数的。 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 >
 bind( F&& f, Args&&... args );
 
template< class R, class F, class... Args >
 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用法内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: C++ std::bind用法详解

本文链接: https://www.lsjlt.com/news/135485.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • C++ std::bind用法详解
    一、介绍 C++11中提供了std::bind。bind()函数的意义就像它的函数名一样,是用来绑定函数调用的某些参数的。 bind的思想实际上是一种延迟计算的思想,将可调用对象保存...
    99+
    2024-04-02
  • C++11 中的std::function和std::bind详解
    目录1. 可调用对象2. std::function3. std::bind3.1 std::bind绑定普通函数3.2 std::bind绑定一个成员函数3.3 绑定一个引用参数4...
    99+
    2024-04-02
  • C++11中std::function与std::bind的用法实例
    目录关于std::function 的用法:关于std::bind 的用法:附:std::function与std::bind双剑合璧总结关于std::function 的用法: 其...
    99+
    2024-04-02
  • C++11std::function和std::bind的使用示例详解
    目录概述可调用对象std::functionstd::function函数原型std::function的主要作用std::function的优缺点std::bindstd::bin...
    99+
    2023-03-19
    C++11 std::function和std::bind C++11 std::function和std::bind使用
  • C++11 std::function和std::bind如何使用
    这篇文章主要介绍了C++11 std::function和std::bind如何使用的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇C++11 std::function和std::bind如...
    99+
    2023-07-05
  • C++ std::function的用法详解
    类模版std::function是一种通用、多态的函数封装。std::function的实例可以对任何可以调用的目标实体进行存储、复制、和调用操作,这些目标实体包括普通函数、Lamb...
    99+
    2024-04-02
  • C++ std::function详解
    目录一、介绍二、实例三、与std::bind的区别总结一、介绍 std::function是函数模板类(是一个类)。包含在#include <functional> 中。...
    99+
    2024-04-02
  • C++11中std::function基础用法详解
    目录一、std::function基本介绍二、进阶使用方法2.1 与智能指针相结合2.2 存储成员函数指针2.3 存储std::bind三、注意tipsstd::function是C...
    99+
    2023-05-18
    C++11 std::function用法 C++11 std::function C++ std::function
  • 详解C++语言中std::array的神奇用法
    目录概述自动推导数组大小用函数返回std::array编译期字面量数值合法性校验编译期生成数组截取子数组拼接多个数组编译期拼接字符串展望C++20——打破更多的枷锁尾注概述 std:...
    99+
    2024-04-02
  • C++20中的std::span详解
    span就是一个连续对象存储的观察者。类似std::string_view是string的观察者。连续的存储,不一定是数组。例如: zero(char (&arr)...
    99+
    2023-03-06
    C++20 std::span C++ std
  • C++中std::allocator的使用案例详解
    标准库中包含一个名为allocator的类,允许我们将分配和初始化分离。使用allocator通常会提供更好的性能和更灵活的内存管理能力。    ...
    99+
    2024-04-02
  • C++ std::thread 使用方法
    目录一、std::thread的构造和析构二、std::thread的成员函数三、线程间的通信四、线程的异常处理五、总结总结:C++是一种高级编程语言,被广泛用于开发高性能、大规模、...
    99+
    2023-03-19
    C++ std::thread使用 C++ std::thread
  • C++STL标准库std::vector的使用详解
    目录1. 简介2. 使用示例3. 构造、析构、赋值3.1 std::vector::vector 构造函数3.2 std::vector::~vector 析构函数3.3 std::...
    99+
    2024-04-02
  • 详解C++11的std::addressof源码解析
    目录1、源码准备2、std::addressof简介3、std::addressof源码解析4、总结1、源码准备 本文是基于gcc-4.9.0的源代码进行分析,std::addre...
    99+
    2024-04-02
  • C++中std::thread线程用法
    目录1:std::thread的基本用法2:std:: thread常用的成员函数 3:建立新 thread执行类别中的函数 4:建立新 thread 执行 la...
    99+
    2023-01-08
    C++ std::thread线程 C++ std::thread
  • 解析C++11的std::ref、std::cref源码
    目录1、源码准备2、std::ref和std::cref的作用3、std::ref相关源码解析3.1、std::ref解析3.2、std::reference_wrapper解析3....
    99+
    2024-04-02
  • C语言 bind()函数案例详解
    bind()函数介绍        在建立套接字文件描述符成功后,需要对套接字进行地址和端口的绑定,才能进行数据的接收和发送操作。 函数原型 ...
    99+
    2024-04-02
  • c++中的bind使用方法
    除了容器有适配器之外,其实函数也提供了适配器,适配器的特点就是将一个类型改装成为拥有子集功能的新的类型。其中函数的适配器典型的就是通过std::bind来实现。 std::bind函...
    99+
    2024-04-02
  • C++入门笔记之std::vector容器详解
    目录前言1. vector的构造函数原型: 2. vector的赋值函数原型:3. vector的容量和大小函数原型:4. vector的插入和删除函数原型:5. vector的存取...
    99+
    2024-04-02
  • c++中std::怎么用
    std 是 c++ 中包含标准库组件的命名空间。为了使用 std,需要使用 "using namespace std;" 语句。直接使用 std 命名空间中的符号可以简化代码,但建议仅在...
    99+
    2024-05-09
    c++ 标准库
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作