标签:
一.函数对象
? 若一个类重载了运算符 “()”,则该类的对象就成为函数对象
1 class CMyAverage { //函数对象类 2 public: 3 double operator() ( int a1, int a2, int a3 ) { 4 return (double)(a1 + a2+a3) / 3; 5 } 6 }; 7 CMyAverage average; //函数对象 8 cout << average(3,2,3); // average.operator()(3,2,3) 9 //输出 2.66667
二.函数对象的应用
1 #include <iostream> 2 #include <vector> 3 #include <algorithm> 4 #include <numeric> 5 #include <functional> 6 using namespace std; 7 8 int SumSquares( int total, int value) 9 { 10 return total + value * value; 11 } 12 13 template <class T> 14 void PrintInterval(T first, T last) 15 { //输出区间[first,last)中的元素 16 for( ; first != last; ++ first) 17 cout << * first << " "; 18 cout << endl; 19 } 20 21 template<class T> 22 class SumPowers 23 { 24 private: 25 int power; 26 public: 27 SumPowers(int p):power(p) { } 28 const T operator() ( const T & total, const T & value) 29 { //计算 value的power次方,加到total上 30 T v = value; 31 for( int i = 0;i < power - 1; ++ i) 32 v = v * value; 33 return total + v; 34 } 35 }; 36 37 int main() 38 { 39 const int SIZE = 10; 40 int a1[ ] = { 1,2,3,4,5,6,7,8,9,10 }; 41 vector<int> v(a1,a1+SIZE); 42 cout << "1) "; PrintInterval(v.begin(),v.end()); 43 int result = accumulate(v.begin(),v.end(),0,SumSquares); 44 cout << "2) 平方和: " << result << endl; 45 result = 46 accumulate(v.begin(),v.end(),0,SumPowers<int>(3)); 47 cout << "3) 立方和: " << result << endl; 48 result = 49 accumulate(v.begin(),v.end(),0,SumPowers<int>(4)); 50 cout << "4) 4次方和: " << result; 51 return 0; 52 } 53 //输出: 54 //1) 1 2 3 4 5 6 7 8 9 10 55 //2) 平方和: 385 56 //3) 立方和: 3025 57 //4) 4次方和: 25333
int result = accumulate(v.begin(),v.end(),0,SumSquares);
实例化出:
int accumulate(vector<int>::iterator first,vector<int>::iterator last,
int init,int ( * op)( int,int))
{
for ( ; first != last; ++first)
init = op(init, *first);
return init;
}
accumulate(v.begin(),v.end(),0,SumPowers<int>(3));
实例化出:
int accumulate(vector<int>::iterator first,vector<int>::iterator last,
int init, SumPowers<int> op)
{
for ( ; first != last; ++first)
init = op(init, *first);
return init;
}
以下模板可以用来生成函数对象。
equal_to
greater
less
…….
头文件: <functional>
1 #include <list> 2 #include <iostream> 3 using namespace std; 4 class MyLess { 5 public: 6 bool operator()( const int & c1, const int & c2 ) 7 { 8 return (c1 % 10) < (c2 % 10); 9 } 10 }; 11 template <class T> 12 void Print(T first,T last) { 13 for( ; first != last ; ++ first ) cout << * first << ","; 14 } 15 16 int main() 17 { 18 const int SIZE = 5; 19 int a[SIZE] = {5,21,14,2,3}; 20 list<int> lst(a,a+SIZE); 21 lst.sort(MyLess()); 22 Print( lst.begin(),lst.end()); 23 cout << endl; 24 lst.sort(greater<int>()); //greater<int>()是个对象 25 Print( lst.begin(),lst.end()); 26 cout << endl; 27 return 0; 28 } 29 //输出: 30 //21,2,3,14,5, 31 //21,14,5,3,2,
1 #include <iostream> 2 #include <iterator> 3 using namespace std; 4 class MyLess { 5 public: 6 bool operator() (int a1,int a2) { 7 if( ( a1 % 10 ) < (a2%10) ) 8 return true; 9 else 10 return false; 11 } 12 }; 13 14 template <class T, class Pred> 15 T MyMax( T first, T last, Pred myless) 16 { 17 T tmpMax = first; 18 for(; first != last; ++ first) 19 if( myless( * tmpMax,* first)) 20 tmpMax = first; 21 return tmpMax; 22 }; 23 24 bool MyCompare(int a1,int a2) 25 { 26 if( ( a1 % 10 ) < (a2%10) ) 27 return false; 28 else 29 return true; 30 } 31 32 int main() 33 { 34 int a[] = {35,7,13,19,12}; 35 cout << *MyMax(a,a+5,MyLess()) 36 << endl; 37 cout << *MyMax(a,a+5,MyCompare) 38 << endl; 39 return 0; 40 } 41 42 //输出: 43 //19 44 //12
greater 函数对象类模板
template<class T>
struct greater : public binary_function<T, T, bool> {
bool operator()(const T& x, const T& y) const {
return x > y;
}
};
greater 函数对象类模板
template<class T>
struct greater : public binary_function<T, T, bool> {
bool operator()(const T& x, const T& y) const {
return x > y;
}
};
13
greater 的应用
list 有两个sort成员函数
? void sort();
将list中的元素按 “ <” 规定的比较方法升序排列。
? template <class Compare>
void sort (Compare op);
将list中的元素按 op 规定的比较方法升序排列。即要比较x,y
大小时,看 op(x,y)的返回值,为true则认为 x小于y
14
#include <list>
#include <iostream>
using namespace std;
class MyLess {
public:
bool operator()( const int & c1, const int & c2 )
{