一、預(yù)定義函數(shù)對象
1、預(yù)定義函數(shù)對象概念
C++ 的 標(biāo)準(zhǔn)模板庫 ( STL , Standard Template Library ) 中 , 預(yù)定義了一系列的 " 函數(shù)對象 “ , 又稱為 ” 仿函數(shù) Functors " ;
這些 " 預(yù)定義函數(shù)對象 " 在 STL 算法中 , 可以 作為 算法的參數(shù) , 定制某些參數(shù)的行為 , 如 :
- for_each 遍歷算法中 , 傳入 " 一元函數(shù)對象 " , 用于執(zhí)行單個(gè)元素的遍歷操作 ;
- find_if 查找算法中 , 傳入 " 一元謂詞 " , 用于判定某個(gè)元素是否符合查找規(guī)則 ;
- transform 變換算法中 , 傳入 " 二元函數(shù)對象 " , 用于將 2 個(gè)范圍的元素進(jìn)行變換操作 ;
- sort 排序算法中 , 傳入 " 二元謂詞 " , 用于判定 2 個(gè)元素之間的 排序規(guī)則 ;
2、預(yù)定義函數(shù)對象組成
預(yù)定義 函數(shù)對象 , 是由 調(diào)用操作符 和 T 泛型類型 組合使用的 , 以 plus<T>
為例 ,
- plus 表示這是 " 預(yù)定義 算術(shù)運(yùn)算符 函數(shù)對象 " ;
-
T 泛型類型 表示 該函數(shù)對象 執(zhí)行的是 什么類型 的操作 ,
- 如果 T 為 int , 則表示 兩個(gè) int 整型值 進(jìn)行加法操作 ;
- 如果 T 為 string , 則表示 兩個(gè) string 字符串 進(jìn)行加法操作 ;
二、預(yù)定義函數(shù)對象分類
預(yù)定義函數(shù)對象 分為 如下幾類 :
- 預(yù)定義 算術(shù)運(yùn)算符 函數(shù)對象
- 預(yù)定義 比較運(yùn)算符 函數(shù)對象
- 預(yù)定義 邏輯運(yùn)算符 函數(shù)對象
1、預(yù)定義 算術(shù)運(yùn)算符 函數(shù)對象
預(yù)定義 算術(shù)運(yùn)算符 函數(shù)對象 :
- plus<T> : 加法運(yùn)算 ;
- minus<T> : 減法運(yùn)算 ;
- multiplies<T> : 乘法運(yùn)算 ;
- divides<T> : 除法運(yùn)算 ;
- modulus<T> : 取模運(yùn)算 ;
上述 " 預(yù)定義 算術(shù)運(yùn)算符 函數(shù)對象 " 都是 二元函數(shù)對象 , 通常用于 transform 變換算法 , accumulate 累加和算法 , 等算法中 ;
2、預(yù)定義 比較運(yùn)算符 函數(shù)對象
預(yù)定義 比較運(yùn)算符 函數(shù)對象 :
- equal_to<T> : 判斷兩個(gè)值是否相等 ;
- not_equal_to<T> : 判斷兩個(gè)值是否不相等 ;
- greater<T> : 判斷第一個(gè)值是否大于第二個(gè)值 ;
- less<T> : 判斷第一個(gè)值是否小于第二個(gè)值 ;
- greater_equal<T> : 判斷第一個(gè)值是否大于或等于第二個(gè)值 ;
- less_equal<T> : 判斷第一個(gè)值是否小于或等于第二個(gè)值 ;
這些 函數(shù)對象 都是 二元謂詞 , 通常用于 sort 排序算法 , find_if 查找算法 等算法中 ;
3、預(yù)定義 邏輯運(yùn)算符 函數(shù)對象
預(yù)定義 邏輯運(yùn)算符 函數(shù)對象 :
- logical_and<T> : 邏輯與運(yùn)算 ;
- logical_or<T> : 邏輯或運(yùn)算 ;
- logical_not<T> : 執(zhí)行邏輯非運(yùn)算 ; logical_not 函數(shù)對象 通常不直接用于算法中 , 而是用于構(gòu)造其他函數(shù)對象 ;
三、代碼示例 - plus 函數(shù)對象使用
1、plus 函數(shù)對象
plus 函數(shù)對象原型如下 :
// STRUCT TEMPLATE plus
template <class _Ty = void>
struct plus {
_CXX17_DEPRECATE_ADAPTOR_TYPEDEFS typedef _Ty first_argument_type;
_CXX17_DEPRECATE_ADAPTOR_TYPEDEFS typedef _Ty second_argument_type;
_CXX17_DEPRECATE_ADAPTOR_TYPEDEFS typedef _Ty result_type;
constexpr _Ty operator()(const _Ty& _Left, const _Ty& _Right) const {
return _Left + _Right;
}
};
其重載的 " 函數(shù)調(diào)用操作符 () " 函數(shù)原型如下 :
constexpr _Ty operator()(const _Ty& _Left, const _Ty& _Right) const {
return _Left + _Right;
}
上述函數(shù) , 接收 2 個(gè) T 類型函數(shù) , 將這兩個(gè)函數(shù)相加 , 并返回相加的結(jié)果 ;
2、代碼示例
代碼示例 :
#include "iostream"
using namespace std;
#include <vector>
#include <algorithm>
#include "functional"
int main() {
// I . int 類型相加
// 創(chuàng)建 plus 函數(shù)對象實(shí)例
plus<int> intPlus;
// 定義傳入 plus 函數(shù)對象的參數(shù)
int a = 222;
int b = 666;
// 執(zhí)行 plus 函數(shù)對象
int c = intPlus(a, b);
// 打印 plus 函數(shù)對象執(zhí)行結(jié)果
cout << "c = " << c << endl;
// II . string 類型相加
// 創(chuàng)建 plus 函數(shù)對象實(shí)例
plus<string> stringPlus;
// 定義傳入 plus 函數(shù)對象的參數(shù)
string s1 = "Hello";
string s2 = "World";
// 執(zhí)行 plus 函數(shù)對象
string s = stringPlus(s1, s2);
// 打印 plus 函數(shù)對象執(zhí)行結(jié)果
cout << "s = " << s << endl;
// 控制臺暫停 , 按任意鍵繼續(xù)向后執(zhí)行
system("pause");
return 0;
};
3、執(zhí)行結(jié)果
執(zhí)行結(jié)果 :
c = 888
s = HelloWorld
請按任意鍵繼續(xù). . .文章來源:http://www.zghlxwxcb.cn/news/detail-793217.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-793217.html
到了這里,關(guān)于【C++】STL 算法 ⑧ ( 預(yù)定義函數(shù)對象 | 預(yù)定義函數(shù)對象組成 | 預(yù)定義函數(shù)對象分類 | 預(yù)定義 算術(shù)運(yùn)算符 函數(shù)對象 | 預(yù)定義 比較運(yùn)算符 函數(shù)對象 | 預(yù)定義 邏輯運(yùn)算符 函數(shù)對象 )的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!