Transferred from: http://blog.csdn.net/zzzmmmkkk/article/details/4266888/
So I summed up a bit, first look at the sort function in the following table:
| Name of function |
function Description |
| Sort |
Sort all elements of a given interval |
| Stable_sort |
Stable ordering of all elements in a given interval |
| Partial_sort |
Sort all elements of a given interval |
| Partial_sort_copy |
Copy and sort the given interval |
| Nth_element |
Find the element corresponding to a position in a given interval |
| is_sorted |
Determine if an interval has been sequenced. |
| Partition |
Put elements that meet a certain condition in front |
| Stable_partition |
Relatively stable so that elements that meet a certain condition are placed in front |
To use this function, you can use # include <algorithm> sort only, and the syntax is described as:
Sort (begin,end), which represents a range, for example:
int _tmain (int argc, _tchar* argv[])
{
int a[20]={2,4,1,23,5,76,0,43,24,65},i;
for (i=0;i<20;i++)
cout<<a[i]<<endl;
Sort (a,a+20);
for (i=0;i<20;i++)
cout<<a[i]<<endl;
return 0;
}
The output would be to sort the array a in ascending order, and perhaps someone would ask how to use it in descending order? This is what the next discussion is about.
One is to write a comparison function to implement, and then call the three parameter Sort:sort (Begin,end,compare). For the list container, this method also applies, compare as a sort parameter, namely: Sort (Compare).
1) Write your own compare function:
BOOL Compare (int a,int b)
{
Return a<b; Sorted in ascending order, or descending if return a>b is changed
}
int _tmain (int argc, _tchar* argv[])
{
int a[20]={2,4,1,23,5,76,0,43,24,65},i;
for (i=0;i<20;i++)
cout<<a[i]<<endl;
Sort (a,a+20,compare);
for (i=0;i<20;i++)
cout<<a[i]<<endl;
return 0;
}
2) Further, make this operation more adaptable to change. That is, you can give a comparison function a parameter that indicates whether to sort in ascending or descending order, which turns to the function object's appearance.
For the sake of description, I first define an enumeration type Enumcomp to represent ascending and descending. Very simple:
Enum ENUMCOMP{ASC,DESC};
It then starts with a class to describe the function object. It will decide whether to use "<" or ">" according to its parameters.
Class Compare
{
Private
Enumcomp comp;
Public
Compare (Enumcomp C): Comp (c) {};
BOOL operator () (int num1,int num2)
{
Switch (COMP)
{
Case ASC:
Return num1<num2;
Case DESC:
Return num1>num2;
}
}
};
Next use Sort (Begin,end,compare (ASC) to implement ascending, sort (Begin,end,compare (DESC) implementation in descending order.
The main functions are:
int main ()
{
int a[20]={2,4,1,23,5,76,0,43,24,65},i;
for (i=0;i<20;i++)
cout<<a[i]<<endl;
Sort (A,a+20,compare (DESC));
for (i=0;i<20;i++)
cout<<a[i]<<endl;
return 0;
}
3) In fact, for such a simple task (type support "<", ">" and other comparison operators), it is completely unnecessary to write a class. The standard library already has a ready-made, in the functional, include in the line. Functional provides a bunch of template-based comparison function objects. They are (see name to know meaning):equal_to<type>, not_equal_to<type>, greater<type>, greater_equal<type>, Less<type>, less_equal<type>. For this problem, greater and less are enough to come straight to use:
- Ascending: Sort (begin,end,less<data-type> ());
- Descending: Sort (begin,end,greater<data-type> ()).
int _tmain (int argc, _tchar* argv[])
{
int a[20]={2,4,1,23,5,76,0,43,24,65},i;
for (i=0;i<20;i++)
cout<<a[i]<<endl;
Sort (a,a+20,greater<int> ());
for (i=0;i<20;i++)
cout<<a[i]<<endl;
return 0;
}
4) Since there is an iterator, if it is a string you can use a reverse iterator to complete the order, the program is as follows:
int main ()
{
String str ("cvicses");
String s (Str.rbegin (), Str.rend ());
cout << S <<endl;
return 0;
}
Qsort ():
Prototype:
_crtimp void __cdecl qsort (void*, size_t, Size_t,int (*) (const void*, const void*));
Explanation: qsort (array name, number of elements, space occupied by elements (sizeof), comparison function)
The comparison function is a self-written function that follows the format of the int COM (const void *a,const void *b).
When a-B relationship is > < =, a positive negative value of 0 (or vice versa) is returned respectively.
When you use a B to cast the type, the operation is performed after the type of the response is converted from void *.
Array subscript starts from zero, number is N, subscript 0-(n-1).
Instance:
int compare (const void *a,const void *b)
{
return * (int*) b-* (int*) A;
}
int main ()
{
int a[20]={2,4,1,23,5,76,0,43,24,65},i;
for (i=0;i<20;i++)
cout<<a[i]<<endl;
Qsort (void *) a,20,sizeof (int), compare);
for (i=0;i<20;i++)
cout<<a[i]<<endl;
return 0;
}
Related:
1) Why do you have to give the number of elements?
Because the array doesn't know how many of its own elements
2) Why do you have to give size?
Because qsort doesn't know the units it wants to sort.
3) Why do you have to write the ugly function that compares the two values?
Because qsort requires an indicator to point to a function because it does not know what type of element it is ordering.
4) Why does the comparison function used by Qsort accept a const void* argument instead of a char* argument?
Because qsort can sort the values of non-strings.
Of course there are other uses for sorting functions, just say this today, I am also learning, the above just published a little bit of my humble opinion, the final statement of appeal is based on vc++2008.
C + + sort function sort (), use of qsort ()