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. 查看zookeeper管理的solrcloud配置文件

    进入zookeeper/bin目录下 连接zookeeper sh zkCli.sh -server localhost:2181 查看 ls /configs 结果如下 [zk: localhost ...

  2. 《C/C++工程师综合练习卷》之小试牛刀

    第一套练习之感受 刚刚注册了牛客网的账号,准备在此衡量一下水平,好好磨练一番.看到推荐练习<C/C++工程师综合练习卷>,oh,20道题,2个小时.由于木有经验,好一番紧张~ 结果用了20 ...

  3. 循环链表的C风格实现(单向)

    头文件: #ifndef _CIRCLELIST_H_ #define _CIRCLELIST_H_ typedef void CircleList; // typedef struct _tag_C ...

  4. Python中的socket网络编程(TCP/IP,UDP)讲解

    在网络编程中的一个基本组件就是套接字(socket).套接字基本上是两个端点的程序之间的"信息通道".程序可能分布在不同的计算机上,通过套接字互相发送信息.套接字包括两个:服务器套 ...

  5. PAT Basic 1070

    1070 结绳 给定一段一段的绳子,你需要把它们串成一条绳.每次串连的时候,是把两段绳子对折,再如下图所示套接在一起.这样得到的绳子又被当成是另一段绳子,可以再次对折去跟另一段绳子串连.每次串连后,原 ...

  6. python基础学习笔记——方法返回值

    字符串中(需要有变量接收) 判断是不是阿拉伯数字,返回的是布尔值 1 2 3 4 name = 'alexdasx' new_name = name.isdigit() print(new_name) ...

  7. 【04】在webstorm里Export declarations are not supported by current JavaScript version

    [04]在webstorm里Export declarations are not supported by current JavaScript version     Export declara ...

  8. luogu3698 [CQOI2017]小Q的棋盘

    最长链是根节点到深度最深的结点的路径. 显然,要么直接走最长链,要么兜兜转转几个圈圈再走最长链,而最长链以外的结点因为要"兜圈",所以要经过两次. #include <ios ...

  9. NYOJ-525一道水题思路及详解

    一道水题 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描述 今天LZQ在玩一种小游戏,但是这游戏数有一点点的大,他一个人玩的累,想多拉一些人进来帮帮他,你能写一个程序帮帮他 ...

  10. JSPatch安全部署

    前言 这个事JSPatch集成到客户端的第二篇,第一篇链接:http://www.cnblogs.com/hxwj/p/5163158.html 安全部署链接:http://blog.cnbang.n ...