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. linux下安装oracle11g 64位最简客户端(转)

    安装环境 Linux服务器:SuSe11 sp1 64位           Oracle客户端:Oracle11gR2 64位(最简客户端) 部署流程 1.准备工作,首先在oracle官网下载最新的 ...

  2. Copy-and-swap

    为了在自己定义类里重载一个异常安全(exception safe)的赋值操作符.创造了一个这种习惯用语.也叫:Create-Temporary-and-Swap. 要想写健壮的C++代码.异常安全很重 ...

  3. SendMail如何签名

    MailAddress类有两个参数 第1个参数:发送者的邮箱 第2个参数:发送者的签名 示例: MailMessage message = new MailMessage();message.From ...

  4. StringUtils.isNumeric(String str) 的一个坑(转)

    在项目中遇到一处bug,调试的结果竟然是StringUtils.isNumeric(String str) 在捣鬼(采用的是org.apache.commons.lang.StringUtils),下 ...

  5. Channel Allocation (poj 1129 dfs)

    Language: Default Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12 ...

  6. [LeetCode257]Binary Tree Paths

    题目: Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree ...

  7. linux 下安装mysql

    看以下这篇文章就够了   http://www.cnblogs.com/xiaoluo501395377/archive/2013/04/07/3003278.html 查看有没有安装过:       ...

  8. HDU 2454 Degree Sequence of Graph G(Havel定理 推断一个简单图的存在)

    主题链接:pid=2454">http://acm.hdu.edu.cn/showproblem.php?pid=2454 Problem Description Wang Haiya ...

  9. POJ 2112 Optimal Milking (二分 + floyd + 网络流)

    POJ 2112 Optimal Milking 链接:http://poj.org/problem?id=2112 题意:农场主John 将他的K(1≤K≤30)个挤奶器运到牧场,在那里有C(1≤C ...

  10. shuffle一个简单的过程叙述性说明

    shuffle它是在map和reduce过程之间.我们看看在这个过程中的步骤,了解在这个问题上不深,有可能是一个错误.忘记修正 1. map map出口key,value,里的context.writ ...