C++11標(biāo)準(zhǔn)庫(kù)bind函數(shù)應(yīng)用教程
bind 是什么?
bind 顧名思義: 綁定
通俗來講呢,可以這么理解有點(diǎn)像函數(shù)指針的意思。
資料上是這么講的:可以將 bind 函數(shù)看做一個(gè)通用函數(shù)的適配器,它接受一個(gè)可調(diào)用對(duì)象,生成一個(gè)新的可以調(diào)用對(duì)象來“適應(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ù)的位置來識(shí)別的,形如 _n 之類的, n 是整形, _1 是第一個(gè)參數(shù),_2是第二個(gè)參數(shù),以此類推。
而名字 _n 是定義在 placeholders 命名空間中, 而 placeholders 本身又定義在 std 命名空間中, 所以形如:
using std:: placeholders::_1
接下來,我們舉例幾個(gè)列子
舉個(gè)栗子
bind 是在頭文件 #include <functional>
中, 首先要包含它。
1. bind 無參數(shù)的普通函數(shù)
#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é)果:
2. bind 1個(gè)參數(shù)的普通函數(shù)
#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é)果:
3. bind 多個(gè)參數(shù)的普通函數(shù)
#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é)果:
4. bind 多個(gè)參數(shù)的普通函數(shù)并打亂參數(shù)位置
#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é)果:
5. bind 類的成員函數(shù)
#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é)果:
再舉個(gè)應(yīng)用栗子
#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é)果:
到此這篇關(guān)于C++11標(biāo)準(zhǔn)庫(kù)bind函數(shù)應(yīng)用教程的文章就介紹到這了,更多相關(guān)C++11 bind函數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- C++標(biāo)準(zhǔn)庫(kù)封裝的vector數(shù)組
- 淺談C++標(biāo)準(zhǔn)庫(kù)
- 從c++標(biāo)準(zhǔn)庫(kù)指針萃取器談一下traits技法(推薦)
- c/c++ 標(biāo)準(zhǔn)庫(kù) bind 函數(shù)詳解
- C++標(biāo)準(zhǔn)庫(kù)bitset類型的簡(jiǎn)單使用方法介紹
- 淺析C++標(biāo)準(zhǔn)庫(kù)元組(tuple)源碼
- C++標(biāo)準(zhǔn)庫(kù)中sstream與strstream的區(qū)別詳細(xì)解析
- C++的sstream標(biāo)準(zhǔn)庫(kù)詳細(xì)介紹
- C++超詳細(xì)講解標(biāo)準(zhǔn)庫(kù)
相關(guān)文章
C++實(shí)現(xiàn)LeetCode(57.插入?yún)^(qū)間)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(57.插入?yún)^(qū)間),本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07C++數(shù)據(jù)結(jié)構(gòu)之哈希表的實(shí)現(xiàn)
哈希表,即散列表,可以快速地存儲(chǔ)和查詢記錄。這篇文章主要為大家詳細(xì)介紹了C++數(shù)據(jù)結(jié)構(gòu)中哈希表的實(shí)現(xiàn),感興趣的小伙伴可以了解一下2023-03-03c語言數(shù)據(jù)結(jié)構(gòu)之并查集 總結(jié)
一種用于管理分組的數(shù)據(jù)結(jié)構(gòu)。它具備兩個(gè)操作:(1)查詢?cè)豠和元素b是否為同一組 (2) 將元素a和b合并為同一組,需要的朋友可以參考下2018-08-08C++基礎(chǔ)之this指針與另一種“多態(tài)”
this指針識(shí)別了同一個(gè)類的不同的對(duì)象,換句話說,this指針使得成員函數(shù)可以訪問同一個(gè)類的不同對(duì)象。再深入一點(diǎn),this指針使得成員函數(shù)會(huì)因?yàn)閠his指針的不同而訪問到了不同的成員變量2013-07-07C++Node類Cartographer開始軌跡的處理深度詳解
這篇文章主要介紹了C++Node類Cartographer開始軌跡的處理,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-03-03