C++ Boost Bind庫(kù)示例分析使用
一、說明Boost.Bind
Boost.Bind 是一個(gè)庫(kù),它簡(jiǎn)化和概括了最初需要 std::bind1st() 和 std::bind2nd() 的功能。這兩個(gè)函數(shù)被添加到 C++ 98 的標(biāo)準(zhǔn)庫(kù)中,即使它們的簽名不兼容,也可以連接函數(shù)。
二、庫(kù)應(yīng)用示范
Boost.Bind 被添加到 C++ 11 的標(biāo)準(zhǔn)庫(kù)中。如果你的開發(fā)環(huán)境支持C++ 11,你會(huì)在頭文件中找到函數(shù)std::bind()。根據(jù)用例,使用 lambda 函數(shù)或 Boost.Phoenix 可能比 std :: bind () 或 Boost.Bind 更好。
例 41.1。 std::for_each() 具有兼容功能
#include <vector> #include <algorithm> #include <iostream> void print(int i) { std::cout << i << '\n'; } int main() { std::vector<int> v{1, 3, 2}; std::for_each(v.begin(), v.end(), print); }
std::for_each() 的第三個(gè)參數(shù)是一個(gè)函數(shù)或函數(shù)對(duì)象,它需要一個(gè)唯一的參數(shù)。在示例 41.1 中,std :: for_each () 將容器 v 中的數(shù)字作為唯一參數(shù)一個(gè)接一個(gè)地傳遞給 print()。
如果你需要傳入一個(gè)簽名不符合算法要求的函數(shù),那就更難了。例如,如果您希望 print() 接受一個(gè)輸出流作為附加參數(shù),則不能再將其與 std::for_each() 原樣使用。
例 41.2。 std :: for_each () 與 std :: bind1st ()
#include <vector> #include <algorithm> #include <functional> #include <iostream> class print : public std::binary_function<std::ostream*, int, void> { public: void operator()(std::ostream *os, int i) const { *os << i << '\n'; } }; int main() { std::vector<int> v{1, 3, 2}; std::for_each(v.begin(), v.end(), std::bind1st(print{}, &std::cout)); }
與示例 41.1 一樣,示例 41.2 將 v 中的所有數(shù)字寫入標(biāo)準(zhǔn)輸出。但是,這一次,輸出流作為參數(shù)傳遞給 print()。為此,函數(shù) print() 被定義為從 std::binary_function 派生的函數(shù)對(duì)象。
使用 Boost.Bind,您無需將 print() 從函數(shù)轉(zhuǎn)換為函數(shù)對(duì)象。相反,您使用在 boost/bind.hpp 中定義的函數(shù)模板 boost::bind()。
例 41.3。 std :: for_each () 與 boost :: bind ()
#include <boost/bind.hpp> #include <vector> #include <algorithm> #include <iostream> void print(std::ostream *os, int i) { *os << i << '\n'; } int main() { std::vector<int> v{1, 3, 2}; std::for_each(v.begin(), v.end(), boost::bind(print, &std::cout, _1)); }
示例 41.3 使用 print() 作為函數(shù),而不是作為函數(shù)對(duì)象。因?yàn)?print() 需要兩個(gè)參數(shù),所以函數(shù)不能直接傳遞給 std::for_each()。相反,boost::bind() 被傳遞給 std::for_each() 并且 print() 作為第一個(gè)參數(shù)被傳遞給 boost::bind()。
由于 print() 需要兩個(gè)參數(shù),所以這兩個(gè)參數(shù)也必須傳遞給 boost::bind()。它們是指向 std :: cout 和 _1 的指針。
_1 是占位符。 Boost.Bind 定義了從 _1 到 _9 的占位符。這些占位符告訴 boost::bind() 返回一個(gè)函數(shù)對(duì)象,該對(duì)象期望與具有最大數(shù)量的占位符一樣多的參數(shù)。如果像示例 41.3 中一樣,僅使用占位符 _1,則 boost :: bind () 返回一個(gè)一元函數(shù)對(duì)象 - 一個(gè)需要唯一參數(shù)的函數(shù)對(duì)象。在這種情況下這是必需的,因?yàn)?std :: for_each () 只傳遞一個(gè)參數(shù)。
std::for_each() 調(diào)用一元函數(shù)對(duì)象。傳遞給函數(shù)對(duì)象的值 - 來自容器 v 的數(shù)字 - 占據(jù)占位符 _1 的位置。 boost :: bind () 獲取數(shù)字和指向 std :: cout 的指針并將它們轉(zhuǎn)發(fā)給 print ()。
請(qǐng)注意 boost::bind() 和 std::bind1st() 和 std::bind2nd() 一樣,都是按值取參數(shù)的。為了防止調(diào)用程序試圖復(fù)制 std::cout,print() 需要一個(gè)指向流的指針。 Boost.Ref 提供了一個(gè)允許您通過引用傳遞參數(shù)的函數(shù)。
示例 41.4 說明了如何使用 boost::bind() 定義二進(jìn)制函數(shù)對(duì)象。它使用算法std :: sort (),它期望一個(gè)二進(jìn)制函數(shù)作為它的第三個(gè)參數(shù)。
例 41.4。 std :: sort () 與 boost :: bind ()
#include <boost/bind.hpp> #include <vector> #include <algorithm> #include <iostream> bool compare(int i, int j) { return i > j; } int main() { std::vector<int> v{1, 3, 2}; std::sort(v.begin(), v.end(), boost::bind(compare, _1, _2)); for (int i : v) std::cout << i << '\n'; }
在示例 41.4 中,創(chuàng)建了一個(gè)二進(jìn)制函數(shù)對(duì)象,因?yàn)槭褂昧苏嘉环?_2。算法 std::sort() 從容器 v 調(diào)用這個(gè)二進(jìn)制函數(shù)對(duì)象,并用兩個(gè)值計(jì)算返回值以對(duì)容器進(jìn)行排序。函數(shù) compare() 被定義為對(duì) v 進(jìn)行降序排序。 由于 compare() 是二進(jìn)制函數(shù),所以可以直接傳給 std::sort()。但是,使用 boost :: bind () 仍然有意義,因?yàn)樗试S您更改參數(shù)的順序。
例如,如果您想對(duì)容器進(jìn)行升序排序但不想更改 compare(),則可以使用 boost::bind()。
例 41.5。 std :: sort () 與 boost :: bind () 并更改了占位符的順序
#include <boost/bind.hpp> #include <vector> #include <algorithm> #include <iostream> bool compare(int i, int j) { return i > j; } int main() { std::vector<int> v{1, 3, 2}; std::sort(v.begin(), v.end(), boost::bind(compare, _2, _1)); for (int i : v) std::cout << i << '\n'; }
示例 41.5 簡(jiǎn)單地通過交換占位符對(duì) v 進(jìn)行升序排序:首先傳遞 _2,然后傳遞 _1。
到此這篇關(guān)于C++ Boost Bind庫(kù)示例分析使用的文章就介紹到這了,更多相關(guān)C++ Boost Bind內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++?vector與數(shù)組轉(zhuǎn)換寫入/讀出文件方式
這篇文章主要介紹了C++?vector與數(shù)組轉(zhuǎn)換寫入/讀出文件方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11基于malloc與free函數(shù)的實(shí)現(xiàn)代碼及分析
本篇文章介紹了malloc與free函數(shù)的實(shí)現(xiàn)代碼及分析。需要的朋友參考下2013-05-05新手向超詳細(xì)的C語言實(shí)現(xiàn)動(dòng)態(tài)順序表
本文主要介紹了C語言實(shí)現(xiàn)動(dòng)態(tài)順序表,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09C++實(shí)現(xiàn)鄰接表頂點(diǎn)的刪除
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)鄰接表頂點(diǎn)的刪除,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04C語言通過三步翻轉(zhuǎn)法實(shí)現(xiàn)單詞倒置詳解
這篇文章主要為大家分享了用三步翻轉(zhuǎn)法將一句話的單詞進(jìn)行倒置的方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-05-05