C++ 定义实用比较函数(Custom Compare Function) 注意点
2015-09-25 16:36
519 查看
学过C++的应该都知道在调用sort算法的时候可以自定义比较函数(map, heap也都有类似的比较函数谓词的定义), 从而实现不同的排序比如可以从小到大或者从大到小,比如如下就是sort函数的申明(摘自cplusplus):
compBinary function that accepts two elements in the range as arguments, and returns a value convertible to
weak ordering it defines.
The function shall not modify any of its arguments.
This can either be a function pointer or a function object.
那么今天其实我要说的一个注意的问题就是我自己在些程序的时候遇到的,现在分享出来:自定义的比较函数不能是类的成员函数。其实这个也不难理解, 因为成员函数作为类的成员, 你要在某个具体的类的实例中才能找到对应的代码段或者在内存中的位置, 这样才能被sort 使用用来比较, 这里可能概念上说的也不完全正确, 大概是这么理解一下,先面是我自己写的一个小的测试程序, 一看就应该明白我想讲的意思:
template <class RandomAccessIterator, class Compare> void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp); 其中 comp是一个谓词可以是`函数指针`或者`函数对象`:
compBinary function that accepts two elements in the range as arguments, and returns a value convertible to
bool. The value returned indicates whether the element passed as first argument is considered to go before the second in the specificstrict
weak ordering it defines.
The function shall not modify any of its arguments.
This can either be a function pointer or a function object.
那么今天其实我要说的一个注意的问题就是我自己在些程序的时候遇到的,现在分享出来:自定义的比较函数不能是类的成员函数。其实这个也不难理解, 因为成员函数作为类的成员, 你要在某个具体的类的实例中才能找到对应的代码段或者在内存中的位置, 这样才能被sort 使用用来比较, 这里可能概念上说的也不完全正确, 大概是这么理解一下,先面是我自己写的一个小的测试程序, 一看就应该明白我想讲的意思:
class comp { public: bool operator() (const int &l, const int &r) const { return l > r; } }; bool comp_fun(const int &a, const int &b) { return a > b; } class Test { public: bool comp_in_class(const int &a, const int &b) { return a > b; } void test(vector<int> &data) { //sort(data.begin(), data.end(), comp_in_class); //compile error! sort(data.begin(), data.end(), comp_fun); } }; int main(void) { vector<int> data; data.push_back(1); data.push_back(-1); data.push_back(12); data.push_back(15); data.push_back(-8); sort(data.begin(), data.end()); cout<<"defualt compare: "; for(vector<int>::size_type i = 0; i != data.size(); ++i) cout<<data[i]<<' '; cout<<endl; Test t; t.test(data); cout<<"class method call: "; for(vector<int>::size_type i = 0; i != data.size(); ++i) cout<<data[i]<<' '; cout<<endl; sort(data.begin(), data.end(), comp()); cout<<"function object: "; for(vector<int>::size_type i = 0; i != data.size(); ++i) cout<<data[i]<<' '; cout<<endl; sort(data.begin(), data.end(), comp_fun); cout<<"just function (pointer): "; for(vector<int>::size_type i = 0; i != data.size(); ++i) cout<<data[i]<<' '; cout<<endl; return 0; }
相关文章推荐
- 【C++】小心使用文件读写模式:回车('\r') 换行('\n')问题的一次纠结经历
- c语言学习笔记(7)单引号,双引号和逻辑符号的用法
- 黑马程序员——C语言基础---数据类型
- C语言中的简单函数
- 比较好的c++博文
- vim插件之AutoComplPop和OmniCppComplete的安装和使用
- c++中格式化输出。十六进制浮点数0x p输出问题
- C语言常见编译错误与执行错误
- 关于strcmp()的实现
- C语言的基本知识
- C语言的几个基本结构
- c++primer 第三章习题(4)
- C++Primer第五版 练习11.11(解答)
- C++Primer第五版 练习11.10(解答)
- C++之内部类与外部类(嵌套类)及友元
- C++Primer第五版 练习11.9(解答)
- 解决java和C语言之间DES加解密不一致的问题。
- C/C++ 运算符优先级
- C++Primer第五版 11.2.2节练习
- C++ 嵌套类使用(三)