C++ std::is_sorted_until with std::array














































C++ std::is_sorted_until with std::array



what does it do?

Returns iterator to the first element in a range [first, last) after which ascending order is violated.
NOTE: - if the iterator is equal to the end() of the container then the range is sorted

SYNTAX:-


version 1


template <class IteratorLoc>
ForwardIterator is_sorted_until
(IteratorLoc first, IteratorLoc last);

version 2

template <class IteratorLoc, class Compare_function>
ForwardIterator is_sorted_until
(IteratorLoc first, IteratorLoc last, Compare_function function_name);



PARAMETERS-

first - iterator to the first position of the  range.
last - iterator of the last position of the range.
function_name - is the function pointer of the bool fucntion which returns true or false after comparision.

Complexity-
Linear i.e, O(n)

Link to repository-
https://github.com/pyskmr/D4datastructures/tree/master/CPP%20algorithm%20header%20file/is_sorted_until

IN THIS ARTICLE WE ARE SEEING HOW TO USE THIS ALGORITH HEADER FUCNTION ON array CONTAINER.


 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// is_sorted_until example
#include <iostream>
#include <algorithm> // is_sorted_until
#include <array> // array
using namespace std;

/*
procedure to call is_sorted_until
*/

void check(array<int,6> a)
{
for(int i=0;i<6;i++)
{
cout<<a[i]<<" ";
}
array<int,6>::iterator i=is_sorted_until(a.begin(),a.end()); //you can also give your range here
i!=a.end()?cout<<"\nafter "<<*i<<" the range is unsorted\n\n":cout<<"\nsorted\n\n";
}

int main () {
array<int,6> arr1 {2,4,8,9,1,99};
array<int,6> arr2 {1,3,9,12,28,99};
check(arr1);
check(arr2);
return 0;


OUTPUT:-





NOTE :- cant be implemented with any container which do not support iterator like, stack and queue.

Comments