C++ 仿函数

首先介绍一个简单的例子

问题描述:

struct stRecordItem
{
char szname[_MAX_NAME_LEN_]; //物品名称
int dwBaseID; //基本ID
 int btItemLvl; //品阶
int ncount; //数量
__int64 i64Time; //记录这条信息的时间,如果有叠加的,按后一个的时间
};

list<stRecordItem> ListRecordItem;

现在要按照 ncount 降序,在 ncount 相同的时候,按btItemLvl的降序排列。

解决方法:

[cpp]  view plain  copy
  1. struct cmp  
  2. {  
  3.     bool operator()(const stRecordItem &st1,const stRecordItem &st2 )  
  4.     {  
  5.         if(st1.ncount<st2.ncount)  
  6.         {  
  7.             return false;  
  8.         }  
  9.         else if(st1.ncount == st2.ncount)  
  10.         {  
  11.             if(st1.btItemLvl < st2.btItemLvl)  
  12.             {  
  13.                 return false;  
  14.             }  
  15.             else  
  16.                 return true;  
  17.         }  
  18.         else  
  19.             return true;  
  20.     }  
  21. };  
  22. ListRecordItem.sort(cmp());  

不言而喻,排序算法的简洁性与可扩展性都非常好。这个方法就是使用了stl常用的一种方法:仿函数

仿函数的概念:

仿函数(functor),就是使一个类的使用看上去象一个函数。其实现就是类中实现一个operator(),这个类就有了类似函数的行为,就是一个仿函数类了。

 

http://www.cnblogs.com/rereadyou/articles/2011048.html  有一个比较好的例子,实现了Huffman 编码与解码的程序,摘录如下:

 

使用 C++ 在 Editplus 中编写一个 Huffman 编码与解码的程序,遇到了需要对一个文件中的所有字符进行权重计算以创建每个字符的最终编码的过程,这其中有一个步骤是需要遍历已有的字符权重表以查找当前从文件中取得的字符是否已经存在于该表中,如果存在就将该表中的该字符权重加一,如不存在则需要新建一个存储 node 以存储该字符,并将该 node 结构添加到已有的权重表中。

      考虑到需要在权重表中进行字符查找以及之后创建 Huffman Tree 时可能需要对该表各项进行排序所以选用 vector<Node>作为存储容器,查找及排序可以使用 algorithm 头文件当中的 sort 和 find 算法。

huffman.h

[cpp]  view plain  copy
  1. #include <iostream>  
  2. #include <fstream>  
  3. #include <string>  
  4. #include <vector>   
  5. #include <algorithm>  
  6. //for find_if func;  
  7. #include <functional>  
  8. //for bind2nd func;   
  9. using namespace std;     
  10. class Huffman {       
  11. public:           
  12.     Huffman(void);                
  13.     void encode(const string& file);          
  14.     void decode(const string& file, const string& srcTable) const;          
  15.     ~Huffman();       
  16. private:           
  17.     //table node            
  18.     typedef struct           
  19.     {               
  20.         char letter;              
  21.         int  level;               
  22.         int  parent;               
  23.         int direction;//-1 for no parent, 0 for left, 1 right;          
  24.     }Node;           
  25.     vector<Node> nodeTable;//can be sorted;                    
  26.     //仿函数,用于find_if,侦测字符是否已经存在;           
  27.     //template <typename T1, typename T2>           
  28.     class Comparer : public binary_function<Node, charbool>           
  29.     {              
  30.     public:                  
  31.         Comparer(const char ch):_ch(ch){};                    
  32.         const bool operator()(const vector<Node>::value_type& node, char ch) const                  
  33.         {                      
  34.             return node.letter == ch;                  
  35.         }                
  36.     private:                  
  37.         char _ch;          
  38.     };  
  39. };    

Huffman.cpp

[cpp]  view plain  copy
  1. #include "Huffman.h"   
  2. Huffman::Huffman(void)   
  3. {      
  4.     //dummany;   
  5. }     
  6. void Huffman::encode(const string& file)   
  7. {       
  8.     ifstream fin;       
  9.     fin.open(file.c_str());  
  10.     char ch;  
  11.     while(fin.get(ch))   
  12.     {           
  13.         if (nodeTable.size() != 0)   
  14.         {               
  15.             //vector<Node>::iterator result = find_if(nodeTable.begin(),nodeTable.end(),Compare(ch));   
  16.             //仿函数              
  17.             vector<Node>::iterator result = find_if(nodeTable.begin(),nodeTable.end(),bind2nd(Comparer(ch), ch));  
  18.             //lambda 函数方式;              
  19.             //vector<Node>::iterator result = find_if(nodeTable.begin(),nodeTable.end(),[](const Node& node)->bool{return node.letter == ch;});   
  20.             if (result == nodeTable.end())    
  21.             {                  
  22.                 Node* node = new Node;    
  23.                 node->letter = ch;             
  24.                 node->level = 1;                  
  25.                 node->parent = 0;                 
  26.                 node->direction = -1;            
  27.             }              
  28.             else          
  29.             {                
  30.                 result->level += 1;    
  31.             }         
  32.         }         
  33.         else        
  34.         {              
  35.             Node* node = new Node;     
  36.             node->letter = ch;          
  37.             node->level = 1;            
  38.             node->parent = 0;            
  39.             node->direction = -1;        
  40.         }      
  41.     }       
  42.     fin.close();          
  43.     //huffman tree;   
  44. }     
  45. void Huffman::decode(const string& file, const string& srcTable) const  
  46. {      
  47.     //dummany;   
  48. }     
  49. Huffman::~Huffman(void)  
  50. {      
  51.     //dummany;   
  52. }   

说明:

1、仿函数不是函数,而是一个类,这个类重载了()操作符;

2、仿函数调用过程是这样的:find_if(nodeTable.begin(), nodeTable.end(), Comparer(ch))

中 Comparer(ch)只是创建了 Comparer 类的匿名方法,重载的 operator() 真正的调用是在

接下来将要看到的 find_if 模板函数的这一句 pred(*first)

3、代码中不使用 find 而使用 find_if 是因为需要进行查找的不是 prime type 而是自行编写的符合

类型,find_if 的函数原型参考如下,从原型中可以看到第一个参数是以默认的方式进行的:

[cpp]  view plain  copy
  1. template<class InputIterator, class Predicate>   
  2. InputIterator find_if ( InputIterator first, InputIterator last, Predicate pred )    
  3. {     
  4.     for ( ; first!=last ; first++ )   
  5.         if ( pred(*first) )  
  6.             break;      
  7.     return first;   
  8. }   

4、bind2nd 函数的作用是将 二元算子(binary functor, bf) 转化为一元算子(unary functor,uf)还有一个类似的bind1st,

使用时需要包含functional头文件;

5、进行比较的仿函数需要继承 binary_functor<typename T1,typename T2,typename T3 >,原型如下:

[cpp]  view plain  copy
  1. template<class Arg1, class Arg2, class Result>  
  2. struct binary_function   
  3. {        
  4.       typedef Arg1 first_argument_type;     
  5.       typedef Arg2 second_argument_type;    
  6.       typedef Result result_type;      
  7. };   


还有一些比较有用的博客,可以借鉴参考:

http://www.cnblogs.com/wzh206/archive/2010/03/26/1696983.html //介绍了一些STL的仿函数的应用

http://cunsh.ycool.com/post.1380153.html //实现了一个泛化的仿函数

http://www.soft-bin.com/html/2010/07/21/stl-cpp-functor-tutorial2.html //详细介绍了STL源码中对仿函数的使用并解析了remove_if、bind2nd的源代码(个人觉得非常具有参考性,推荐阅读)

http://blog.sina.com.cn/s/blog_491c65000100d62y.html  //实现了一个泛化的仿函数(>、<、== 、!=)的比较。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值