最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++ std::bind用法詳解

 更新時(shí)間:2021年09月13日 11:03:40   作者:物隨心轉(zhuǎn)  
這篇文章主要介紹了C++ std::bind用法詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下

一、介紹

C++11中提供了std::bind。bind()函數(shù)的意義就像它的函數(shù)名一樣,是用來(lái)綁定函數(shù)調(diào)用的某些參數(shù)的。

bind的思想實(shí)際上是一種延遲計(jì)算的思想,將可調(diào)用對(duì)象保存起來(lái),然后在需要的時(shí)候再調(diào)用。而且這種綁定是非常靈活的,不論是普通函數(shù)、函數(shù)對(duì)象、還是成員函數(shù)都可以綁定,而且其參數(shù)可以支持占位符,比如你可以這樣綁定一個(gè)二元函數(shù):

auto f = bind(&func, std::placeholders::_1, std::placeholders::_2);調(diào)用的時(shí)候通過(guò)f(1,2)實(shí)現(xiàn)調(diào)用。所以,我們可簡(jiǎn)單的認(rèn)為std::bind就是std::bind1ststd::bind2nd的加強(qiáng)版。

std::bind函數(shù)有兩種函數(shù)原型,定義如下:

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

二、實(shí)例

這里要先學(xué)習(xí)仿函數(shù)。請(qǐng)參考仿函數(shù)的使用

實(shí)例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;
}

 

上面這段代碼主要說(shuō)的是bind中std::placeholders的使用。 std::placeholders是一個(gè)占位符。當(dāng)使用bind生成一個(gè)新的可調(diào)用對(duì)象時(shí),std::placeholders表示新的可調(diào)用對(duì)象的第 幾個(gè)參數(shù)和原函數(shù)的第幾個(gè)參數(shù)進(jìn)行匹配。

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

可以看到,在bind的時(shí)候,第一個(gè)位置是TestFunc,除了這個(gè),參數(shù)的第一個(gè)位置為占位符std::placeholders::_2,這就表示,調(diào)用bindFunc3的時(shí)候,它的第二個(gè)參數(shù)——即30,和TestFunc的第一個(gè)參數(shù)匹配,所以std::placeholders::_2為30,以此類推,最后,實(shí)際執(zhí)行的是TestFunc(30,'C', 100.1)。 

實(shí)例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

到此這篇關(guān)于C++ std::bind用法詳解的文章就介紹到這了,更多相關(guān)C++ std::bind用法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

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

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

    C++空指針void是一種通用指針類型,可以指向任何類型的數(shù)據(jù)或?qū)ο?。它不關(guān)心指向的數(shù)據(jù)或?qū)ο蟮念愋?,只關(guān)心指針本身的地址,在使用void指針時(shí),需要將其轉(zhuǎn)換為特定類型的指針,以便對(duì)其進(jìn)行操作或訪問(wèn)其值,本文就給大家介紹一下C++空指針void的使用方法
    2023-06-06
  • 帶你粗略了解C++中的深淺拷貝

    帶你粗略了解C++中的深淺拷貝

    這篇文章主要給大家介紹了關(guān)于c++中深淺拷貝以及寫(xiě)時(shí)拷貝實(shí)現(xiàn)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-08-08
  • C語(yǔ)言實(shí)現(xiàn)最小生成樹(shù)構(gòu)造算法

    C語(yǔ)言實(shí)現(xiàn)最小生成樹(shù)構(gòu)造算法

    這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)最小生成樹(shù)構(gòu)造算法,利用Prim算法或kruskal算法求解,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • C++內(nèi)存四區(qū)之代碼區(qū)、全局區(qū)、棧區(qū)和堆區(qū)

    C++內(nèi)存四區(qū)之代碼區(qū)、全局區(qū)、棧區(qū)和堆區(qū)

    C++編譯器會(huì)把代碼直接分為四個(gè)小區(qū),弄懂這四小區(qū)對(duì)我們理解內(nèi)存有所幫助,所以下面這篇文章主要給大家介紹了關(guān)于C++內(nèi)存四區(qū)之代碼區(qū)、全局區(qū)、棧區(qū)和堆區(qū)的相關(guān)資料,需要的朋友可以參考下
    2021-07-07
  • openCV中meanshift算法查找目標(biāo)的實(shí)現(xiàn)

    openCV中meanshift算法查找目標(biāo)的實(shí)現(xiàn)

    本文主要介紹了openCV中meanshift算法查找目標(biāo)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • C++17結(jié)構(gòu)化綁定的實(shí)現(xiàn)

    C++17結(jié)構(gòu)化綁定的實(shí)現(xiàn)

    這篇文章主要介紹了C++17結(jié)構(gòu)化綁定的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • Qt QChart實(shí)現(xiàn)折線圖的繪制

    Qt QChart實(shí)現(xiàn)折線圖的繪制

    QChart是常用的圖表,這篇文章主要為大家詳細(xì)介紹了Qt如何利用QChart實(shí)現(xiàn)折線圖的繪制,文中的示例代碼講解詳細(xì),感興趣的可以了解一下
    2023-04-04
  • C++關(guān)于構(gòu)造函數(shù)可向父類或者本類傳參的講解

    C++關(guān)于構(gòu)造函數(shù)可向父類或者本類傳參的講解

    今天小編就為大家分享一篇關(guān)于C++關(guān)于構(gòu)造函數(shù)可向父類或者本類傳參的講解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2018-12-12
  • 關(guān)于C/C++中可變參數(shù)的詳細(xì)介紹(va_list,va_start,va_arg,va_end)

    關(guān)于C/C++中可變參數(shù)的詳細(xì)介紹(va_list,va_start,va_arg,va_end)

    可變參數(shù)的函數(shù)原理其實(shí)很簡(jiǎn)單,而va系列是以宏定義來(lái)定義的,實(shí)現(xiàn)跟堆棧相關(guān).我們寫(xiě)一個(gè)可變函數(shù)的C函數(shù)時(shí),有利也有弊,所以在不必要的場(chǎng)合,我們無(wú)需用到可變參數(shù)。如果在C++里,我們應(yīng)該利用C++的多態(tài)性來(lái)實(shí)現(xiàn)可變參數(shù)的功能,盡量避免用C語(yǔ)言的方式來(lái)實(shí)現(xiàn)
    2013-10-10
  • C語(yǔ)言中feof函數(shù)和ferror函數(shù)示例詳解

    C語(yǔ)言中feof函數(shù)和ferror函數(shù)示例詳解

    在C語(yǔ)言中feof函數(shù)用于檢查文件流的結(jié)束標(biāo)志,判斷文件在讀取時(shí)是否已經(jīng)到達(dá)了文件的末尾,這篇文章主要給大家介紹了關(guān)于C語(yǔ)言中feof函數(shù)和ferror函數(shù)的相關(guān)資料,需要的朋友可以參考下
    2024-09-09

最新評(píng)論

玉环县| 依安县| 鹤峰县| 祥云县| 琼结县| 江源县| 滁州市| 民乐县| 潍坊市| 久治县| 农安县| 梁河县| 新源县| 兴化市| 崇仁县| 安康市| 菏泽市| 新蔡县| 浏阳市| 安化县| 清新县| 襄城县| 鲁山县| 安吉县| 鄂州市| 平顶山市| 剑阁县| 安顺市| 伊川县| 商城县| 社旗县| 新郑市| 普洱| 普定县| 西丰县| 洛隆县| 镶黄旗| 汶川县| 安溪县| 曲靖市| 马边|