C++ Primer 学习中。。。

简单记录下我的学习过程 (代码为主)

min_element、max_element  找最小、最大值。 非常easy没什么大作用



#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; /*******************************************************************************************
template <class ForwardIterator>
ForwardIterator min_element ( ForwardIterator first, ForwardIterator last ); template <class ForwardIterator, class Compare>
ForwardIterator min_element ( ForwardIterator first, ForwardIterator last,
Compare comp ); eg:
template <class ForwardIterator>
ForwardIterator min_element ( ForwardIterator first, ForwardIterator last )
{
ForwardIterator lowest = first;
if (first==last) return last;
while (++first!=last)
if (*first<*lowest) // or: if (comp(*first,*lowest)) for the comp version
lowest=first;
return lowest;
}
********************************************************************************************/ /********************************************************************************************
template <class ForwardIterator>
ForwardIterator max_element ( ForwardIterator first, ForwardIterator last ); template <class ForwardIterator, class Compare>
ForwardIterator max_element ( ForwardIterator first, ForwardIterator last,
Compare comp );
eg:
template <class ForwardIterator>
ForwardIterator max_element ( ForwardIterator first, ForwardIterator last )
{
ForwardIterator largest = first;
if (first==last) return last;
while (++first!=last)
if (*largest<*first) // or: if (comp(*largest,*first)) for the comp version
largest=first;
return largest;
}
********************************************************************************************/ bool myfn(int i, int j)
{
return i<j;
} struct myclass
{
bool operator() (int i,int j)
{
return i<j;
}
} myobj; int main ()
{
int myints[] = {3,7,2,5,6,4,9}; // using default comparison:
cout << "The smallest element is " << *min_element(myints,myints+7) << endl;
cout << "The largest element is " << *max_element(myints,myints+7) << endl; // using function myfn as comp: 函数
cout << "The smallest element is " << *min_element(myints,myints+7,myfn) << endl;
cout << "The largest element is " << *max_element(myints,myints+7,myfn) << endl; // using object myobj as comp: 函数对象
cout << "The smallest element is " << *min_element(myints,myints+7,myobj) << endl;
cout << "The largest element is " << *max_element(myints,myints+7,myobj) << endl; return 0;
} /*********
Output:
The smallest element is 2
The largest element is 9
The smallest element is 2
The largest element is 9
The smallest element is 2
The largest element is 9
*********/

STL_算法_最小值和最大值(min_element、max_element)的更多相关文章

  1. cb27a_c++_STL_算法_最小值和最大值

    cb27a_c++_STL_算法_最小值和最大值min_element(b,e) b--begin(), e--end()min_element(b,e,op). op:函数,函数对象,一元谓词.ma ...

  2. STL_算法_中使用的函数对象

    写在前面: STL算法中的 函数对象的功能: (1).都是提供一种比较的函数,比较相邻的左右两个值的 相等/大小 等的关系, (2).返回值都是bool :该返回值 貌似是指明 遍历元素是否还要继续往 ...

  3. STL_算法_查找算法(lower_bound、upper_bound、equal_range)

    C++ Primer 学习中. .. 简单记录下我的学习过程 (代码为主) //全部容器适用(O(log(n)))    已序区间查找算法 lower_bound()        //找第一个符合的 ...

  4. STL_算法_查找算法(find、find_if)

    C++ Primer 学习中. .. 简单记录下我的学习过程 (代码为主) find . find_if /**********************线性查找O(n) find(); find_if ...

  5. STL_算法_逆转(reverse,reverse_copy)

    C++ Primer 学习中.. . 简单记录下我的学习过程 (代码为主) //全部容器适用 reverse(b,e)        //逆转区间数据 reverse_copy(b,e,b2) /** ...

  6. STL_算法_依据第n个元素排序(nth_element)

    C++ Primer 学习中... 简单记录下我的学习过程 (代码为主) //全部容器适用 nth_element(b,n,e) nth_element(b,n,e,p) 对照:partition() ...

  7. STL_算法_查找算法(binary_search、includes)

    C++ Primer 学习中.. . 简单记录下我的学习过程 (代码为主) 全部容器适用(O(log(n)))     已序区间查找算法 binary_search             //二分查 ...

  8. STL_算法_局部排序(partial_sort、partial_sort_copy)

    C++ Primer 学习中. . . 简单记录下我的学习过程 (代码为主) /***************************************** // partial_sort(b, ...

  9. STL_算法_区间的比較(equal、mismatch、 lexicographical_compare)

    C++ Primer 学习中.. . 简单记录下我的学习过程 (代码为主) //全部容器适用 equal(b,e,b2)       //用来比較第一个容器[b,e)和第二个容器b2开头,是否相等 e ...

随机推荐

  1. apidoc利用代码注释书写文档

    个人博客同步文章 https://mr-houzi.com/2018/07/... apidoc是一款利用源代码中注释来创建RESTful Web API文档的工具.apidoc可用于C#,Go,Da ...

  2. rspec测试(使用guard自动测试和spork加速测试)配置

    Gemfile文件添加rspec.guard和spork,之后执行bundle install命令 gem 'rb-readline' group :development, :test do # C ...

  3. HTML5表单新增元素与属性

    form属性 在html4中,表单的从属元素必须写在表单内部,而在HTML5中,可以把他们书写在任何地方,然后为该元素指定一个form属性,属性值为该表单的id,这样就可以声明该元素从属于指定表单了. ...

  4. python 连接docker

    发现15年写过的一小段,先mark一下,后续玩儿起来 import docker c = docker.Client(base_url='unix://var/run/docker.sock',ver ...

  5. 《算法导论》— Chapter 11 散列表

    1 序 在很多应用中,都要用到一种动态集合结构,它仅支持INSERT.SEARCH以及DELETE三种字典操作.例如计算机程序设计语言的编译程序需要维护一个符号表,其中元素的关键字为任意字符串,与语言 ...

  6. 牛客网 牛可乐发红包脱单ACM赛 B题 小a的旅行计划

    [题解] 题意其实就是把n个物品分成4个集合,其中三个集合不可以为空(只属于A.只属于B.AB的交),一个集合空或者非空都可以(不属于A也不属于B),问有多少种方案. 考虑容斥,4个集合都不为空的方案 ...

  7. 大数据学习——Linux上常用软件安装

    4.1 Linux系统软件安装方式 Linux上的软件安装有以下几种常见方式: 1.二进制发布包 软件已经针对具体平台编译打包发布,只要解压,修改配置即可 2.RPM发布包 软件已经按照redhat的 ...

  8. loadrunner 场景设计-手工场景设计

    概述 通过选择需要运行的脚本,分配运行脚本的负载生成器,在脚本中分配Vuser来建立手工场景 手工场景就是自行设置虚拟用户的变化,主页是通过设计用户的添加和减少过程,来模拟真实的用户请求模型,完成负载 ...

  9. POJ 2553 The Bottom of a Graph 【scc tarjan】

    图论之强连通复习开始- - 题目大意:给你一个有向图,要你求出这样的点集:从这个点出发能到达的点,一定能回到这个点 思路:强连通分量里的显然都可以互相到达 那就一起考虑,缩点后如果一个点有出边,一定不 ...

  10. HDU3572:Task Schedule【最大流】

    上了一天课 心塞塞的 果然像刘老师那么说 如果你有挂科+4级没过 那基本上是WF队 题目大意:有时间补吧 思路:给每个任务向每个时间点连边容量为1 每个时间点向汇点连边 容量为机器的个数 源点向每个任 ...