一、函數(shù)適配器簡介
1、函數(shù)適配器概念
在 STL 中 預(yù)定義了很多 函數(shù)對(duì)象 , 如果要 對(duì) 函數(shù)對(duì)象 的 參數(shù) / 返回值 進(jìn)行 計(jì)算 或 設(shè)置 , 可以 使用 " 函數(shù)適配器 " 實(shí)現(xiàn)上述需求 ;
" 函數(shù)適配器 " 可以 將 已存在的 函數(shù)對(duì)象 轉(zhuǎn)化為 另一種符合要求的 函數(shù)對(duì)象 ;
" 函數(shù)適配器 " 定義在 <functional> 頭文件 中 ;
2、函數(shù)適配器分類
" 函數(shù)適配器 " 常用類型的兩類 :
-
綁定適配器 :
- std::bind 綁定適配器 : 引入的一個(gè) 通用綁定機(jī)制 , 可 綁定 函數(shù) / 函數(shù)指針 / 函數(shù)對(duì)象 / 成員函數(shù)指針 任意 位置 的 參數(shù) 到指定的值 , 也可以重新排列參數(shù)的順序 , 或者將多個(gè)可調(diào)用對(duì)象組合在一起 ; C++ 11 最新引入的 , 建議使用 該 綁定適配器 ;
- std::binder1st 綁定適配器 : 將一個(gè)二元函數(shù)對(duì)象的 第一個(gè) 參數(shù) 綁定到一個(gè)特定的值 , 從而創(chuàng)建一個(gè)新的一元函數(shù)對(duì)象 ; C++ 11 棄用 , 建議使用 std::bind 綁定適配器 ;
- std::binder2nd 綁定適配器 : 將一個(gè)二元函數(shù)對(duì)象的 第二個(gè) 參數(shù) 綁定到一個(gè)特定的值 , 從而創(chuàng)建一個(gè)新的一元函數(shù)對(duì)象 ; C++ 11 棄用 , 建議使用 std::bind 綁定適配器 ;
-
組合適配器 :
- unary_negate 組合適配器 : 將 一元謂詞 的返回值 , 進(jìn)行 邏輯取反 操作 , 得到一個(gè)新的 bool 類型 布爾值 ;
- binary_negate 組合適配器 : 將 二元謂詞 的返回值 , 進(jìn)行 邏輯取反 操作 , 得到一個(gè)新的 bool 類型 布爾值 ;
3、函數(shù)適配器輔助函數(shù)
函數(shù)適配器 的 創(chuàng)建構(gòu)造 需要很復(fù)雜的類型聲明 , 為了方便開發(fā) , C++ 的 STL 標(biāo)準(zhǔn)模板庫 中提供了 " 函數(shù)適配器輔助函數(shù) " , 可以 無需顯示聲明類型 , 就可以 實(shí)現(xiàn) 函數(shù)適配器 的創(chuàng)建 ;
常用的 " 函數(shù)適配器輔助函數(shù) " :
- bind1st 函數(shù) : 輔助構(gòu)造 std::binder1st 綁定適配器 實(shí)例對(duì)象 , 可以 為 二元函數(shù) 第一個(gè)參數(shù) 綁定一個(gè)固定的值 ;
- bind2nd 函數(shù) : 輔助構(gòu)造 std::binder2nd 綁定適配器 實(shí)例對(duì)象 , 可以 為 二元函數(shù) 第二個(gè)參數(shù) 綁定一個(gè)固定的值 ;
- not1 函數(shù) : 輔助構(gòu)造 unary_negate 組合適配器 實(shí)例對(duì)象 , 將 一元謂詞 的返回值 , 進(jìn)行 邏輯取反 操作 ;
- not2 函數(shù) : 輔助構(gòu)造 unary_negate 組合適配器 實(shí)例對(duì)象 , 將 二元謂詞 的返回值 , 進(jìn)行 邏輯取反 操作 ;
二、函數(shù)適配器使用示例 - std::bind2nd 函數(shù)
1、std::bind2nd 函數(shù)原型
std::bind2nd 是一個(gè)函數(shù)適配器 , 它用于 生成一個(gè)新的一元函數(shù)對(duì)象 , 該對(duì)象將給定二元函數(shù)對(duì)象的第二個(gè)參數(shù)綁定到一個(gè)特定的值 ;
std::bind2nd 函數(shù)原型如下 :
template <class Operation>
binder2nd<Operation> bind2nd(const Operation& op, const typename Operation::second_argument_type& value);
-
參數(shù)解析 :
- Operation &op 參數(shù) : 該參數(shù)是 要綁定的二元函數(shù)對(duì)象 , 它必須定義 second_argument_type 作為其第二個(gè)參數(shù)的類型 , 也就是說 第二個(gè)參數(shù)類型 需要 與 本函數(shù)的 value 值類型相同 ;
- value 參數(shù) : 該參數(shù)是 要綁定到二元函數(shù)對(duì)象第二個(gè)參數(shù)的 值 或 變量 ;
-
返回值解析 : 返回一個(gè) binder2nd 類 的對(duì)象 , 該對(duì)象是一個(gè)一元函數(shù)對(duì)象 , 可以像普通函數(shù)對(duì)象一樣被調(diào)用 ;
2、代碼示例 - std::bind2nd 函數(shù)
代碼示例 :
#include "iostream"
using namespace std;
#include <vector>
#include <algorithm>
#include "functional"
int main() {
// 創(chuàng)建一個(gè) set 集合容器
vector<int> myVector;
// 向容器中插入元素
myVector.push_back(9);
myVector.push_back(5);
myVector.push_back(2);
myVector.push_back(7);
myVector.push_back(2);
// 向 foreach 循環(huán)中傳入 Lambda 表達(dá)式
for_each(myVector.begin(), myVector.end(), [](int a) {
std::cout << a << " ";
});
cout << endl;
// 計(jì)算 vector 容器中 , 值為 2 的個(gè)數(shù)
int equal_num = 2;
int count = count_if(myVector.begin(), myVector.end(), bind2nd(equal_to<int>(), equal_num));
cout << "值為 2 的元素個(gè)數(shù) : " << count << endl;
// 控制臺(tái)暫停 , 按任意鍵繼續(xù)向后執(zhí)行
system("pause");
return 0;
};
執(zhí)行結(jié)果 :
9 5 2 7 2
值為 2 的元素個(gè)數(shù) : 2
Press any key to continue . . .
三、函數(shù)適配器使用示例 - std::bind 函數(shù)
1、std::bind 函數(shù)原型
std::bind 函數(shù)適配器 是 C++11 新引入的 函數(shù)適配器 , 可以 將 函數(shù) / 函數(shù)對(duì)象 / 函數(shù)指針 與 其參數(shù)綁定到一起 , 產(chǎn)生一個(gè)新的可調(diào)用函數(shù)對(duì)象 ;
std::bind 函數(shù)適配器 比 std::bind1st 和 std::bind2nd 函數(shù)適配器更加靈活 ,
- std::bind1st 函數(shù)適配器 只能綁定 函數(shù)對(duì)象 第一個(gè)函數(shù)參數(shù) ;
- std::bind2nd 函數(shù)適配器 只能綁定 函數(shù)對(duì)象 第二個(gè)函數(shù)參數(shù) ;
- std::bind 函數(shù)適配器 不僅能綁定 第一第二個(gè)函數(shù)參數(shù) , 還能綁定第三個(gè)第四個(gè)直至第 N 個(gè)函數(shù)參數(shù) , 沒有任何位置限制 ;
std::bind 函數(shù)原型如下 :
template< class Fn, class... Args >
/*unspecified*/ bind( Fn&& fn, Args&&... args );
-
參數(shù)解析 :
- Fn&& fn 參數(shù) : fn 是一個(gè)可調(diào)用對(duì)象 , 可以是 函數(shù) / 函數(shù)指針 / 成員函數(shù) / 成員函數(shù)指針 / 函數(shù)對(duì)象 / 謂詞 等可調(diào)用對(duì)象 ;
- Args&&… args 參數(shù) : 這是一個(gè)可變的參數(shù) , 參數(shù)可以是值 , 引用 或者 占位符 ;
-
返回值解析 : 返回的是一個(gè)未指定的類型 , 這個(gè)類型是一個(gè)函數(shù)對(duì)象 , 可以像普通函數(shù)那樣被調(diào)用 ; 當(dāng)返回的函數(shù)對(duì)象被調(diào)用時(shí) , 它會(huì)用提供的參數(shù)和 std::bind 中的占位符來調(diào)用 fn ;
占位符 是 std::placeholders::_1 , std::placeholders::_2 等值 ;
如果 參數(shù)中是 std::placeholders::_1 占位符 , 表示 第一個(gè) 參數(shù) , 不進(jìn)行修改 , 仍然保持其默認(rèn)值 ; std::placeholders::_2 占位符 , 表示 第二個(gè) 參數(shù) , 不進(jìn)行修改 , 仍然保持其默認(rèn)值 ;
2、代碼示例 - std::bind 函數(shù)
代碼示例 :
#include "iostream"
using namespace std;
#include <vector>
#include <algorithm>
#include "functional"
int main() {
// 創(chuàng)建一個(gè) set 集合容器
vector<int> myVector;
// 向容器中插入元素
myVector.push_back(9);
myVector.push_back(5);
myVector.push_back(2);
myVector.push_back(7);
myVector.push_back(2);
// 向 foreach 循環(huán)中傳入 Lambda 表達(dá)式
for_each(myVector.begin(), myVector.end(), [](int a) {
std::cout << a << " ";
});
cout << endl;
// 計(jì)算 vector 容器中 , 值為 2 的個(gè)數(shù)
int equal_num = 2;
int count = count_if(myVector.begin(), myVector.end(), bind(equal_to<int>(), placeholders::_1, equal_num));
cout << "值為 2 的元素個(gè)數(shù) : " << count << endl;
// 控制臺(tái)暫停 , 按任意鍵繼續(xù)向后執(zhí)行
system("pause");
return 0;
};
執(zhí)行結(jié)果 :文章來源:http://www.zghlxwxcb.cn/news/detail-786211.html
9 5 2 7 2
值為 2 的元素個(gè)數(shù) : 2
Press any key to continue . . .
文章來源地址http://www.zghlxwxcb.cn/news/detail-786211.html
到了這里,關(guān)于【C++】STL 算法 ⑩ ( 函數(shù)適配器 | 函數(shù)適配器概念 | 函數(shù)適配器分類 | 函數(shù)適配器輔助函數(shù) | std::bind2nd 函數(shù)原型及示例 | std::bind 函數(shù)原型及示例 )的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!