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 ...
随机推荐
- 使用wireshark常用的过滤命令
使用wireshark常用的过滤命令 方法/步骤 过滤源ip.目的ip.在wireshark的过滤规则框Filter中输入过滤条件.如查找目的地址为192.168.101.8的包,ip.dst==19 ...
- c/c++ 直接使用动态库 dlopen
c/c++ 直接使用动态库 dlopen 把各个版本编译成动态库,××.so ,提供统一的接口进行调用.这里使用的工具是dlxx系列函数 dlopen void *dlopen(const char ...
- Swift - 类和结构体的区别
类和结构体有许多相同之处,也有许多不同之处. 二者区别如下: 1,类可以继承和扩展,结构体不可以 2,可以让一个类的实例来反初始化,释放存储空间,结构体做不到 3,类的对象是引用类型,而结构体是值类型 ...
- Indy的评价
已经抛弃了indy,实在是不好,tcp在android下退出报错.我现在改用系统自带的httpclient.推荐RTC RTC带有一个tcp组件,不过处理方式跟indy不同,测试过,在android下 ...
- 14.3.2.4 Locking Reads 锁定读
14.3.2.4 Locking Reads 锁定读 如果你的查询数据,然后插入或者更新相关的数据 在同一个事务, 普通的SELECT 语句不足以给予足够保护. 其他事务可以更新或者删除相同的你要查询 ...
- ISO/OSI网络体系结构和TCP/IP协议模型
1. ISO/OSI的参考模型共有7层,由低层至高层分别为:物理层.数据链路层.网络层.传输层.会话层.表示层. 应用层.各层功能分别为: (1)物理层 提供建立.维护和拆除 ...
- Json for Java API学习
首先声明:本文来个非常多网友的博客,我通过參考了他们的博客,大致的了解了一些项目中经常使用的Json in java 类和方法,以及关于json的个人理解 个人对json的一些简单理解 在近期的学习中 ...
- 从零开始学C++之动态创建对象
回顾前面的文章,实现了一个简单工厂模式来创建不同类对象,但由于c++没有类似new "Circle"之类的语法,导致CreateShape 函 数中需要不断地ifelse地去判断, ...
- PostgreSQL代码分析,查询优化部分,pull_ands()和pull_ors()
PostgreSQL代码分析,查询优化部分. 这里把规范谓词表达式的部分就整理完了,阅读的顺序例如以下: 一.PostgreSQL代码分析,查询优化部分,canonicalize_qual 二.Pos ...
- ftk学习记(waitbox篇)
[声明:版权全部.欢迎转载,请勿用于商业用途. 联系信箱:feixiaoxing @163.com] 前面说到了脚本.那么就看看ftk中demo与script搭配的效果是什么样的? 上面的效果图就相 ...