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. 苹果Swift编程语言新手教程【中文版】

    文件夹 1 简单介绍 2 Swift入门 3 简单值 4 控制流 5 函数与闭包 6 对象与类 7 枚举与结构 1 简单介绍 Swift是供iOS和OS X应用编程的新编程语言,基于C和Objecti ...

  2. uva 10817 Headmaster&#39;s Headache 出发dp 位计算

    出发dp,用在一些议题的操作非常~  给出s个课程.m个教师.n个求职者,教师必须招聘.然后招聘一些求职者,使得每一门课都至少有两个老师能教.问题就转换成了招聘哪些求职者使得花费最少.由于s范围小于8 ...

  3. MAC随机修改批处理

    原文:MAC随机修改批处理 @echo off mode con cols=70 lines=20 title MAC随机修改工具 color 3F setlocal enabledelayedexp ...

  4. 怎样配置git ssh连接,怎样在GitHub上加入协作开发人员,怎样配置gitignore和怎样在GitHub上删除资源库.

    **********1.在运行git push origin master指令时报例如以下错误: iluckysi@ILUCKYSI-PC /d/ilucky/message/code (master ...

  5. Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock’

    今天服务器遇到了一个很熟悉的问题 输入 #mysql -u root -p ERROR 2002 (HY000): Can't connect to local MySQL server throug ...

  6. 探索Windows Azure 监控和自动伸缩系列3 - 启用Azure监控扩展收集自定义监控数据

    上一篇我们介绍了获取Azure的监控指标和监控数据: http://www.cnblogs.com/teld/p/5113376.html 本篇我们继续:监控虚拟机的自定义性能计数器. 随着我们应用规 ...

  7. 学习swift语言的快速入门教程推荐

    随着苹果产品越来越火爆,苹果新推出的swift必定将在很大程度上代替oc语言.学好swift语言,对于IOS工程师来讲,已经是一门必备技能. 有一些比较好的英文版教程,值得学习. 1. Swift T ...

  8. 第十二章——SQLServer统计信息(4)——在过滤索引上的统计信息

    原文:第十二章--SQLServer统计信息(4)--在过滤索引上的统计信息 前言: 从2008开始,引入了一个增强非聚集索引的新功能--过滤索引(filter index),可以使用带有where条 ...

  9. 【Leetcode】Partition List (Swap)

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...

  10. 【LeetCode】3Sum 解决报告

    这个问题是我目前的知识回答,不来,只有良好的网上搜索解决方案,发现 K Sum 它是一类问题,但是,互联网是没有更简洁的代码,我想对于谁刚开始学习的人.您可能仍然想看看这个问题该怎么解决,然后看看他们 ...