STL algorithmi算法s_sorted和is_sorted_until(28)
is_sort原型:
::is_sorted
| default (1) |
template <class ForwardIterator>
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.
其行为类似例如以下:
|
|
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(int argv,char **argc)
{
vector<int> v1{1,2,3,4};
vector<double> v2{4.0,5.0,3.5,6.0};
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 <class ForwardIterator> 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);
|
返回第一个破坏序列有序的元素迭代器。
使用operator<或者comp来进行比較。
其行为类似与:
|
|
一个简单的样例:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(int argv,char **argc)
{
vector<int> v1{1,2,3,4,5};
vector<double> v2{4.0,5.0,3.5,6.0,1.0};
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
——————————————————————————————————————————————————————————————————
版权声明:本文博客原创文章,博客,未经同意,不得转载。
浙公网安备 33010602011771号