C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
C++ Algorithm is_sorted_until()C++ Algorithm is_sorted_until() function is used to find first unsorted element in the range. It means it returns an iterator to the first element in the range [first, last) which does not follow an ascending order. The elements are compared using operator < for the first version, and comp for the second version. Syntaxdefault (1) template <class ForwardIterator> ForwardIterator is_sorted_until (ForwardIterator first, ForwardIterator last); custom (2) template <class ForwardIterator, class Compare> ForwardIterator is_sorted_until (ForwardIterator first, ForwardIterator last, Compare comp); Parameterfirst: An forward iterator pointing to the first element in the range to be checked. last: An random access iterator pointing to the past last element in the range to be checked. comp: A user-defined binary predicate function that accepts two arguments and returns true if the two arguments are in order and false otherwise. It follows the strict weak ordering to order the elements. Return valueIt returns an iterator to the first element if the range is unsorted and returns last if the entire range is sorted. ComplexityThe Complexity is linear between first and last: calls comp for each element until a mismatch is found. Data racesThe object in the range [first, last) are accessed. ExceptionsThis function throws an exception if either comp, or an operation on iterator throws an exception. Please note that invalid parameters cause an undefined behavior.Example 1Let's see the simple example to demonstrate the use of is_sorted_until(): #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { vector<int> v = {3, 1, 4, 2, 5}; cout << boolalpha; cout << "Before: is it sorted? " << (is_sorted_until(v.begin(), v.end()) == v.end()) << endl; sort(v.begin(), v.end()); cout << "After: is it sorted? " << (is_sorted_until(v.begin(), v.end()) == v.end()) << endl; return 0; } Output: Before: is it sorted? false After: is it sorted? true Example 2Let's see another simple example: #include <iostream> #include <vector> #include <algorithm> using namespace std; bool ignore_case(char a, char b) { return (tolower(a) == tolower(b)); } int main(void) { vector<char> v = {'A', 'b', 'C', 'd', 'E'}; auto it = is_sorted_until(v.begin(), v.end()); cout << "First unsorted element = " << *it << endl; it = is_sorted_until(v.begin(), v.end(), ignore_case); if (it == end(v)) cout << "Entire vector is sorted." << endl; return 0; } Output: First unsorted element = C Entire vector is sorted. Example 3Let's see another simple example: #include <iostream> #include <vector> #include <algorithm> using namespace std; int main(void) { vector<int> v = {1, 2, 5, 3, 4}; auto it = is_sorted_until(v.begin(), v.end()); cout << "First unsorted element = " << *it << endl; v[3] = 4; it = is_sorted_until(v.begin(), v.end()); if (it == end(v)) cout << "Entire vector is sorted." << endl; return 0; } Output: First unsorted element = 3 Example 4Let's see another simple example: #include <iostream> // std::cout #include <algorithm> // std::is_sorted_until, std::prev_permutation #include <array> // std::array using namespace std; int main () { array<int,4> foo {2,4,1,3}; array<int,4>::iterator it; do { // try a new permutation: prev_permutation(foo.begin(),foo.end()); // print range: cout << "foo:"; for (int& x:foo) cout << ' ' << x; it=is_sorted_until(foo.begin(),foo.end()); cout << " (" << (it-foo.begin()) << " elements sorted)\n"; } while (it!=foo.end()); cout << "the range is sorted!\n"; return 0; } Output: foo: 2 3 4 1 (3 elements sorted) foo: 2 3 1 4 (2 elements sorted) foo: 2 1 4 3 (1 elements sorted) foo: 2 1 3 4 (1 elements sorted) foo: 1 4 3 2 (2 elements sorted) foo: 1 4 2 3 (2 elements sorted) foo: 1 3 4 2 (3 elements sorted) foo: 1 3 2 4 (2 elements sorted) foo: 1 2 4 3 (3 elements sorted) foo: 1 2 3 4 (4 elements sorted) the range is sorted!
Next TopicC++ Algorithm
|