C ++ 11 new feature application-introduces several new convenience algorithms (several algorithms used for sorting)
Continue to add the algorithm in the header file algorithm in C ++ 11.
At least I think the most used algorithm in stl is sort. Instead of exploring the source code of sort, we will introduce several new sorting functions in C ++ 11.
How do we know whether a sequence is ordered? This is used:
Is_sorted
Prototype:
template
ForwardIterator is_sorted_until (ForwardIterator first, ForwardIterator last);template
ForwardIterator is_sorted_until (ForwardIterator first, ForwardIterator last, Compare comp);
Purpose:
Whether the queue [first, last) is ordered.
Note that there are two prototypes.
The first is the default value, that is, there are only two parameters. This is to determine whether [first, last) is in ascending order, that is, <.
The second is three parameters, which determines whether the [first, last) interval is sorted by comp.
Application:
# Include
// Std: cout # include
// Std: cout # include // std: is_sorted, std: prev_permutation # include // std: arraybool compare (int a, int B) {return a> B; // ascending order. If it is changed to return a> B, it is in descending order} int main () {std: array
Foo {2, 4, 3}; std: array
Foo2 {2, 4, 3}; do {// try a new permutation: std: prev_permutation (foo. begin (), foo. end (); // print range: std: cout <"foo:"; for (int & x: foo) std :: cout <''<x; std: cout <'\ n';} while (! Std: is_sorted (foo. begin (), foo. end (); std: cout <"the range is sorted! \ N "; do {// try a new permutation: std: prev_permutation (foo2.begin (), foo2.end (); // print range: std :: cout <"foo2:"; for (int & x: foo2) std: cout <''<x; std: cout <'\ n ';} while (! Std: is_sorted (foo2.begin (), foo2.end (), compare); std: cout <"the range is Descending sorted! \ N "; return 0;} // output: // foo: 2 3 4 1 // foo: 2 3 1 4 // foo: 2 1 4 3 // foo: 2 1 4 3 // foo: 2 1 3 4 // foo: 1 4 3 2 // foo: 1 4 2 3 // foo: 1 3 4 2 // foo: 1 3 2 4 4/foo: 1 3 2 4/foo: 1 2 4 3 // foo: 1 2 3 4 // the range is sorted! // Foo2: 2 3 4 1 // foo2: 2 3 1 4 // foo2: 2 1 4 3 // foo2: 2 1 3 4 // foo2: 2 1 3 4 // foo2: 1 4 3 2 // foo2: 1 4 2 3 // foo2: 1 3 4 2 // foo2: 1 3 2 4 // foo2: 1 2 4 3 // foo2: 1 2 3 4 // foo2: 4 3 2 1 // the range is Descending sorted!
A fully-arranged algorithm is used here. If it is not added to C ++ 11, it will not be repeated.
The above code shows two versions using is_sorted.
Note that:
If the number of elements in the range is less than two, true is always returned.
Is_sorted_until
Prototype:
template
ForwardIterator is_sorted_until (ForwardIterator first, ForwardIterator last);template
ForwardIterator is_sorted_until (ForwardIterator first, ForwardIterator last, Compare comp);
Purpose:
Find first unsorted element in range
Returns an iterator to the first element in the range [first, last) which does not follow an ascending order.
If the entire range is sorted, the function returns last.
Application:
#include
// std::cout#include // std::is_sorted_until, std::prev_permutation#include // std::arrayint main () { std::array
foo {2,4,1,3}; std::array
::iterator it; do { // try a new permutation: std::prev_permutation(foo.begin(),foo.end()); // print range: std::cout << "foo:"; for (int& x:foo) std::cout << ' ' << x; it=std::is_sorted_until(foo.begin(),foo.end()); std::cout << " (" << (it-foo.begin()) << " elements sorted)\n"; } while (it!=foo.end()); std::cout << "the range is sorted!\n"; return 0;}