您好,登錄后才能下訂單哦!
這篇文章主要講解了“C++11標(biāo)準(zhǔn)庫(kù)bind函數(shù)應(yīng)用方法是什么”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“C++11標(biāo)準(zhǔn)庫(kù)bind函數(shù)應(yīng)用方法是什么”吧!
bind 顧名思義: 綁定
通俗來(lái)講呢,可以這么理解有點(diǎn)像函數(shù)指針的意思。
資料上是這么講的:可以將 bind 函數(shù)看做一個(gè)通用函數(shù)的適配器,它接受一個(gè)可調(diào)用對(duì)象,生成一個(gè)新的可以調(diào)用對(duì)象來(lái)“適應(yīng)”原對(duì)象參數(shù)列表
它一般調(diào)用形式:
// 其中 newCallable 是一個(gè)可調(diào)用的對(duì)象, arg_list 是以逗號(hào)分隔的參數(shù)列表 // 這時(shí)我們調(diào)用 newCallable,newCallable 就會(huì)調(diào)用 callable, 并用 arg_list 傳遞參數(shù) auto newCallable = bind(callable, arg_list);
好了,重點(diǎn)在于 arg_list 里,那么我們?nèi)绾蝹魅雲(yún)?shù)呢
它們是靠這些參數(shù)的位置來(lái)識(shí)別的,形如 _n 之類的, n 是整形, _1 是第一個(gè)參數(shù),_2是第二個(gè)參數(shù),以此類推。
而名字 _n 是定義在 placeholders 命名空間中, 而 placeholders 本身又定義在 std 命名空間中, 所以形如:
using std:: placeholders::_1
接下來(lái),我們舉例幾個(gè)列子
bind 是在頭文件 #include <functional>
中, 首先要包含它。
#include <iostream> #include <functional> // 包含此頭文件 // 普通函數(shù) void Fun() { std::cout << "I am Fun!" << std::endl; } // 主函數(shù) int main() { auto fun = std::bind(Fun); // 適配 Fun 函數(shù)并返回一個(gè)可調(diào)用的對(duì)象 fun(); return 0; }
調(diào)試結(jié)果:
#include <iostream> #include <functional> // 包含此頭文件 // 普通函數(shù) void Fun(int a) { std::cout << "I am Fun! a = " << a <<std::endl; } // 主函數(shù) int main() { auto fun = std::bind(Fun, std::placeholders::_1); fun(5); return 0; }
調(diào)試結(jié)果:
#include <iostream> #include <functional> // 包含此頭文件 // 普通函數(shù) int Fun(int a, int b) { return a - b; } // 主函數(shù) int main() { auto fun = std::bind(Fun, std::placeholders::_1, std::placeholders::_2); std::cout << fun(5, 2) << std::endl; return 0; }
調(diào)試結(jié)果:
#include <iostream> #include <functional> // 包含此頭文件 // 普通函數(shù) int Fun(int a, int b) { return a - b; } // 主函數(shù) int main() { auto fun1 = std::bind(Fun, std::placeholders::_1, std::placeholders::_2); auto fun2 = std::bind(Fun, std::placeholders::_2, std::placeholders::_1); std::cout << fun1(5, 2) << std::endl; std::cout << fun1(5, 2) << std::endl; return 0; }
調(diào)試結(jié)果:
#include <iostream> #include <functional> // 包含此頭文件 class MyClass { public: MyClass() {} ~MyClass() {} public: void printInfo() { std::cout << "MyClass Info." << std::endl; } }; // 主函數(shù) int main() { MyClass A; auto fun = std::bind(&MyClass::printInfo, A); fun(); return 0; }
調(diào)試結(jié)果:
#include <iostream> #include <functional> // 包含此頭文件 typedef std::function<void(int)> CallbackType; // A 類 class MyClassA { public: void regeditCallBack(CallbackType fun) { _callback_fun = fun; } void printInfoA(int d) { _callback_fun(d); } private: CallbackType _callback_fun; }; // B 類 class MyClassB { public: void printInfoB(int d) { std::cout << d << std::endl; } }; // 主函數(shù) int main() { MyClassB B; auto funB = std::bind(&MyClassB::printInfoB, B, std::placeholders::_1); MyClassA A; A.regeditCallBack(funB); A.printInfoA(1); return 0; }
調(diào)試結(jié)果:
感謝各位的閱讀,以上就是“C++11標(biāo)準(zhǔn)庫(kù)bind函數(shù)應(yīng)用方法是什么”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)C++11標(biāo)準(zhǔn)庫(kù)bind函數(shù)應(yīng)用方法是什么這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。