STL中sort函数
一、STL中排序函数的各个版本如下图所示:

在C标准库stdlib中包含了一个快排序函数qsort:
void qsort (void* base, size_t num, size_t size,
int (*compar)(const void*,const void*));
它的比较函数参数也即谓词部分是函数指针形式,而STL中的谓词部分则是操作符或布尔函数。
int compar (const void* p1, const void* p2);
示例如下(摘自cplusplus.com):
#include <stdio.h> /* printf */
#include <stdlib.h> /* qsort */
int values[] = { 40, 10, 100, 90, 20, 25 };
int compare (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}
int main ()
{
int n;
qsort (values, 6, sizeof(int), compare);
for (n=0; n<6; n++)
printf ("%d ",values[n]);
return 0;
}
稳定排序是指排序后,两个相等的元素位置不发生改变。既然元素相等,那么位置改不改变有什么关系?原因是这里的元素相等,指的是排序基准元素相等,如果排序的对象是一个类,类中还可能有其他并不相等的元素。
二、排序函数的使用
STL中所有的算法都有两个版本,第一版本是最常用的的运算,第二种则允许用户定义自己的运算方式;
template<class RanIt>
void sort(RanIt fist, RanIt last);
template<class RanIt, class Pred>
void sort(RanIt fist, RanIt last, Pred pr);
根据我的理解,用户定义自己的运算有两种情况:
(1)内建类型或自定义类型,谓词部分为普通函数,该函数必须为全局函数或类的静态数据成员,不能为类的普通成员函数,示例如下(摘自leetcode Largest Number):
bool compare(const string& str1,const string& str2)
{
return (str1+str2)>(str2+str1);
}
sort(str_vector.begin(),str_vector.end(),compare);
(2)自定义类型。这时重载相应操作符(重载函数为类成员或者全局函数)(视所使用的算法函数而定,如果是排序函数一般是‘<’操作符;如果是find函数一般是‘==’),或者使用仿函数(函数对象)(一般重载'()'操作符),自定义或者调用库中的。示例如下(摘自):
//重载类成员操作符
#include "stdafx.h"
#include <vector>
#include <algorithm> // Include algorithms
#include <iostream>
#include <string>
using namespace std;
class myless
{
public:
bool operator()( const int &a, const int &b) {
return a < b;
}
};
int main(int argc, char* argv[])
{
vector<int> vec;
vector<int>::iterator i;
vec.push_back (10);
vec.push_back (3);
vec.push_back (7);
sort(vec.begin(), vec.end(), myless()); // Sort the vector
for (i = vec.begin(); i != vec.end(); i++)
{
cout<<*i<<endl;
}
return 0;
}
//比较函数是普通函数
#include "stdafx.h"
#include <vector>
#include <algorithm> // Include algorithms
#include <iostream>
#include <string>
using namespace std;
typedef struct
{
string first;
string last;
}NAME;
bool sortbyfirst(const NAME& n1, const NAME& n2)
{
return (n1.first<n2.first);
}
bool sortbylast(const NAME& n1, const NAME& n2)
{
return (n1.last<n2.last);
}
int main(int argc, char* argv[])
{
vector<NAME> contacts;
vector<NAME>::iterator j;
NAME tmp;
tmp.first = "liu";
tmp.last = "bei";
contacts.push_back(tmp);
tmp.first = "zhao";
tmp.last = "yun";
contacts.push_back(tmp);
tmp.first = "gun";
tmp.last = "yu";
contacts.push_back(tmp);
tmp.first = "zhang";
tmp.last = "fei";
contacts.push_back(tmp);
cout<<"by first:"<<endl;
sort(contacts.begin(), contacts.end(), sortbyfirst);
for(j=contacts.begin(); j!= contacts.end(); j++)
{
cout<<j->first<<" "<<j->last<<endl;
}
cout<<"by last:"<<endl;
sort(contacts.begin(), contacts.end(), sortbylast);
for(j=contacts.begin(); j!= contacts.end(); j++)
{
cout<<j->first<<" "<<j->last<<endl;
}
return 0;
}
struct node{
int i;
int j;
string str;
}
class A{
.....
public:
void use_sort();
bool ALess(node n1,node2)
{
if(n1.i<n2.i) return true;
else return false;
}
.....
}
bool BLess(node n1,node n2)
{
if(n1.i<n2.i) return true;
else return false;
}
void A::use_sort()
{
vector<node>node_vec;
.....
sort(node_vec.begin(),node_vec.end(),ALess);//not work
sort(node_vec.begin(),node_vec.end(),BLess);//work
}

浙公网安备 33010602011771号