C++中的函数指针和函数对象

本文详细介绍了C++中函数对象和函数指针的使用方法,包括类实现函数对象、模板函数指针、附加数据的函数对象以及根据特殊条件计数的函数对象实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

//c++ function object
// class Add  
// {  
// public:  
//  int operator()(int a, int b)  
//  {  
//      return a + b;  
//  }  
// };  

// int main(int argc, char *argv[])
// {
//  Add add; // 定义函数对象  
//  cout << add(3,2); // 5 
//  return 0;
// }


//function pinter
// int AddTunc(int a, int b)
// {
//  return a+b;
// }

// typedef int (*FunctionPointer)(int a, int b);
// //or like this, typedef int (*FunctionPointer)(int, int );


// int main(int argc, char* argv[])
// {
//  FunctionPointer add = &AddTunc;
//  //or like this , FunctionPointer add = AddTunc;
//  cout<<add(2,8);
// }

//  //function object with additional data
// class IsLess
// {
// public: 
//  IsLess(int a) : value(a)
//  {}

//  bool operator()(int value)
//  {
//      return this->value < value;
//      //return value < value1;
//  }
// private:
//  int value;  
// };

// int main(int argc, char* argv[])
// {
//  IsLess less(10);
//  cout<<less(9)<<" "<<less(11)<<"\n";

//  const int SIZE = 5;  
//  int array[SIZE] = { 50, 30, 9, 7, 60};  
//  // 找到小于数组array中小于10的第一个数的位置  
//  int * pa = find_if(array, array + SIZE, IsLess(50)); 
//  // pa point to the number less bigger than 50, or pa pointe to the end
//  if(array + SIZE != pa )
//      cout<<"*pa= "<<*pa<<"\n";  

//  pa = find_if(array, array + SIZE, IsLess(60)); 
//  // pa point to the number less bigger than 60, or pa pointe to the end
//  if(array + SIZE != pa )
//      cout<<"*pa= "<<*pa<<"\n";  
// }



//function: count the number  according  to  a special condition 
// template<typename FUNC> 
// int count_n(int* array, int size, FUNC func)  
// {  
//  int count = 0;  
//  for(int i = 0; i < size; ++i){
//      if(func(array[i]))  
//          count ++;       
//  }  

//  return count;  
// } 

// class IsLess
// {
// public: 
//  IsLess(int a) : value(a)
//  {}

//  bool operator()(int value)
//  {
//      return this->value < value;
//      //return value < value1;
//  }
// private:
//  int value;  
// };

// bool less10(int value)  
// {  
//  return 10 < value;  
// }  
// int main()
// {
//  const int SIZE = 5;  
//  int array[SIZE] = { 50, 30, 9, 7, 20};  
//  cout << count_n(array, SIZE, IsLess(10))<<"\n"; // 3  
//  cout << count_n(array, SIZE, less10); // 3  
// }



// template<typename O> 
// class memfun  
// {  
// public:  
//  memfun(void(O::*f)(const char*), O* o): pFunc(f), pObj(o){}  
//  void operator()(const char* name)  
//  {  
//      (pObj->*pFunc)(name);  
//  }  
// private:  
//  void(O::*pFunc)(const char*);  
//  O* pObj;  
// };

// class A  
// {  
// public:  
//  void doIt(const char* name)  
//  { 
//      cout << "Hello " << name << "!";
//  }  
// };  


// int main(int argc, char *argv[])
// {
//  A a;  
//  memfun<A> call(&A::doIt, &a); // 保存 a::doIt指针以便调用  
//  call("Kitty"); // 输出 Hello Kitty!   
// }


引用:http://blog.youkuaiyun.com/bonchoix/article/details/8050627
http://www.cnblogs.com/lvpengms/archive/2011/02/21/1960078.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值