1. List of names of all STL Sort Algorithm functions:
Function Description
Sort sorts all elements in a given range.
Stable_sort performs a stable sorting of all elements in a given range
Partial_sort sorts all elements in a given range.
Partial_sort_copy: Copy and sort data within a specified range.
Nth_element: Find the elements corresponding to a location in a given range.
Is_sorted checks whether the order of a range has been sorted
Partition puts elements that meet certain conditions in front
Stable_partition is relatively stable, so that elements that meet certain conditions are placed in front
2. Comparison functions:
When you want to sort data in a specific way, you need to specify a comparison function for sort. Otherwise, the program will automatically provide you with a comparison function.
Vector <int> vect;
Sort (vect. Begin (), vect. End (); // This is equivalent to calling
Sort (vect. Begin (), vect. End (), less <int> ());
Other comparison functions in sort
Equal _
Not_0000_to is not equal
Less
Greater is greater
Less_equal less than or equal
Greater_equal greater than or equal
In the preceding example, the system provides the less function for sort. Other replicas are also provided in STL. The following is a list of replicas: you cannot directly write the name of the replicas, but you need to write its overloaded () function: less <int> ();
You can directly use these function templates when there are some standard types (INT float char) or strings in your container metadata. However, if you define a type or you want to sort it by other means, you can use either of the following methods to achieve the effect:One is to write a comparison function by yourself..The other is the '<' operation of the overload type..
3. Full sorting:
In full order, all elements in a given range are arranged in order of size and relationship. Sort uses a mature "quick sorting algorithm" (currently, most STL versions do not adopt simple quick sorting, but use interpolation sorting algorithms ). The complexity is N * log (n ). Stable_sort uses "Merge Sorting". When enough memory is allocated, the complexity of the algorithm is N * log (N). Otherwise, the complexity is N * log (n) * log (n) has the advantage that the relative positions of equal elements are consistent before and after sorting.
The functions used for full sorting are:
1. Void sort (randomaccessiterator first, randomaccessiterator last );
2. Void sort (randomaccessiterator first, randomaccessiterator last, strictweakordering comp );
3. Void stable_sort (randomaccessiterator first, randomaccessiterator last );
4. Void stable_sort (randomaccessiterator first, randomaccessiterator last, strictweakordering comp );
4. Partial sorting:
Partial_sort uses heapsort, Which is N * log (n) in all situations ).
Partial sorting is actually a sort method provided to reduce unnecessary operations.
Its function prototype is:
4.1Void partial_sort (randomaccessiterator first, randomaccessiterator middle, randomaccessiterator last );
4.2Void partial_sort (randomaccessiterator first, randomaccessiterator middle, randomaccessiterator last, strictweakordering comp );
4.3Randomaccessiterator partial_sort_copy (inputiterator first, inputiteratorlast, randomaccessiteratorresult_first, randomaccessiterator result_last );
4.4Randomaccessiterator partial_sort_copy (inputiterator first, inputiteratorlast, randomaccessiteratorresult_first, randomaccessiterator result_last, compare comp );
For example:There are 1000 students in the class. I want to know who has the lowest scores.
Partial_sort (vect. Begin (), vect. Begin () + 5, vect. End (), less <student> ());
5. nth_element Specify element sorting
5.1Void nth_element (randomaccessiterator first, randomaccessiterator nth, randomaccessiterator last );
5.2Void nth_element (randomaccessiterator first, randomaccessiterator nth, randomaccessiterator last,
Strictweakordering comp );
For example:There are 1000 students in the class. I want to know the number of students with the lowest scores.
Nth_element (vect. Begin (), vect. Begin () + 3, vect. End (), less <student> ());
6. Partition and stable_partition
Partition divides the elements in a range into two classes based on a condition, and does not sort them.
Its function prototype is:
6.1Forwarditerator partition (forwarditerator first, forwarditerator last, predicate Pred)
6.2Forwarditerator stable_partition (forwarditerator first, forwarditerator last, predicate Pred );
For example, if there are 10 students in the class who pass the calculation (below 60 points:
Student exam ("pass", 60 );
Stable_partition (vect. Begin (), vect. End (), bind2nd (less <student> (), exam ));
7. Efficiency increases from high to low (Time consumed ):
Partion
Stable_partition
Nth_element
Partial_sort
Sort
Stable_sort
8. Valid STLThe following is a good summary of how to select the sorting function:
8.1To sort the vector, String, deque, or array containers in full order, you can select sort or stable_sort;
8.2If you only need to obtain the top n elements in the vector, String, deque, or array container, partial_sort is the first choice.
8.3For a vector, String, deque, or array container, you need to find the element at the nth position or you need to get the top N and it is not related to the internal order in Top N, nth_element is the most ideal;
8.4If you want to separate elements that meet or do not meet a certain condition from the standard sequence container or array, you 'd better use partition or stable_partition;
8.5If you use the list container, you can directly use the partition and stable_partition algorithms. You can use list: Sort instead of sort and stable_sort.