有没有一种方法可以编写一个单行条件,如果 STL 容器已排序,该条件将返回 true?有问题的容器是 std::vector
我打算在断言中使用它
Use 相邻查找 http://en.cppreference.com/w/cpp/algorithm/adjacent_find与较小或较大函子结合。
限制:
您应该知道容器是按升序还是降序排序。
If the 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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)