I came across the old question on is_sorted here: std::is_sorted and strictly less comparison?
after stumbling upon a use case where monotonicity check using a comparator of pairs failed the tests. I thought giving the lambda as below was in line with the requirements: it would return true (only if) both elements of lhs and rhs were sorted
bool
check_pair_sorted(std::vector<std::pair<int, int>>& vec)
{
return std::is_sorted(vec.begin(), vec.end(),[](auto& lhs, auto& rhs){
return (lhs.first < rhs.first) && (lhs.second < rhs.second);
});
}
However, the true intended function is possible if I split the check like so
bool check_separate_sorted(std::vector<std::pair<int, int>>& vec)
{
return std::is_sorted(vec.begin(), vec.end(),[](auto& lhs, auto& rhs){
return (lhs.first < rhs.first) ; })
&& std::is_sorted(vec.begin(), vec.end(),[](auto& lhs, auto& rhs){
return (lhs.second < rhs.second) ; });
}
Looking at the answers in the questions above, where it is suggested an early-return of false might be the preferred implementation, shouldn't the requirement also be posed likewise?
comp - comparison function object (i.e. an object that satisfies the requirements of Compare) which returns false if the first argument is not less than (i.e. is not ordered before) the second.
It's possible that I might be missing something in terms of ordering here, but just can't seem to wrap my head around it at the moment.