網站首頁 編程語言 正文
一、說明Boost.Bind
Boost.Bind 是一個庫,它簡化和概括了最初需要 std::bind1st() 和 std::bind2nd() 的功能。這兩個函數被添加到 C++ 98 的標準庫中,即使它們的簽名不兼容,也可以連接函數。
二、庫應用示范
Boost.Bind 被添加到 C++ 11 的標準庫中。如果你的開發環境支持C++ 11,你會在頭文件中找到函數std::bind()。根據用例,使用 lambda 函數或 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() 的第三個參數是一個函數或函數對象,它需要一個唯一的參數。在示例 41.1 中,std :: for_each () 將容器 v 中的數字作為唯一參數一個接一個地傳遞給 print()。
如果你需要傳入一個簽名不符合算法要求的函數,那就更難了。例如,如果您希望 print() 接受一個輸出流作為附加參數,則不能再將其與 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 中的所有數字寫入標準輸出。但是,這一次,輸出流作為參數傳遞給 print()。為此,函數 print() 被定義為從 std::binary_function 派生的函數對象。
使用 Boost.Bind,您無需將 print() 從函數轉換為函數對象。相反,您使用在 boost/bind.hpp 中定義的函數模板 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));
}
Example41.3
示例 41.3 使用 print() 作為函數,而不是作為函數對象。因為 print() 需要兩個參數,所以函數不能直接傳遞給 std::for_each()。相反,boost::bind() 被傳遞給 std::for_each() 并且 print() 作為第一個參數被傳遞給 boost::bind()。
由于 print() 需要兩個參數,所以這兩個參數也必須傳遞給 boost::bind()。它們是指向 std :: cout 和 _1 的指針。
_1 是占位符。 Boost.Bind 定義了從 _1 到 _9 的占位符。這些占位符告訴 boost::bind() 返回一個函數對象,該對象期望與具有最大數量的占位符一樣多的參數。如果像示例 41.3 中一樣,僅使用占位符 _1,則 boost :: bind () 返回一個一元函數對象 - 一個需要唯一參數的函數對象。在這種情況下這是必需的,因為 std :: for_each () 只傳遞一個參數。
std::for_each() 調用一元函數對象。傳遞給函數對象的值 - 來自容器 v 的數字 - 占據占位符 _1 的位置。 boost :: bind () 獲取數字和指向 std :: cout 的指針并將它們轉發給 print ()。
請注意 boost::bind() 和 std::bind1st() 和 std::bind2nd() 一樣,都是按值取參數的。為了防止調用程序試圖復制 std::cout,print() 需要一個指向流的指針。 Boost.Ref 提供了一個允許您通過引用傳遞參數的函數。
示例 41.4 說明了如何使用 boost::bind() 定義二進制函數對象。它使用算法std :: sort (),它期望一個二進制函數作為它的第三個參數。
例 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 中,創建了一個二進制函數對象,因為使用了占位符 _2。算法 std::sort() 從容器 v 調用這個二進制函數對象,并用兩個值計算返回值以對容器進行排序。函數 compare() 被定義為對 v 進行降序排序。 由于 compare() 是二進制函數,所以可以直接傳給 std::sort()。但是,使用 boost :: bind () 仍然有意義,因為它允許您更改參數的順序。
例如,如果您想對容器進行升序排序但不想更改 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';
}
Example41.5
示例 41.5 簡單地通過交換占位符對 v 進行升序排序:首先傳遞 _2,然后傳遞 _1。
原文鏈接:https://yamagota.blog.csdn.net/article/details/127832675
相關推薦
- 2022-11-16 python多維列表總是只轉為一維數組問題解決_python
- 2022-04-09 iOS實現簡單計算器小功能_IOS
- 2022-12-05 go?code?review?代碼調試_Golang
- 2022-11-09 Sql?Server?"用戶登錄失敗,錯誤編18456"的解決過程_MsSql
- 2022-11-16 Python加載文件內容的兩種實現方式_python
- 2022-06-22 python?實現?mp3Play?音頻播放_python
- 2022-08-25 .net加載失敗的程序集實現重新加載_實用技巧
- 2022-06-12 Pandas對CSV文件讀寫操作詳解_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支