STL algorithm算法max,max_elements(33)
max原型:
std::max
| default (1) |
template <class T> const T& max (const T& a, const T& b); |
|---|---|
| custom (2) |
template <class T, class Compare> |
| initializer list (3) |
template <class T> T max (initializer_list<T> il); |
该函数返回范围或者两个数中最大的一个。
对于(1),假设两个数相等,则返回a;
其行为类似于:
template <class T> const T& max (const T& a, const T& b) {
return (a<b)?
b:a; // or: return comp(a,b)?b:a; for version (2)
}
一个简单的样例:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void max2(){
cout<<"max(10,22)="<<max(10,22)<<endl;
cout<<"max({1,2,5,7,9,999,888})="<<max({1,2,5,7,9,999,888})<<endl;
}
执行截图:
max_elements原型:
std::max_element
| default (1) |
template <class ForwardIterator> |
|---|---|
| custom (2) |
template <class ForwardIterator, class Compare> |
返回范围内值最大那个元素的迭代器,假设存在多个同样最大值,则返回第一个。
(max返回的是元素,这个返回的是迭代器)
假设范围为空,则返回last.
使用operator<进行比較。
其行为类似于:
template <class ForwardIterator>
ForwardIterator max_element ( ForwardIterator first, ForwardIterator last )
{
if (first==last) return last;
ForwardIterator largest = first; while (++first!=last)
if (*largest<*first) // or: if (comp(*largest,*first)) for version (2)
largest=first;
return largest;
}
一个简单的样例:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void maxelement(){
vector<int> vi{1,1,2,3,4};
cout<<"at first vi=";
for(int i:vi)
cout<<i<<" ";
cout<<endl;
cout<<"max_element(vi.begin(),vi.end())="<<*max_element(vi.begin(),vi.end())<<endl;
cout<<"max_element(vi.begin(),vi.begin()+1)="<<*max_element(vi.begin(),vi.begin()+1)<<endl;
if(max_element(vi.end(),vi.end())==vi.end())
cout<<"max_element(vi.end(),vi.end())=vi.end()"<<endl; }
执行结果:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXE4NDQzNTIxNTU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">
——————————————————————————————————————————————————————————————————
//写的错误或者不好的地方请多多指导。能够在以下留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足。以便我改动,更好的分享给大家,谢谢。
转载请注明出处:http://blog.csdn.net/qq844352155
author:天下无双
Email:coderguang@gmail.com
2014-9-17
于GDUT
——————————————————————————————————————————————————————————————————
STL algorithm算法max,max_elements(33)的更多相关文章
- STL algorithm算法merge(34)
merge原型: std::merge default (1) template <class InputIterator1, class InputIterator2, class Outpu ...
- STL algorithm算法mismatch(37)
mismatch原型: std::mismatch equality (1) template <class InputIterator1, class InputIterator2> p ...
- STL algorithm算法is_permutation(27)
is_permutation原型: std::is_permutation equality (1) template <class ForwardIterator1, class Forwar ...
- STL algorithm算法lower_bound和upper_bound(31)
lower_bound原型: function template <algorithm> std::lower_bound default (1) template <class F ...
- STL algorithm算法minmax,minmax_element(36)
minmax原型: std::minmax C++11 C++14 default (1) template <class T> pair <const T&,const T ...
- STL algorithm算法min,min_element(35)
min样板: std::min C++98 C++11 C++14 default (1) template <class T> const T& min (const T& ...
- STL algorithm算法mov,move_backward(38)
move原型: std::move template <class InputIterator, class OutputIterator> OutputIterator move (In ...
- STL algorithm算法make_heap和sort_heap(32)
make_heap原型: std::make_heap default (1) template <class RandomAccessIterator> void make_heap ( ...
- STL algorithm算法lexicographical_compare(30)
lexicographical_compare原型: std::lexicographical_compare default (1) template <class InputIterator ...
随机推荐
- 1、初学探讨PYTHON的itchat和wxpy两库
最近好奇学习了python,觉得简单明了,但是最头疼的就是调整空格和调试吧,的确调试不如C#使用visual studio 方便,都是使用print()来调试.也许因为我是菜鸟,如果大家还有更好的方法 ...
- pandas中Timestamp类用法讲解
由于网上关于Timestamp类的资料比较少,而且官网上面介绍的很模糊,本文只是对如何创建Timestamp类对象进行简要介绍,详情请读者自行查阅文档. 以下有两种方式可以创建一个Timestamp对 ...
- 1 producer — n consumers 模型 实现
#include<stdio.h> #include<string.h> #include<pthread.h> #include<stdlib.h> ...
- HTML5 移动端web
概述 HTML5 提供了很多新的功能,主要有: 新的 HTML 元素,例如 section, nav, header, footer, article 等 用于绘画的 Canvas 元素 用于多媒体播 ...
- es6--之箭头函数
「箭头函数」是 ECMAScript6 中非常重要的性特性.很多文章都在描述它的上下文透明性以及短语法.新特性必然会带来很多好处,但凡事都有两面性.本篇文章会通过情景引导,让你知晓哪些情景下应该绕过箭 ...
- Mysql 学习目录
Mysql 目录 Mysql之路[第一篇]:Mysql基础 Mysql之路[第二篇]:Mysql 常用命令 Mysql之路[第三篇]:Python对Mysql的操作 Mysql之路[第四篇]:ORM ...
- appium之toast处理
注意 toast要appium1.6.3以上版本才支持,Android 5.0以上(需使用夜神多开模拟器),jdk1.8且配置了环境变量. toast定位 1.先看下toast长什么样,如下图,像这种 ...
- PHP cannoy modify header information - headers already sent by ....
我采用的是MVC模式的写法,代码和html分离的写法 <?php require '../mysql_connect.php'; require('../model/functions.php' ...
- BZOJ 2720 [Violet 5]列队春游 ——期望DP
很喵的一道题(我可不是因为看了YOUSIKI的题解才变成这样的) $ans=\sum_{x<=n}\sum_{i<=n} iP(L=i)$ 其中P(x)表示视线为x的概率. 所以只需要求出 ...
- SPOJ QTREE4 Query on a tree IV ——动态点分治
[题目分析] 同bzoj1095 然后WA掉了. 发现有负权边,只好把rmq的方式改掉. 然后T了. 需要进行底(ka)层(chang)优(shu)化. 然后还是T 下午又交就A了. [代码] #in ...