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. MPEG-4视频编码核心思想

    1 引言   当今时代,信息技术和计算机互联网飞速发展,在此背景下,多媒体信息已成为人类获取信息的最主要载体,同时也成为电子信息领域技术开发和研究的热点.多媒体信息经数字化处理后具有易于加密.抗干扰能 ...

  2. WIN10配置instantclient

    在PLSQL Developer目录下建立如下bat文件,替换其快捷方式,启动PLSQL Developer: @echo off set path=C:\instantclient-basic-nt ...

  3. windons杀死8080进程

    1,netstat -aon|findstr "8080" //8080端口号 2,taskkill -PID 2976 -F //2976 ,8080端口号对应的进程号

  4. expdp / impdp 用法详解(Oracle)

    一 .关于expdp和impdp     使用EXPDP和IMPDP时应该注意的事项: EXP和IMP是客户端工具程序,它们既可以在客户端使用,也可以在服务端使用. EXPDP和IMPDP是服务端的工 ...

  5. Java-在一个包装器对象中包装一个原始类型

    使用基本类型的包装对象,好处可以为空且可以序列化 package com.tj; public class MyClass2 { public static void main(String[] ar ...

  6. loj2143 「SHOI2017」组合数问题

    大傻逼题--就是求 \(nk\) 个元素选出一些元素,选出的元素的个数要满足模 \(k\) 余 \(r\),求方案数. 想到 \(\binom{n}{m}=\binom{n-1}{m-1}+\bino ...

  7. appium之toast处理

    注意 toast要appium1.6.3以上版本才支持,Android 5.0以上(需使用夜神多开模拟器),jdk1.8且配置了环境变量. toast定位 1.先看下toast长什么样,如下图,像这种 ...

  8. Matplotlib基本图形之折线图

    Matplotlib基本图形之折线图折线图特点 折线图是用折线将各数据连起来组成的图形常用来观察数据随时间变化的趋势例如:股票价格,温度变化,等等 示例代码: import os import tim ...

  9. NYOJ 293 Sticks

    Sticks 时间限制:3000 ms  |  内存限制:65535 KB 难度:5   描述 George took sticks of the same length and cut them r ...

  10. 九度oj 题目1114:神奇的口袋

    题目描述: 有一个神奇的口袋,总的容积是40,用这个口袋可以变出一些物品,这些物品的总体积必须是40.John现在有n个想要得到的物品,每个物品的体积分别是a1,a2……an.John可以从这些物品中 ...