C++ std::is_sorted_until with std::set














































C++ std::is_sorted_until with std::set



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 set 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
28
// is_sorted_until example
#include <iostream>
#include <algorithm> // is_sorted_until
#include <set> // set
using namespace std;

/*
procedure to call is_sorted_until
*/

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

int main () {
set<int> s1 {2,4,8,9,1,99};
set<int> s2 {1,3,9,12,28,99};
check(s1);
check(s2);
return 0;
}


OUTPUT:-



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

Comments