STL algorithm算法minmax,minmax_element(36)
minmax原型:
std::minmax
| default (1) |
template <class T> |
|---|---|
| custom (2) |
template <class T, class Compare> |
| initializer list (3) |
template <class T> |
该函数返回一个pair,该pair的first元素的值为a,b中的最小值,second元素的值为a.b中的最大值。
使用operator<进行比較。
假设a=b,那么将返回make_pair(a,b).
对于(3)。返回初始化列表中的最小最大值的pair,假设最值多于一个。firstf返回的是第一个出现的最小值。second返回的是最后一个出现的最大值。
其行为类似于:
template <class T> pair <const T&,const T&> minmax (const T& a, const T& b) {
return (b<a) ? std::make_pair(b,a) : std::make_pair(a,b);
}
一个简单的样例:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void minmax2(){
auto it=minmax(20,10);
cout<<" auto it=minmax(20,10)"<<endl;
cout<<"it.first()="<<it.first<<" ,it.second="<<it.second<<endl;
auto it2=minmax({2,3,5,7,1,3,1});
cout<<"auto it2=minmax({2,3,5,7,1,3,1})"<<endl;
cout<<"it2.first()="<<it2.first<<" ,it2.second="<<it2.second<<endl;
}
执行截图:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXE4NDQzNTIxNTU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">
minmax_element原型:
std::minmax_element
| default (1) |
template <class ForwardIterator> |
|---|---|
| custom (2) |
template <class ForwardIterator, class Compare> |
该函数是返回指定范围内的最大最小值的元素的迭代器组成的一个pair,假设最值多于一个,firstf返回的是第一个出现的最小值的迭代器,second返回的是最后一个出现的最大值的迭代器。
使用operator<进行比較。
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void minmaxelement(){
vector<int> vi{3,5,4,1,3,1,9,9,5};
cout<<"vi=";
for(int i:vi)
cout<<i<<" ";
cout<<endl;
auto it=minmax_element(vi.begin(),vi.end());
cout<<" auto it=minmax_element(vi.begin(),vi.end())"<<endl;
cout<<"*it.first="<<*it.first<<" ,*it.second="<<*it.second<<endl;
cout<<"*(it.first-1)="<<*(it.first-1)<<" ,*(it.second-1)="<<*(it.second-1)<<endl; }
执行截图:
——————————————————————————————————————————————————————————————————
//写的错误或者不好的地方请多多指导。能够在以下留言或者点击左上方邮件地址给我发邮件。指出我的错误以及不足,以便我改动,更好的分享给大家,谢谢。
转载请注明出处:http://blog.csdn.net/qq844352155
author:天下无双
Email:coderguang@gmail.com
2014-9-18
于GDUT
——————————————————————————————————————————————————————————————————
STL algorithm算法minmax,minmax_element(36)的更多相关文章
- 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算法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算法max,max_elements(33)
max原型: std::max C++98 C++11 C++14 default (1) template <class T> const T& max (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 ...
随机推荐
- Swift - 控制流/控制结构说明(if,switch,for,while)
1,if语句 1 2 3 4 5 if count >=3 { println("yes") }else{ println("no") } ...
- 在VC++中启用内存泄露检测
检测内存泄漏的主要工具是调试器和 CRT 调试堆函数.若要启用调试堆函数,请在程序中包括以下语句: #define CRTDBG_MAP_ALLOC#include <stdlib.h># ...
- Java Web Services (0) - Overview
前言第1章 Web服务快速入门 1.1 Web服务杂项 1.2 Web服务有什么好处 1.3 Web服务和面向服务的架构 1.4 Web服务简史 1.4.1 从DCE/RPC到XML-RPC 1.4. ...
- shodan
https://www.shodan.io/ from:http://www.exploit-db.com/wp-content/themes/exploit/docs/33859.pdf 0x00 ...
- 菜鸟玩云计算之十一:Hadoop 手动安装指南
Hadoop 手动安装指南 cheungmine 2013-4 本文用于指导在Windows7,VMWare上安装Ubuntu, Java, Hadoop, HBase实验环境. 本指南用于实验的软件 ...
- symbol(s) not found for architecture i386
此问题针对百度地图真机调试和模拟器.a文件的选取问题 "$(SRCROOT)/MobileYonyou/Third/BaiduMap_IOSSDK_v2.3.0_Lib/Release$(E ...
- Selenium: 空指针error
Error 类型:空指针 可能原因一: 只是引用了该类,但是没有对该类进行实例化(即没有New 一下),即没有给该类分配内存,所以导致空指针: 类调用前注意要实例化,否则会导致空指针错误. 首先声明D ...
- uva 1434 - YAPTCHA(数论)
题目链接:uva 1434 - YAPTCHA 题目大意:给定n和k,求题目中给定的式子S(n). 解题思路:威尔逊定理,x为素数时有,((x−1)!+1)%x==0,所以对于本题.假设3*k+7为素 ...
- hdu1255(线段树——矩形面积交)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1255 题意:求N个矩形中,求被覆盖至少俩次的面积和 分析:覆盖两次即col[rt]>=2就好.一 ...
- tomcat开始批量——setclasspath.bat
除了上述两批,另一个重要的脚本,那是,setclasspath.bat.它主要负责查找.检查JAVA_HOME和JRE_HOME两个变量. ****************************** ...