std::is_sorted_until in C++
Last Updated :
18 Feb, 2021
std::is_sorted_until is used to find out the first unsorted element in the range [first, last). It returns an iterator to the first unsorted element in the range, so all the elements in between first and the iterator returned are sorted.
It can also be used to count the total no. of sorted elements in the range. It is defined inside the header file . In case, the whole range is sorted, it will return an iterator pointing to last.
It can be used in two ways as shown below:
Comparing elements using "<":
Syntax:
template
ForwardIterator is_sorted_until (ForwardIterator first, ForwardIterator last);
first: Forward iterator to the first element in the list.
last: forward iterator to the last element in the list.
Return Value: It returns an iterator to the first
unsorted element in the list.
It returns last in case if there is only one element in
the list or if all the elements are sorted.
CPP
// C++ program to demonstrate the use of std::is_sorted_until
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int v[] = { 1, 2, 3, 4, 7, 10, 8, 9 }, i;
int* ip;
// Using std::is_sorted_until
ip = std::is_sorted_until(v, v + 8);
cout << "There are " << (ip - v) << " sorted elements in "
<< "the list and the first unsorted element is " << *ip;
return 0;
}
Output:
There are 6 sorted elements in the list and the first unsorted element is 8
By comparing using a pre-defined function:
Syntax:
template
ForwardIterator is_sorted_until (ForwardIterator first, ForwardIterator last,
Compare comp);
Here, first and last are the same as previous case.
comp: Binary function that accepts two elements in the
range as arguments, and returns a value convertible to bool.
The value returned indicates whether the element passed as
first argument is considered to go before the second in the specific
strict weak ordering it defines.
The function shall not modify any of its arguments.
This can either be a function pointer or a function object.
Return Value: It returns an iterator
to the first unsorted element in the list.
It returns last in case if there is only one element in
the list or if all the elements are sorted.
CPP
// C++ program to demonstrate
// the use of std::nth_element
// C++ program to demonstrate the
// use of std::nth_element
#include <algorithm>
#include <iostream>
using namespace std;
// Defining the BinaryFunction
bool comp(int a, int b) { return (a < b); }
int main()
{
int v[] = { 1, 3, 20, 10, 45, 33, 56, 23, 47 }, i;
int* ip;
// Using std::is_sorted_until
ip = std::is_sorted_until(v, v + 9, comp);
cout << "There are " << (ip - v)
<< " sorted elements in "
<< "the list and the first unsorted element is "
<< *ip;
return 0;
}
Output:
There are 3 sorted elements in the list and the first unsorted element is 10
Similar Reads
is_sorted() in C++ STL In C++, is_sorted() is a built-in function used to check whether the element of the given range is sorted or not in ascending order. In this article, we will learn about is_sorted() function in C++.Letâs take a quick look at a simple example that illustrates is_sorted() method:C++#include <bits/s
4 min read
std::list::sort in C++ STL Lists are containers used in C++ to store data in a non contiguous fashion, Normally, Arrays and Vectors are contiguous in nature, therefore the insertion and deletion operations are costlier as compared to the insertion and deletion option in Lists. list::sort() sort() function is used to sort the
2 min read
sort() in C++ STL In C++, sort() is a built-in function used to sort the given range in desired order. It provides a simple and efficient way to sort the data in C++, but it only works on data structures that provide random access to its elements such as vectors and arrays.Let's take a look at an example:C++#include
4 min read
std::forward_list::sort() in C++ STL Forward list in STL implements singly linked list. Introduced from C++11, forward list are useful than other containers in insertion, removal and moving operations (like sort) and allows time constant insertion and removal of elements.It differs from list by the fact that forward list keeps track of
3 min read
std::is_partitioned in C++ std::is_partitioned is used for finding whether the range[first, last) is partitioned or not. A range is said to be partitioned with respect to a condition if all the elements for which the condition evaluates to true precede those for which it is false. It is defined in the header file . If the ran
3 min read