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.
其行为类似如下:
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)
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);
|
该函数类似与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
——————————————————————————————————————————————————————————————————
分享到:
相关推荐
algorithm algorithm STL 算法 algorithm_头文件_说明 algorithm algorithm STL 算法 algorithm_头文件_说明 algorithm algorithm STL 算法 algorithm_头文件_说明
用于三维光学形貌扫描完成后,生成的stl文件的读取,并形成俯视投影云图
stlshow_stl分层_STL分层_stlmatlab_STL切片_stl分层.zip
C++读取STL文件,输出所有三角形的顶点坐标
STL课件_C++_课件stl_c++课件_源码.zip
STL算法(for_each/transform)
stlshow_stl分层_STL分层_stlmatlab_STL切片_stl分层_源码.rar.rar
09_07_IO_STL标准模板09_07_IO_STL标准模板09_07_IO_STL标准模板09_07_IO_STL标准模板09_07_IO_STL标准模板09_07_IO_STL标准模板09_07_IO_STL标准模板09_07_IO_STL标准模板09_07_IO_STL标准模板09_07_IO_STL标准模板...
stl的入门ppt 很好用哦,分为导入 STL的概念与组成 Iterator(迭代器) Container(容器) Algorithm(算法) Adaptors(配接器)
STL学习教程,
STL 最新源代码
C++ STL 参考手册Cpp_STL_ReferenceManual ,对学习STL 有帮助
SGI STL是由SGI公司开发的STL库.这份STL可以用在VC上.
vc显示stl,读取顶点坐标,并显示在独立窗口中
在MATLAB中完成STL数据的渲染,用PATCH函数完成加颜色和亮度等功能
最新的STL源码,最新的STL源码,最新的STL源码
读取、显示STL格式的3D文件,可对二进制的STL文件进行读取、显示。显示模式可以选择有网格、无网格等。
看STL文件的小软件,可以自由的实现旋转,等功能,现在只是一小部分,以后会发后面的
十多个STL的经典例子,相当不错,初学STL必备
读取 点云数据 STL 文件 分块化编程