Talk:cpp/algorithm/is sorted
From cppreference.com
1 1 3 4 5 is not an ascending order, it's a non-descending order. The wording in the standard is correct, your wording is not. The fact that is_sorted returns true for this example is proof of that.
[edit] Why is n non-negative
In "a sequence is sorted with respect to a comparator comp if for any iterator it pointing to the sequence and any non-negative integer n such that it + n is a valid iterator pointing to an element of the sequence, comp(*(it + n), *it) evaluates to false," if n=0, and comp is std::less_equal<>(), wouldn't comp(*(it + 0), *it) be true, so is_sorted will always return false? Should n be a positive integer instead?
- std::less_equal isn't a valid comparator as it doesn't establish a strict weak ordering (required by the Compare requirement). Note that even if
n
was defined to be a strictly positive integer, it would still be the case thatcomp(*(it + 1), *it)
would evaluate totrue
if*it == *(it + 1)
. --Ybab321 (talk) 07:56, 27 September 2020 (PDT)