Reprint: http://blog.csdn.net/sunshangjin/article/details/40296357
Think of themselves every day sort of sort, bubbling ah, binary search Ah, the results in the STL with the sorting function Sort,qsort, finally freed himself ~
The Std::sort () function is powerful and can be used to sort elements such as classes, structs, and so on. C++sort Reference Manual
So I summed up a bit, first look at the sort function in the following table: Reference csdn
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 just use # include <algorithm>
Sort can be used with the syntax described as:
Sort (begin,end), which represents a range, for example:
[CPP]View PlainCopy
- #include <iostream>
- #include <algorithm>
- #include <string>
- Using namespace std;
- int main ()
- {
- int a[10]={9,12,17,30,50,20,60,65,4,49};
- Sort (a,a+10);
- For (int i=0;i<10;i++)
- cout<<a[i]<<";
- cout<<endl;
- return 0;
- }
2) Write a comparison function to implement, then call the three parameters of the Sort:sort (Begin,end,compare) becomes. For the list container, this method also applies, compare as a sort parameter, namely: Sort (Compare).
[CPP]View PlainCopy
- #include <iostream>
- #include <algorithm>
- #include <string>
- Using namespace std;
- BOOL Compare (int A,int b)
- {
- return a<b; //default ascending order, or descending if return a>b is changed
- }
- BOOL Compare_decrease (int A,int b)
- {
- return a>b; //From a large to a small arrangement
- }
- int main ()
- {
- int a[10]={9,12,17,30,50,20,60,65,4,49};
- Sort (a,a+10);
- For (int i=0;i<10;i++)
- cout<<a[i]<<";
- cout<<endl;
- Sort (a,a+10,compare_decrease);
- For (int i=0;i<10;i++)
- cout<<a[i]<<";
- cout<<endl;
- return 0;
- }
PS: Generally not used for string sorting
Sorting algorithm accumulation (2)