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 ...
随机推荐
- map标签的详细使用参数
map标签必须成对出现,即 <map> ....</map> 同时map必须和area配合使用. img标签里的usermap属性值必须与map标签里的id和name值完全一致 ...
- find查找大于1M小于10M的文件 $ find . -size +1M -size -10M
查找大于1M小于10M的文件$ find . -size +1M -size -10M
- IIS部署asp.net报404错误
1).所建网站->(右键)权限->"ASP.NET计算机帐户"是否已添加. 2).所建网站->(右键)属性->ASP.NET选项卡->版本是否 ...
- premake 在64位Ubuntu系统下编译32位GCC程序
首先,要安装GCC 4.8, 参考前文:Ubuntu 12.04 & 13.04 安装 GCC4.8.1 其中,重点是安装multilib apt-get install gcc-4.8-mu ...
- Java经典面试题及详解
Java基础方面: 1.作用域public,private,protected,以及不写时的区别 答:区别如下: 作用域 当前类 同一package ...
- poj 3450 Corporate Identity
题目链接:http://poj.org/problem?id=3450 题目分类:后缀数组 题意:求n个串的最长公共字串(输出字串) //#include<bits/stdc++.h> # ...
- java学习笔记10--枚举
java学习笔记10--枚举 在JDK1.5之前,java可以有两种方式定义新类型:类和接口.对于大部分面向对 象编程来说,这两种方法看起来似乎足够了,但是在一些特殊情况下,这些方法就不适合.例如,想 ...
- Java+7入门经典 - 6 扩展类与继承 Part 2/2
6.12 设计类 1) 通过已定义的基类派生子类, 并且添加方法和数据成员来自定义子类, 创建出类的层次结构; Dog 'IS-A' Animal 2) 定义一系列没有层次结构, 由类对象作为数据成员 ...
- 事件总线帧---Otto
我们如果这样一种业务场景.如今在做一款及时聊天应用,我们在聊天页面进行收发信息.同一时候也要实时更新前一页面的聊天记录,这时我们该怎样去实现?说说我曾经的实现策略.我使用的是广播接收器BroadCas ...
- jQuery选择器实现隔行变色和使用javaScript实现隔行变色
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <!--什么是选择器? jQuery选择器继承了 ...