std::sort不支持list,set,只支持随机存取的容器,如vector
std::sort排序的使用:
1,对vector<int>m_Vect升序排序,可以直接std::sort(m_Vect.begin(), m_Vect.end());
2,对vector<Elem>m_Vect排序,其中Elem是一个类,或者一个结构体,现在对vector排序是指按Elem中的某个属性排序。
如:
class Elem
{
private:
int property1;
int property2;
public:
int getProperty1();
void set Property1();
int getProperty2();
void set Property2();
};
typedef std::vector<Elem> ElemVect;
ElemVect m_ElemVect;
现在对m_ElemVect按照属性property1排序。
//降序排序
temple<typename T1, typename T2>
bool DescendingOrder(T1 t1, T2 t2)
{
return t1.getProperty1() > t2.getProperty1();
}
使用的地方:
std::sort(m_ElemVect.begin(), m_ElemVect.end(), DescendingOrder<Elem, Elem>);
________________________________________________________________________________________________________________________
#include <algorithm>
void sort( iterator start, iterator end );
void sort( iterator start, iterator end, StrictWeakOrdering cmp );
The sort() algorithm sorts the elements in the range [start,end) into ascending order. If two elements are equal, there is no guarantee what order they will be in.
If the strict weak ordering function object cmp is given, then it will be used to compare two objects instead of the < operator.
The algorithm behind sort() is the introsort algorithm. sort() runs in O(N log(N)) time (average and worst case) which is faster than polynomial time but slower thanlinear time.
For example, the following code sorts a vector of integers into ascending order:
vector<int> v;
v.push_back( 23 );
v.push_back( -1 );
v.push_back( 9999 );
v.push_back( 0 );
v.push_back( 4 );
cout << "Before sorting: ";
for( unsigned int i = 0; i < v.size(); i++ ) {
cout << v[i] << " ";
}
cout << endl;
sort( v.begin(), v.end() );
cout << "After sorting: ";
for( unsigned int i = 0; i < v.size(); i++ ) {
cout << v[i] << " ";
}
cout << endl;
When run, the above code displays this output:
Before sorting: 23 -1 9999 0 4
After sorting: -1 0 4 23 9999
Alternatively, the following code uses the sort() function to sort a normal array of integers, and displays the same output as the previous example:
int array[] = { 23, -1, 9999, 0, 4 };
unsigned int array_size = 5;
cout << "Before sorting: ";
for( unsigned int i = 0; i < array_size; i++ ) {
cout << array[i] << " ";
}
cout << endl;
sort( array, array + array_size );
cout << "After sorting: ";
for( unsigned int i = 0; i < array_size; i++ ) {
cout << array[i] << " ";
}
cout << endl;
This next example shows how to use sort() with a user-specified comparison function. The functioncmp is defined to do the opposite of the < operator. When sort() is called withcmp used as the comparison function, the result is a list sorted in descending, rather than ascending, order:
bool cmp( int a, int b ) {
return a > b;
}
...
vector<int> v;
for( int i = 0; i < 10; i++ ) {
v.push_back(i);
}
cout << "Before: ";
for( int i = 0; i < 10; i++ ) {
cout << v[i] << " ";
}
cout << endl;
sort( v.begin(), v.end(), cmp );
cout << "After: ";
for( int i = 0; i < 10; i++ ) {
cout << v[i] << " ";
}
cout << endl;