函数对象(functor)

 

简单地说,如果一个类重载了调用操作符(),那么这个类的对象就是所谓的函数对象。

 

一、 关于操作符重载的简单说明

1.     调用操作符()的重载必须以成员函数的形式出现,另外还必须以成员函数形式重载的操作符有:=[ ]->

2.     IO操作符<<>>的重载则必须以友元函数的形式出现。

3.     下面4个符号是不能被重载的:

:: .* . ?:

 

二、 for_each函数的简单说明

0.      for_each<algorithm>中定义

1.      函数原型:

template <class InputIterator, class Function>

Function for_each (InputIterator first, InputIterator last, Function f);

 

2.     参数:

first, last

Input iterators to the initial and final positions in a sequence. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.

 

f

Unary function taking an element in the range as argument. This can either be a pointer to a function or an object whose class overloads operator().

Its return value, if any, is ignored.

 

3.     返回值:

f的返回值一样

 

三、 示例代码

// 函数对象和for_each的用法

#include <iostream>

#include <algorithm>

#include <string>

#include <vector>

using namespace std;

 

class Student

{

public:

         Student(const string name, const int age) : name(name), age(age)

         {}

 

         //~Student()

         //{

         //       cout << name << "\tdeleted." << endl;

         //}

 

public:

         const string get_name() const

         {

                   return name;

         }

 

         const int get_age() const

         {

                   return age;

         }

 

private:

         string name;

         int age;

};

 

// 普通函数。global callable function

template<typename T>

void myfun(const T stu)

{

         cout << "Name: " << stu.get_name() << "\tage: " << stu.get_age() << endl;

}

 

// myclass的对象就是一个函数对象,因为myclass重载了调用操作符()

template<typename T>

class myclass

{

public:

         void operator()(const T);

};

 

template<typename T>

void myclass<T>::operator ()(const T stu )

{

         cout << "Name: " << stu.get_name() << "\tage: " << stu.get_age() << endl;

}

 

int main(void)

{

         vector<Student> myvector;

         Student stu01("Andrew", 7);

         Student stu02("Bob", 7);

         Student stu03("Chris", 8);

         Student stu04("Dudley", 7);

         Student stu05("Ely", 8);

         Student stu06("Flora", 9);

         Student stu07("Greg", 8);

         Student stu08("Howard", 9);

         Student stu09("Iris", 10);

 

         myvector.push_back(stu01);

         myvector.push_back(stu02);

         myvector.push_back(stu03);

         myvector.push_back(stu04);

         myvector.push_back(stu05);

         myvector.push_back(stu06);

         myvector.push_back(stu07);

         myvector.push_back(stu08);

         myvector.push_back(stu09);

 

         // use the general function

         cout << "myvector contains: " << endl;

         for_each(myvector.begin(), myvector.end(), myfun<Student>);

 

         cout << "--------------------------------" << endl;

         // or use the function object

         myclass<Student> mc;

         cout << "myvector contains: " << endl;

         for_each(myvector.begin(), myvector.end(), mc);

 

         cout << "--------------------------------" << endl;

         // 调用函数myfun

         myfun<Student>(stu01);

 

         // 调用函数对象mc,和调用普通函数一样

         mc(stu01);

 

         return 0;

}

运行结果:

myvector contains:

Name: Andrew   age: 7

Name: Bob          age: 7

Name: Chris       age: 8

Name: Dudley    age: 7

Name: Ely           age: 8

Name: Flora       age: 9

Name: Greg       age: 8

Name: Howard age: 9

Name: Iris          age: 10

--------------------------------

myvector contains:

Name: Andrew  age: 7

Name: Bob        age: 7

Name: Chris      age: 8

Name: Dudley   age: 7

Name: Ely          age: 8

Name: Flora      age: 9

Name: Greg      age: 8

Name: Howard age: 9

Name: Iris          age:10

--------------------------------

Name: Andrew  age: 7

Name: Andrew  age: 7

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值