一行断言测试是否STL容器进行排序 [英] one line assert to test if STL container is sorted
本文介绍了一行断言测试是否STL容器进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有写一行的条件,如果STL容器进行排序,将返回true的方法吗?有问题的容器的std ::矢量
Is there a way to write a one line condition that would return true if STL container is sorted? The container in question is std::vector
我打算断言使用它
推荐答案
使用 adjacent_find 在小于或大于仿函数组合。
Use adjacent_find in combination with less or greater functor.
限制:结果
你应该知道容器是否排序在升序或降序。
Restriction:
You should know whether the container is sorted in ascending or descending.
如果在矢量
是应该按升序进行排序:
If the vector
is supposed to be sorted in ascending order:
//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;
}
这篇关于一行断言测试是否STL容器进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文