is_sort的原型:
| 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原型:
| 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
——————————————————————————————————————————————————————————————————
STL algorithm算法is_sorted和is_sorted_until(28)
原文地址:http://blog.csdn.net/qq844352155/article/details/39337945