node->letter = ch;
node->level = 1;
node->parent = 0;
node->direction = -1;
}
else
{
result->level += 1;
}
}
else
{
Node* node = new Node;
node->letter = ch;
node->level = 1;
node->parent = 0;
node->direction = -1;
}
}
fin.close();
//huffman tree;
}
void Huffman::decode(const string& file, const string& srcTable) const
{
//dummany;
}
Huffman::~Huffman(void)
{
//dummany;
}
说明:
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 的函数原型参考如下,从原型中可以看到第一个参数是以默认的方式进行的:
template
InputIterator find_if ( InputIterator first, InputIterator last, Predicate pred )
{
for ( ; first!=last ; first++ )
if ( pred(*first) )
break;
return first;
}
4、bind2nd 函数的作用是将 二元算子(binary functor, bf) 转化为一元算子(unary functor,uf)还有一个类似的bind1st,
使用时需要包含functional头文件;
5、进行比较的仿函数需要继承 binary_functor
template
struct binary_function
{
typedef Arg1 first_argument_type;
typedef Arg2 second_argument_type;
typedef Result result_type;
};
还有一些比较有用的博客,可以借鉴参考:
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 //实现了一个泛化的仿函数(>、<、== 、!=)的比较。
摘自 lbqBraveheart的专栏