STLalgorithm算法is_sorted和is_sorted_until(28)
is_sort的原型:
::is_sorted
default (1)
template
bool is_sorted (ForwardIterator first, ForwardIterator last);
custom (2)
template <class ForwardIterator, class Compare>
bool is_sorted (ForwardIterator first, ForwardIterator last, Compare comp);
该函数是测试范围内的元素是否已经有序!
使用operator<或者comp来进行比较。
如果范围内的元素个数少于两个,总是返回true.
其行为类似如下:
1 | 1 |
1 | template <class ForwardIterator> |
一个简单的测试例子:
#include
#include
#include
using namespace std;
int main(int argv,char **argc)
{
vector
vector
cout<<”v1=”;
for(int i:v1)
cout<<i<<” “;
cout<<endl;
if(is_sorted(v1.begin(),v1.end()))
cout<<”v1 is sorted!”<<endl;
else
cout<<”v1 is not sorted!”<<endl;
cout<<”v2=”;
for(double i:v2)
cout<<i<<” “;
cout<<endl;
if(is_sorted(v2.begin(),v2.end()))
cout<<”v2 is sorted!”<<endl;
else
cout<<”v2 is not sorted!”<<endl;
}
运行结果:
is_sorted_until原型:
std::is_sorted_until
default (1)
template
ForwardIterator is_sorted_until (ForwardIterator first, ForwardIterator last);
custom (2)
template <class ForwardIterator, class Compare>
ForwardIterator is_sorted_until (ForwardIterator first, ForwardIterator last,
Compare comp);
该函数类似与is_heap和is_heap_until的关系。
返回第一个破坏序列有序的元素迭代器。
使用operator<或者comp来进行比较。
其行为类似与:
1 | 2 |
1 | template <class ForwardIterator> |
一个简单的例子:
#include
#include
#include
using namespace std;
int main(int argv,char **argc)
{
vector
vector
cout<<”v1=”;
for(int i:v1)
cout<<i<<” “;
cout<<endl;
cout<<”v2=”;
for(double i:v2)
cout<<i<<” “;
cout<<endl;
auto it=is_sorted_until(v1.begin(),v1.end());
if(it==v1.end())
cout<<”v1 is sorted!”<<endl;
auto it2=is_sorted_until(v2.begin(),v2.end());
cout<<”v2 the return is “<<*it2<<endl;
}
运行截图:
通过对比源代码可以知道,第一个返回的是v1.end(),第二个返回的则是3.5!
——————————————————————————————————————————————————————————————————
//写的错误或者不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。
转载请注明出处:http://blog.csdn.net/qq844352155
author:天下无双
Email:coderguang@gmail.com
2014-9-17
于GDUT
——————————————————————————————————————————————————————————————————
- 本文作者: royalchen
- 本文链接: http://www.royalchen.com/2016/02/24/stlalgorithm算法is-sorted和is-sorted-until28/
- 版权声明: 本博客所有文章除特别声明外,均采用 MIT 许可协议。转载请注明出处!