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 ...
随机推荐
- Python9-继承1-day24(大年初一)
#面向对象编程:'''思想:角色的抽象,创建类,创建角色,面对对象的关键字class 类名: 静态属性 = ‘aaa' def __init__(self):pass 类名.静态属性 ———存储在类的 ...
- STM32L0开发——ADC多通道采集,IDE和IAR开发注意事项
keil开发L0系列是免费的,官方提供许可的.因此建议Keil开发,L011F3由于flash只有8K,因此不建议HAL库,建议使用cubemx+LL(或snippets库).0.起初,可以参考官方库 ...
- Pyhton开发:Python基础杂货铺
if 语句 Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块. Python中if语句的一般形式如下所示: if condition_1: stateme ...
- python 多线程、多进程、协程性能对比(以爬虫为例)
基本配置:阿里云服务器低配,单核2G内存 首先是看协程的效果: import requests import lxml.html as HTML import sys import time impo ...
- div的显示隐藏方法汇总
JQuery DIV 动态隐藏和显示的方法 1. 如果在载入是隐藏: <head> <script language="javascript"> funct ...
- Lenovo笔记本电脑进入BIOS的方法
使用NOVO键开机进入BIOS的操作方法 适用范围:2012年后发布的部分笔记本产品,含:IdeaPad全系列.Lenovo G系列部分IdeaPad U或S系列,YOGA/FLEX全系列产品Leno ...
- 也来“玩”Metro UI之磁贴(一)
Win8出来已有一段时间了,个人是比较喜欢Metro UI的.一直以来想用Metro UI来做个自己的博客,只是都没有空闲~~今天心血来潮,突然想自己弄一个磁贴玩玩,动手……然后就有了本篇. Win8 ...
- HDU——2612Find a way(多起点多终点BFS)
Find a way Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- VirtualBox 下主机与虚拟机以及虚拟机之间互通信配置
引用链接:1)http://www.it165.net/os/html/201401/7063.html 2)http://www.cnblogs.com/sineatos/p/4489620.htm ...
- CentOS7关于网络的设置
装好CentOS7后,我们一开始是上不了网的 这时候,可以输入命令dhclient,可以自动获取一个IP地址,再用命令ip addr查看IP 不过这时候获取的IP是动态的,下次重启系统后,IP地址也会 ...