function template
<algorithm>
std::is_sorted default (1)template <class ForwardIterator> bool is_sorted (ForwardIterator first, ForwardIterator last);custom (2)
template <class ForwardIterator, class Compare> bool is_sorted (ForwardIterator first, ForwardIterator last, Compare comp);
Check whether range is sorted
Returnstrue
if the range [first,last)
is sorted into ascending order.
The elements are compared using operator<
for the first version, and comp for the second.
The behavior of this function template is equivalent to:
1
2
3
4
5
6
7
8
9
10
11
12
template <class ForwardIterator>
bool is_sorted (ForwardIterator first, ForwardIterator last)
{
if (first==last) return true;
ForwardIterator next = first;
while (++next!=last) {
if (*next<*first) // or, if (comp(*next,*first)) for version (2)
return false;
++first;
}
return true;
}
[first,last)
, which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
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.
true
if the range [first,last)
is sorted into ascending order, false
otherwise.
If the range [first,last)
contains less than two elements, the function always returns true
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// is_sorted example
#include <iostream> // std::cout
#include <algorithm> // std::is_sorted, std::prev_permutation
#include <array> // std::array
int main () {
std::array<int,4> foo {2,4,1,3};
do {
// try a new permutation:
std::prev_permutation(foo.begin(),foo.end());
// print range:
std::cout << "foo:";
for (int& x:foo) std::cout << ' ' << x;
std::cout << '\n';
} while (!std::is_sorted(foo.begin(),foo.end()));
std::cout << "the range is sorted!\n";
return 0;
}
foo: 2 3 4 1 foo: 2 3 1 4 foo: 2 1 4 3 foo: 2 1 3 4 foo: 1 4 3 2 foo: 1 4 2 3 foo: 1 3 4 2 foo: 1 3 2 4 foo: 1 2 4 3 foo: 1 2 3 4 the range is sorted!
[first,last)
are accessed.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4