1.函數對象
1.1 概念
重載函數調用操作符的類,其對象被稱為函數對象
函數對象使用重載的()時,行為類似函數調用,也成為仿函數
本質:函數對象(仿函數)是一個類,不是一個函數
1.2 函數對象的使用
在使用時可以像普通函數一樣調用,可以有參數和返回值
函數對象可以有自己的狀態
函數對象可以作為參數傳遞
#include <iostream>
#include <vector>using namespace std;class Myadd{public:int operator()(int x,int y){return x+y;}
};void test1(){Myadd ma;cout<<ma(10,10)<<endl;
}//可以有自己的狀態
class MyPrint{public: MyPrint(){count=0;}void operator()(string test){cout<<test<<endl;count++;}int count;
};void test2(){MyPrint mp;mp("hello world");mp("hello world");mp("hello world");cout<<"調用了"<<mp.count<<"次"<<endl;
}// 可以作為參數傳遞
void DoPrint(MyPrint &mp,string test){mp(test);
}void test3(){MyPrint mp;DoPrint(mp,"hello world");
}int main()
{test1();test2();test3();system("pause");return 0;
}
?2.謂詞
2.1 概念:
返回bool類型的仿函數稱為謂詞
如果operator()接收一個參數成為一元謂詞
如果operator()接收兩個參數成為二元謂詞
2.2 一元謂詞
一元謂詞(Unary Predicate)?是一個接受一個參數并返回布爾值(true
?或?false
)?的函數對象或函數。
其核心作用是:對單個輸入值進行條件判斷,返回是否滿足特定條件。
#include <iostream>
#include <vector>
#include<algorithm>using namespace std;struct CreateFive{bool operator()(int x){return x>5;}
};void test(){vector<int> v;for(int i=0;i<10;i++){v.push_back(i);}vector<int>::iterator it = find_if(v.begin(),v.end(),CreateFive());// 查找滿足CreateFive的元素,指向第一個滿足條件的位置if (it==v.end()){cout<<"沒有大于5的元素"<<endl;}else{cout<<*it<<endl;//只輸出一個6}
}int main()
{test();system("pause");return 0;
}
2.3 二元謂詞
二元謂詞(Binary Predicate)?是一個接受兩個參數并返回布爾值(true
?或?false
)?的函數對象或函數。
其核心作用是:對兩個輸入值進行條件判斷,返回是否滿足特定關系。
#include <iostream>
#include <vector>
#include<algorithm>
using namespace std;class MyCompare{public:bool operator()(int v1,int v2){return v1>v2;}// 這個函數接受兩個參數(v1 和 v2),返回一個布爾值(true 或 false),// 表示 v1 是否大于 v2。
};void test(){vector<int> v;v.push_back(10);v.push_back(20);v.push_back(30);v.push_back(40);v.push_back(50);sort(v.begin(),v.end());for(vector<int>::iterator it=v.begin();it!=v.end();it++){cout<<*it<<" "; }cout<<endl;//改變排序順序sort(v.begin(),v.end(),MyCompare());for(vector<int>::iterator it=v.begin();it!=v.end();it++){cout<<*it<<" "; }cout<<endl;}int main()
{test();system("pause");return 0;
}
3.內建函數對象
3.1意義
STL內建了一些函數對象
需要在頭文件#include<functional>
3.2算數仿函數
negate是一元運算,其他是二元運算,所以需要重載()
template<class T> T plus<T>;//加法
template<class T> T minus<T>;//減法
template<class T> T multiplies<T>;//乘法
template<class T> T divides<T>;//除法
template<class T> T modulus<T>;//取余
template<class T> T negate<T>;//取反
#include <iostream>
#include <functional>using namespace std;//negate
void Negate(){negate<int> n;cout<<n(10)<<endl;
}void Add(){plus<int> p;cout<<p(10,20)<<endl;
}int main()
{Negate();Add();system("pause");return 0;
}
3.3關系仿函數
實現關系對比
template<class T> bool equal_to<T>// 等號
template<class T> bool not_equal_to<T>// 不等號
template<class T> bool greater<T>// 大于
template<class T> bool less<T>// 小于
template<class T> bool greater_equal<T>// 大于等于
template<class T> bool less_equal<T>// 小于等于
#include <iostream>
#include <vector>
#include<functional>
#include<algorithm>using namespace std;class MyCompare{public:bool operator()(int x,int y){return x>y;}
};void test(){vector<int> v;v.push_back(10);v.push_back(80);v.push_back(30);v.push_back(50);v.push_back(70);for(vector<int>::iterator it=v.begin();it!=v.end();it++){cout<<*it<<" ";}cout<<endl;sort(v.begin(),v.end(),MyCompare());//STL內建仿函數sort(v.begin(),v.end(),greater<int>());for(vector<int>::iterator it=v.begin();it!=v.end();it++){cout<<*it<<" ";}cout<<endl;
}int main()
{test();system("pause");return 0;
}
3.4邏輯仿函數
template<class T> bool logical_and<T>// 邏輯與
template<class T> bool logical_or<T>// 邏輯或
template<class T> bool logical_not<T>// 邏輯非
#include <iostream>
#include <vector>
#include<functional>
#include<algorithm>using namespace std;void test(){vector<bool> v;v.push_back(true);v.push_back(false);v.push_back(true);v.push_back(false);for(auto it=v.begin();it!=v.end();it++){cout<<*it<<" ";}cout<<endl;vector<bool> v2;v2.resize(v.size());//將v2的大小設置為v的大小transform(v.begin(),v.end(),v2.begin(),logical_not<bool>());//遍歷v,從v2的begin開始,將v的值取反,然后賦值給v2for(auto it=v2.begin();it!=v2.end();it++){cout<<*it<<" ";}cout<<endl;
}int main()
{test();system("pause");return 0;
}