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; }

执行结果:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXE4NDQzNTIxNTU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

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

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


版权声明:本文博客原创文章,博客,未经同意,不得转载。

STL algorithmi算法s_sorted和is_sorted_until(28)的更多相关文章

  1. 常用的STL查找算法

    常用的STL查找算法 <effective STL>中有句忠告,尽量用算法替代手写循环:查找少不了循环遍历,在这里总结下常用的STL查找算法: 查找有三种,即点线面: 点就是查找目标为单个 ...

  2. 【STL】帮你复习STL泛型算法 一

    STL泛型算法 #include <iostream> #include <vector> #include <algorithm> #include <it ...

  3. STL基础--算法(排序)

    STL排序算法 排序算法要求随机访问迭代器 vector, deque, container array, native array 例子 vector<int> vec = {9,1,1 ...

  4. C++复习:STL之算法

    算法 1算法基础 1.1算法概述 算法部分主要由头文件<algorithm>,<numeric>和<functional>组成. <algorithm> ...

  5. STL所有算法简介 (转) http://www.cnblogs.com/yuehui/archive/2012/06/19/2554300.html

    STL所有算法简介 STL中的所有算法(70个) 参考自:http://www.cppblog.com/mzty/archive/2007/03/14/19819.htmlhttp://hi.baid ...

  6. 初探STL之算法

    算法 STL算法部分主要由头文件<algorithm>,<numeric>,<functional>组成.要使用 STL中的算法函数必须包括头文件<algor ...

  7. [C++ STL] 常用算法总结

    1 概述 STL算法部分主要由头文件<algorithm>,<numeric>,<functional>组成.要使用 STL中的算法函数必须包含头文件<alg ...

  8. stl变易算法(三)

    本篇接着前面stl变易算法(一)和stl变易算法(二)继续讲述变易算法. 这里将介绍完余下的变易算法,主要有:填充fill.n次填充fill_n.随机生成元素generate.随机生成n个元素gene ...

  9. 28.STL常用算法

    #include <algorithm> 算法 常用版本 描述 返回Type std::find() find(_InIt _Fisrt,_InIt _Last, _Ty& _Va ...

随机推荐

  1. HDU 4435 charge-station (并查集)

    先说下题目的意思: 在一个二维坐标系中有N个点,某人要来个走遍所有点的旅行,但是他的车每次加油后只能走M个单位距离:所以要在这个N点中选一些建立加油站:问题来了:i^th  点 建加油站的花费是  2 ...

  2. 无阻塞情况connect生产EINPROGRESS错

    今天的游戏开发client测试程序,非常多,因为出现client.后connect成功.代码证recv系统调用.后来发现,可能是由于socket默认模式被阻止,这将使很多client 接处于链接却不能 ...

  3. java ClassLoader static

    package init; class Person { private static Person person = new Person(); public static int count2 = ...

  4. JavaScript的"类"

    1. 基本创建“类”方式 var Class = function(){ var klass = function(){ this.init.apply(this, arguments); }; kl ...

  5. Xcode_6.3_beta_4 官方 下载地址

    http://adcdownload.apple.com//Developer_Tools/Xcode_6.3_beta_4/Xcode_6.3_beta_4.dmg

  6. Lua 环境结构 --Linux

    curl -R -O http://www.lua.org/ftp/lua-5.2.3.tar.gz tar zxf lua-5.2.3.tar.gz cd lua-5.2.3 make linux ...

  7. iOS 中国排序

    这里分享一个中国某种方便的方法,我们放在一起的人脉资源后,方便的类别,使用自己的包, 此处所使用的方法贴,源代码可以在本文的结尾下载. 要记得加头文件 #import "NSArray+So ...

  8. 使用JMX实现的内存监控(转)

    public final class MemoryWarningSystem { private static MemoryWarningSystem m_instance = null; /** * ...

  9. Action、Category、Data、Extras知识具体解释

    开头 Intent作为联系各Activity之间的纽带,其作用并不仅仅仅仅限于简单的数据传递.通过其自带的属性,事实上能够方便的完毕非常多较为复杂的操作.比如直接调用拨号功能.直接自己主动调用合适的程 ...

  10. jquery声明

    $("[id^=total_item]")代表 id随着total_item开始XX..必须jquery支持 版权声明:本文博主原创文章,博客,未经同意,不得转载.