aJ.*_*aJ. 23
将adjacent_find与更少或更多的仿函数结合使用.
限制:
您应该知道容器是按升序还是按降序排序.
如果vector应该按升序排序:
//Checks the first element where adjacent value where elem > nextElem
//returns end if the vector is sorted!
//Complexity is O(n)
vector<int>::iterator pos = std::adjacent_find (aVec.begin(), aVec.end(), // range
std::greater<int>());
if (pos == aVec.end())
{
std::cout<<" sorted"<<endl;
}
else
{
std::cout<<"Not sorted"<<endl;
}
Run Code Online (Sandbox Code Playgroud)
您可以使用std :: is_sorted(vec.begin(),vec.end())来测试它是否已排序.但请注意,这是O(n).