一、函數(shù)適配器示例 - 函數(shù)適配器正常用法
1、modulus 函數(shù)對象 - 取模運算
在 <functional> 頭文件 中 , 預(yù)定義了 modulus 函數(shù)對象 , 這是一個 二元函數(shù)對象 , 在該函數(shù)對象類中 , 重寫了 函數(shù)調(diào)用操作符 函數(shù) operator() , 該 預(yù)定義函數(shù)對象 代碼如下 :
// STRUCT TEMPLATE modulus
template <class _Ty = void>
struct modulus {
_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ù)對象 定義了 模板參數(shù) template <class _Ty = void> , _Ty 泛型的默認(rèn)參數(shù)是 void , 即 如果 不指定 模板參數(shù) , _Ty 泛型就是 void 類型 , 一般情況下使用 int 類型 進(jìn)行取模運算 ;
在 modulus 函數(shù)對象 中 , 重載 函數(shù)調(diào)用操作符 函數(shù) 是最核心的函數(shù) , 在該函數(shù)中 , 將第一個參數(shù) const _Ty& _Left 與 第二個參數(shù) const _Ty& _Right 進(jìn)行取模運算 , 返回 模運算 的結(jié)果 ;
代碼示例 :
#include "iostream"
using namespace std;
#include "functional"
int main() {
// 創(chuàng)建函數(shù)對象
modulus<int> mod;
// 調(diào)用函數(shù)對象
int result = mod(5, 2);
// 打印執(zhí)行結(jié)果
cout << "result = " << result << endl;
// 控制臺暫停 , 按任意鍵繼續(xù)向后執(zhí)行
system("pause");
return 0;
};
執(zhí)行結(jié)果 :
2、std::count_if 函數(shù)原型
std::count_if 函數(shù) 是 C++ 標(biāo)準(zhǔn)庫算法 , 該 函數(shù) 的作用是 計算范圍內(nèi)滿足特定條件的元素的數(shù)量 , 該函數(shù) 接受 一個迭代器范圍 和 謂詞函數(shù) ;
注意 : 迭代器范圍 的 起始迭代器 ~ 終止迭代器 是一個 前閉后開區(qū)間
std::count_if 算法的 函數(shù)原型 如下 :
// FUNCTION TEMPLATE count_if
template <class _InIt, class _Pr>
_NODISCARD _Iter_diff_t<_InIt> count_if(_InIt _First, _InIt _Last, _Pr _Pred) { // count elements satisfying _Pred
_Adl_verify_range(_First, _Last);
auto _UFirst = _Get_unwrapped(_First);
const auto _ULast = _Get_unwrapped(_Last);
_Iter_diff_t<_InIt> _Count = 0;
for (; _UFirst != _ULast; ++_UFirst) {
if (_Pred(*_UFirst)) {
++_Count;
}
}
return _Count;
}
- _InIt _First 參數(shù) : 迭代器范圍的 起始迭代器 , 包括本迭代器指向的元素 ;
- _InIt _Last 參數(shù) : 迭代器范圍的 終止迭代器 , 不包括本迭代器指向的元素 ;
- _Pr _Pred 參數(shù) : 謂詞函數(shù) ;
3、代碼示例 - 使用 函數(shù)適配器 綁定函數(shù)對象參數(shù)
在下面的代碼中 , 將 myVector 單端數(shù)組 容器 中的元素 設(shè)置給 modulus 函數(shù)對象的 第一個參數(shù) , 將 equal_num 變量設(shè)置為 該 modulus 函數(shù)對象的 第二個參數(shù) , 然后依次遍歷 myVector 單端數(shù)組 容器 將每個元素 與 equal_num 進(jìn)行取模運算 ;
// 計算 vector 容器中 , 值為 2 的個數(shù)
int equal_num = 2;
// 取模運算 , 模 2 返回值 1 就是奇數(shù) , 返回值 0 就是偶數(shù)
int count = count_if(myVector.begin(), myVector.end(), bind2nd(modulus<int>(), 2));
代碼示例 :
#include "iostream"
using namespace std;
#include <vector>
#include <algorithm>
#include "functional"
int main() {
// 創(chuàng)建一個 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;
// 計算 vector 容器中 , 值為 2 的個數(shù)
int equal_num = 2;
// 取模運算 , 模 2 返回值 1 就是奇數(shù) , 返回值 0 就是偶數(shù)
int count = count_if(myVector.begin(), myVector.end(), bind2nd(modulus<int>(), 2));
cout << "值奇數(shù)元素個數(shù) : " << count << endl;
// 控制臺暫停 , 按任意鍵繼續(xù)向后執(zhí)行
system("pause");
return 0;
};
二、函數(shù)適配器示例 - 函數(shù)適配器嵌套用法
1、std::not1 函數(shù)原型
std::not1 是 預(yù)定義的 函數(shù)適配器 函數(shù) , 該 函數(shù) 接收一個 一員函數(shù)對象 , 返回新的 一元函數(shù)對象 , 返回的 一元函數(shù)對象 是對輸入的 一元函數(shù)對象 的 結(jié)果 進(jìn)行 邏輯非 運算 ;
std::not1 函數(shù)原型如下 :
template <class UnaryPredicate>
unary_negate<UnaryPredicate> not1(UnaryPredicate pred);
- UnaryPredicate pred 參數(shù) : 一元謂詞 , 也就是 接受單個參數(shù) 并返回布爾值的可調(diào)用對象 ;
- unary_negate<UnaryPredicate> 類型返回值 : 返回值 是 封裝了 UnaryPredicate 并提供了一個 operator() 成員函數(shù) 的 一元謂詞 , 該函數(shù)對 UnaryPredicate 的結(jié)果取反 ;
std::not1 可以 與 std::bind1st 或 std::bind2nd 嵌套使用 , 創(chuàng)建更復(fù)雜的謂詞 ;
2、代碼示例 - 函數(shù)適配器嵌套用法
核心代碼如下 :
// 計算 vector 容器中 , 值為 2 的個數(shù)
int equal_num = 2;
// 取模運算 , 模 2 返回值 1 就是奇數(shù) , 返回值 0 就是偶數(shù)
// not1 將其取反 也就是獲取的是 非奇數(shù) 個數(shù)
int count = count_if(myVector.begin(), myVector.end(), not1(bind2nd(modulus<int>(), 2)));
下面的代碼中 , modulus 是一個二元函數(shù)對象 , 返回 0 或 1 可以當(dāng)做 二元謂詞 ;
bind2nd(modulus<int>(), 2) 將 二元謂詞 中的 第二個元素進(jìn)行了綁定 , 只需要接收一個參數(shù) , 變成了 一元謂詞 ;
not1(bind2nd(modulus<int>(), 2)) 將 上述 一元謂詞 取反 , 得到一個新的一元謂詞 ;
代碼示例 :
#include "iostream"
using namespace std;
#include <vector>
#include <algorithm>
#include "functional"
int main() {
// 創(chuàng)建一個 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;
// 計算 vector 容器中 , 值為 2 的個數(shù)
int equal_num = 2;
// 取模運算 , 模 2 返回值 1 就是奇數(shù) , 返回值 0 就是偶數(shù)
// not1 將其取反 也就是獲取的是 非奇數(shù) 個數(shù)
int count = count_if(myVector.begin(), myVector.end(), not1(bind2nd(modulus<int>(), 2)));
cout << "偶數(shù)元素個數(shù) : " << count << endl;
// 控制臺暫停 , 按任意鍵繼續(xù)向后執(zhí)行
system("pause");
return 0;
};
執(zhí)行結(jié)果 :文章來源:http://www.zghlxwxcb.cn/news/detail-796491.html
9 5 2 7 2
偶數(shù)元素個數(shù) : 2
請按任意鍵繼續(xù). . .
文章來源地址http://www.zghlxwxcb.cn/news/detail-796491.html
到了這里,關(guān)于【C++】STL 算法 ? ( 函數(shù)適配器嵌套用法 | modulus 函數(shù)對象 - 取模運算 | std::count_if 函數(shù)原型 | std::not1 函數(shù)原型 )的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!