C++程序设计第八周---第四课---函数对象

函数对象:

若一个类重载了运算符 “()”, 则该类的对象就成为函数对象。

class CMyAverage { //函数对象类

public:

        double operator() ( int a1, int a2, int a3 ) {

                return (double)(a1 + a2+a3) / 3;

        }

};

CMyAverage average; //函数对象

cout << average(3,2,3); // average.operator()(3,2,3)

输出 2.66667

函数对象的应用:

Dev C++ 中的 Accumulate 源代码1:

template _Tp accumulate(_InputIterator __first, _InputIterator __last, _Tp __init)

{

        for ( ; __first != __last; ++__first)

                __init = __init + *__first;

        return __init;

}

// typename 等价于class

Dev C++ 中的 Accumulate 源代码2:

template<typename _InputIterator, typename _Tp,typename _BinaryOperation>

_Tp accumulate(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOperation __binary_op)

{

        for ( ; __first != __last; ++__first)

                __init = __binary_op(__init, *__first);

                return __init;

}

调用accumulate时, 和__binary_op对应的实参可以是个函数或函数对象

#include<iostream>

#include<vector>

#include<algorithm>

#include<numeric>

#include<functional>

using namespace std;

int sumSquares( int total, int value)

{ return total + value * value; }

template<class T>

void PrintInterval(T first, T last)

{ //输出区间 [first,last )中的元素

        for( ; first != last; ++ first)

                cout << * first << " ";

        cout << endl ;

}

template

class SumPowers {

        private: int power;

        public: SumPowers(int p):power(p) { }

        const T operator() ( const T & total, const T & value)

        { //计算 value的power次方,加到total上

                T v = value;

                for( int i = 0;i < power - 1; ++ i) v = v * value;

                return total + v;

}

};

int main() {

const int SIZE = 10;

int a1[ ] = { 1,2,3,4,5,6,7,8,9,10 };

vector v(a1,a1+SIZE);

cout << "1) ";

PrintInterval(v.begin(),v.end());

int result = accumulate(v.begin(),v.end(),0,SumSquares);

cout << "2) 平方和:" << result << endl;

result = accumulate(v.begin(),v.end(),0,SumPowers(3));

cout << "3) 立方和:" << result << endl;

result = accumulate(v.begin(),v.end(),0,SumPowers(4));

cout (4)); cout << "4) 4次方和:" << result;

return 0;

}

输出:

1) 1 2 3 4 5 6 7 8 9 10

2) 平方和:385

3) 立方和:3025

4) 4次方和:25333

int result = accumulate(v.begin(),v.end(),0,SumSquares);

实例化出:

int accumulate(vector::iterator first,vector::iterator last,

        int init,int ( * op)( int,int))

{

        for ( ; first != last; ++first) init = op(init, *first);

return init;

}

int accumulate(v.begin(),v.end(),0,SumPowers(3));

实例化出: int accumulate(vector::iterator first,vector::iterator last,

        int init, SumPowers op)

{

        for ( ; first != last; ++first) init = op(init, *first);

return init;

}

STL中的函数对象类模板

以下模板可以用来生成函数对象。

equal_to

greater

less

…….

头文件:<functional>

greater 函数对象类模板

template<class T>

struct greater : public binary_function {

        bool operator()(const T& x, const T& y) const {

                return x > y;

        }

};

greater 的应用

list 有两个sort成员函数

void sort();

        将list中的元素按 “ < "将list中的元素按 op 规定的比较方法升序排列。

template<class Compare>

        void sort (Compare op);

将list中的元素按 op 规定的比较方法升序排列。即要比较x,y 大小时,看 op(x,y)的返回值,为true则认为 x小于y.

#include<list>

#include<iostream>

using namespace std;

class MyLess {

        public:

        bool operator()( const int & c1, const int & c2 )

        {

                return (c1 % 10) < (c2 % 10);

        }

};

template

void Print(T first,T last) {

                for( ; first != last ; ++ first ) cout << * first << ",";

}

int main()

{ const int SIZE = 5;

int a[SIZE] = {5,21,14,2,3};

list lst(a,a+SIZE);

lst.sort(MyLess());

Print( lst.begin(),lst.end());

cout ());

lst.sort(greater<int>()); //greater()是个对象

Print( lst.begin(),lst.end());

cout << endl;

return 0;

}

输出:

21,2,3,14,5,

21,14,5,3,2

在STL中使用自定义的“大”,“小”关系

关联容器和STL中许多算法,都是可以用函数或函数对象自定义 比较器的。在自定义了比较器op的情况下,以下三种说法是等价 的:

1) x小于y

2) op(x,y)返回值为true

3) y大于x

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

自动驾驶--小学生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值