Consider the following C++ program which checks whether the given array is sorted or not.
#include <iostream>
#include <vector>
#include <algorithm>
int main()
{
std::vector<float> array = {1.1, 2.2,3.3, 3.3, 5.5};
std::cout<<std::is_sorted(array.begin(), array.end(), [](float a, float b){std::cout<<"a: "<<a<<"b: "<<b<<std::endl; return a < b;});
return 0;
}
Since there are two equal values, I expected that is_sorted
would return false
. But to my surprise, it returned true
. If I change a<b
to a<=b
, it returns true
!
I also expected that the parameters in the lambda function would be in the order of the original array. But it turned out the other way. That is, a=2.2
and b=1.1
in the first iteration.
Can somebody explain these behaviors?