`
阿尔萨斯
  • 浏览: 4178661 次
社区版块
存档分类
最新评论

STL algorithm算法is_sorted和is_sorted_until(28)

 
阅读更多

is_sort的原型:

::is_sorted

default (1)custom (2)
template <class ForwardIterator>
  bool is_sorted (ForwardIterator first, ForwardIterator last);
template <class ForwardIterator, class Compare>
  bool is_sorted (ForwardIterator first, ForwardIterator last, Compare comp);
该函数是测试范围内的元素是否已经有序!

使用operator<或者comp来进行比较。

如果范围内的元素个数少于两个,总是返回true.

其行为类似如下:

1
2
3
4
5
6
7
8
9
10
11
12
template <class ForwardIterator>
  bool is_sorted (ForwardIterator first, ForwardIterator last)
{
  if (first==last) return true;
  ForwardIterator next = first;
  while (++next!=last) {
    if (*next<*first)     // or, if (comp(*next,*first)) for version (2)
      return false;
    ++first;
  }
  return 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) custom (2)
template <class ForwardIterator>
  ForwardIterator is_sorted_until (ForwardIterator first, ForwardIterator last);
template <class ForwardIterator, class Compare>
  ForwardIterator is_sorted_until (ForwardIterator first, ForwardIterator last,
                                   Compare comp);
该函数类似与is_heap和is_heap_until的关系。

返回第一个破坏序列有序的元素迭代器。

使用operator<或者comp来进行比较。

其行为类似与:
2
3
4
5
6
7
8
9
10
11
template <class ForwardIterator>
  ForwardIterator is_sorted_until (ForwardIterator first, ForwardIterator last)
{
  if (first==last) return first;
  ForwardIterator next = first;
  while (++next!=last) {
    if (*next<*first) return next;
    ++first;
  }
  return last;
}

一个简单的例子:

#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

——————————————————————————————————————————————————————————————————










分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics