日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

STL之函数适配器

發(fā)布時(shí)間:2023/12/19 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 STL之函数适配器 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1.理論知識(shí)



2.常用函數(shù)適配器

標(biāo)準(zhǔn)庫(kù)提供一組函數(shù)適配器,用來特殊化或者擴(kuò)展一元和二元函數(shù)對(duì)象。常用適配器是:

1綁定器(binder):

binder通過把二元函數(shù)對(duì)象的一個(gè)實(shí)參綁定到一個(gè)特殊的值上,將其轉(zhuǎn)換成一元函數(shù)對(duì)象。C++標(biāo)準(zhǔn)庫(kù)提供兩種預(yù)定義的binder適配器:bind1st和bind2nd,前者把值綁定到二元函數(shù)對(duì)象的第一個(gè)實(shí)參上,后者綁定在第二個(gè)實(shí)參上。

2取反器(negator) :

negator是一個(gè)將函數(shù)對(duì)象的值翻轉(zhuǎn)的函數(shù)適配器。標(biāo)準(zhǔn)庫(kù)提供兩個(gè)預(yù)定義的ngeator適配器:not1翻轉(zhuǎn)預(yù)定義一元函數(shù)對(duì)象的真值,而not2翻轉(zhuǎn)二元謂詞函數(shù)的真值。

常用函數(shù)適配器列表如下:

bind1st(op, value) bind2nd(op, value) not1(op) not2(op) mem_fun_ref(op) mem_fun(op) ptr_fun(op)

3.典型案例

class IsGreat { public:IsGreat(int i){m_num = i;}bool operator()(int &num){if (num > m_num){return true;}return false;} protected: private:int m_num; };void main43() {vector<int> v1;for (int i=0; i<5; i++){v1.push_back(i+1);}for (vector<int>::iterator it = v1.begin(); it!=v1.end(); it ++){cout << *it << " " ;}int num1 = count(v1.begin(), v1.end(), 3);cout << "num1:" << num1 << endl;//通過謂詞求大于2的個(gè)數(shù)int num2 = count_if(v1.begin(), v1.end(), IsGreat(2)); cout << "num2:" << num2 << endl;//通過預(yù)定義函數(shù)對(duì)象求大于2的個(gè)數(shù) greater<int>() 有2個(gè)參數(shù) // param > 2int num3 = count_if(v1.begin(), v1.end(), bind2nd(greater<int>(), 2 ) );cout << "num3:" << num3 << endl;//取模 能被2整除的數(shù) 求奇數(shù)int num4 = count_if(v1.begin(), v1.end(), bind2nd(modulus <int>(), 2 ) ); cout << "奇數(shù)num4:" << num4 << endl;int num5 = count_if(v1.begin(), v1.end(), not1( bind2nd(modulus <int>(), 2 ) ) ); cout << "偶數(shù)num5:" << num5 << endl;return ; }

4.預(yù)定義函數(shù)對(duì)象和適配器案例代碼

#include <iostream> using namespace std;#include "string" #include <vector> #include <list> #include "set" #include <algorithm> #include "functional"//plus<int> 預(yù)定義好的函數(shù)對(duì)象 能實(shí)現(xiàn)不同類型的數(shù)據(jù)的 + 運(yùn)算 //實(shí)現(xiàn)了 數(shù)據(jù)類型 和算法的分離 ===》通過函數(shù)對(duì)象技術(shù)實(shí)現(xiàn)的。。。。//思考:怎么樣知道 plus<type> 是兩個(gè)參數(shù) void main21() {/*template<class _Ty>struct plus: public binary_function<_Ty, _Ty, _Ty>{ // functor for operator+_Ty operator()(const _Ty& _Left, const _Ty& _Right) const{ // apply operator+ to operandsreturn (_Left + _Right);}};*/plus<int> intAdd;int x = 10; int y = 20;int z = intAdd(x, y); // x + y cout << "z:" << z << endl;plus<string> stringAdd;string s1 = "aaa";string s2 = "bbb";string s3 = stringAdd(s1, s2);cout << "s3:" << s3 << endl;vector<string> v1;v1.push_back("bbb");v1.push_back("aaa");v1.push_back("ccc");v1.push_back("zzz");v1.push_back("ccc");v1.push_back("ccc");/*template<class _Ty>struct greater: public binary_function<_Ty, _Ty, bool>{ // functor for operator>bool operator()(const _Ty& _Left, const _Ty& _Right) const{ // apply operator> to operandsreturn (_Left > _Right);}};*/sort(v1.begin(), v1.end(), greater<string>() );for (vector<string>::iterator it=v1.begin(); it!=v1.end(); it++){cout << *it << endl;}//求 ccc 出現(xiàn)的個(gè)數(shù)string sc = "ccc";//equal_to<string>() 有兩個(gè)參數(shù) left參數(shù)來自容器,right參數(shù)來自sc//bind2nd函數(shù)適配器 :把預(yù)定義函數(shù)對(duì)象 和 第二個(gè)參數(shù)進(jìn)行綁定int num = count_if(v1.begin(), v1.end(), bind2nd(equal_to<string>(), sc) );cout << "num: " << num << endl; }class IsGreat { public:IsGreat(int i){m_num = i;}bool operator()(int &num){if (num > m_num){return true;}return false;} private:int m_num; };void main22() {vector<int> v1;for (int i=0; i<10; i++){v1.push_back(i+1);}for (vector<int>::iterator it=v1.begin(); it!=v1.end(); it++ ){cout << *it << " ";}cout << endl;int num1 = count(v1.begin(), v1.end(), 3);cout << "num1:" << num1 <<endl;//通過 謂詞 求大于2 的個(gè)數(shù)int num2 = count_if(v1.begin(), v1.end(), IsGreat(2));cout << "num2:" << num2 <<endl;/*template<class _Ty>struct greater: public binary_function<_Ty, _Ty, bool>{ // functor for operator>bool operator()(const _Ty& _Left, const _Ty& _Right) const{ // apply operator> to operandsreturn (_Left > _Right);}};*///通過 預(yù)定義的函數(shù)對(duì)象 求大于2 的個(gè)數(shù)//greater<int>() 有兩個(gè)參數(shù) 左參數(shù)來自容器的元素 ,右參數(shù)固定成2 (通過bind2nd做的)int num3 = count_if(v1.begin(), v1.end(), bind2nd (greater<int>(), 2) );cout << "num3:" << num3 <<endl;//求 奇數(shù)的個(gè)數(shù)int num4 = count_if(v1.begin(), v1.end(), bind2nd (modulus<int>(), 2) );cout << "奇數(shù)的個(gè)數(shù)num4:" << num4 <<endl;//求 偶數(shù)的個(gè)數(shù) 取反器(negator) int num5 = count_if(v1.begin(), v1.end(), not1( bind2nd (modulus<int>(), 2) ) );cout << "偶數(shù)的個(gè)數(shù) num5:" << num5 <<endl;} void main2222() {//main21();main22(); //函數(shù)適配器綜合案例cout<<"hello..."<<endl;system("pause");return ; }

總結(jié)

以上是生活随笔為你收集整理的STL之函数适配器的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。