max原型:

std::max

default (1)
template <class T> const T& max (const T& a, const T& b);
custom (2)
template <class T, class Compare>
const T& max (const T& a, const T& b, Compare comp);
initializer list (3)
template <class T> T max (initializer_list<T> il);
template <class T, class Compare>
T max (initializer_list<T> il, Compare comp);

该函数返回范围或者两个数中最大的一个。

对于(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>
ForwardIterator max_element (ForwardIterator first, ForwardIterator last);
custom (2)
template <class ForwardIterator, class Compare>
ForwardIterator max_element (ForwardIterator first, ForwardIterator last,
Compare comp);

返回范围内值最大那个元素的迭代器,假设存在多个同样最大值,则返回第一个。

(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)的更多相关文章

  1. STL algorithm算法merge(34)

    merge原型: std::merge default (1) template <class InputIterator1, class InputIterator2, class Outpu ...

  2. STL algorithm算法mismatch(37)

    mismatch原型: std::mismatch equality (1) template <class InputIterator1, class InputIterator2> p ...

  3. STL algorithm算法is_permutation(27)

    is_permutation原型: std::is_permutation equality (1) template <class ForwardIterator1, class Forwar ...

  4. STL algorithm算法lower_bound和upper_bound(31)

    lower_bound原型: function template <algorithm> std::lower_bound default (1) template <class F ...

  5. STL algorithm算法minmax,minmax_element(36)

    minmax原型: std::minmax C++11 C++14 default (1) template <class T> pair <const T&,const T ...

  6. 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& ...

  7. STL algorithm算法mov,move_backward(38)

    move原型: std::move template <class InputIterator, class OutputIterator> OutputIterator move (In ...

  8. STL algorithm算法make_heap和sort_heap(32)

    make_heap原型: std::make_heap default (1) template <class RandomAccessIterator> void make_heap ( ...

  9. STL algorithm算法lexicographical_compare(30)

    lexicographical_compare原型: std::lexicographical_compare default (1) template <class InputIterator ...

随机推荐

  1. day03变量补充和数据类型

    1.变量的内存管理机制 引用计数:存放值的内存地址绑定的变量个数 垃圾:引用计数为0的内存地址 引用计数增加: age = 18 x = age 引用计数减少: ①age = 19 ②del x 值的 ...

  2. ACM训练联盟周赛 G. Teemo's convex polygon

    65536K   Teemo is very interested in convex polygon. There is a convex n-sides polygon, and Teemo co ...

  3. NYOJ 747 蚂蚁的难题(三)

    蚂蚁的难题(三) 时间限制:2000 ms  |  内存限制:65535 KB 难度:4   描述 蚂蚁终于把尽可能多的食材都搬回家了,现在开始了大厨计划. 已知一共有 n 件食材,每件食材有一个美味 ...

  4. 【原】缓存之 HttpRuntime.Cache

    1.HttpRuntime.Cache HttpRuntime.Cache 相当于就是一个缓存具体实现类,这个类虽然被放在了 System.Web 命名空间下了.但是非 Web 应用也是可以拿来用的. ...

  5. Codeforces Round #416 (Div. 2) 本来以为这个时间是晚上的,下午就没做

    A. Vladik and Courtesy time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  6. 【java基础 8】垃圾收集算法及内存分配策略

    本篇博客,主要介绍GC的收集算法以及根据算法要求所得的内存分配策略! 一.收集算法 收集算法,主要包括四种,分别是:Mark-Sweep(标记-清除).Copying(复制).Mark-Compact ...

  7. mac上安装ruby

    (转:http://www.cnblogs.com/daguo/p/4097263.html) 以下代码区域,带有 $ 打头的表示需要在控制台(终端)下面执行(不包括 $ 符号) 步骤0 - 安装系统 ...

  8. 【bzoj4889】[Tjoi2017]不勤劳的图书管理员 树状数组+分块+二分

    题目描述(转自洛谷) 加里敦大学有个帝国图书馆,小豆是图书馆阅览室的一个书籍管理员.他的任务是把书排成有序的,所以无序的书让他产生厌烦,两本乱序的书会让小豆产生这两本书页数的和的厌烦度.现在有n本被打 ...

  9. [luoguP3159] [CQOI2012]交换棋子(最小费用最大流)

    传送门 好难的网络流啊,建图真的超难. 如果不告诉我是网络流的话,我估计就会写dfs了. 使用费用流解决本题,设点 $p[i][j]$ 的参与交换的次数上限为 $v[i][j]$ ,以下为建图方式: ...

  10. Caffe的Solver参数设置

    Caffe的solver参数设置 http://caffe.berkeleyvision.org/tutorial/solver.html solver是通过协调前向-反向传播的参数更新来控制参数优化 ...