C++ std::is_sorted_until with std::multiset














































C++ std::is_sorted_until with std::multiset



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

/*
procedure to call is_sorted_until
*/

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

int main () {
multiset<int> ms1 {2,4,8,9,1,99};
multiset<int> ms2 {1,3,9,12,28,99};
check(ms1);
check(ms2);
return 0;
}

OUTPUT:-



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


Comments