C++ std::is_sorted_until with std::forward_list














































C++ std::is_sorted_until with std::forward_list



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 forward_list 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 <forward_list> // forward_list
using namespace std;

/*
procedure to call is_sorted_until
*/

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

int main () {
forward_list<int> fl1 {2,4,8,9,1,99};
forward_list<int> fl2 {1,3,9,12,28,99};
check(fl1);
check(fl2);
return 0;
}

OUTPUT:-





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

Comments