学过C++的应该都知道在调用sort算法的时候可以自定义比较函数(map, heap也都有类似的比较函数谓词的定义), 从而实现不同的排序比如可以从小到大或者从大到小,比如如下就是sort函数的申明(摘自cplusplus):
template <class RandomAccessIterator, class Compare>
void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);
其中 comp是一个谓词可以是`函数指针`或者`函数对象`:
- comp
- Binary 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.
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++中如何自定义sort算法的比较函数,包括使用函数指针、函数对象进行排序的方法,并强调了成员函数不能直接作为比较函数的原因。
821

被折叠的 条评论
为什么被折叠?



