minmax原型:

std::minmax

default (1)
template <class T>
pair <const T&,const T&> minmax (const T& a, const T& b);
custom (2)
template <class T, class Compare>
pair <const T&,const T&> minmax (const T& a, const T& b, Compare comp);
initializer list (3)
template <class T>
pair<T,T> minmax (initializer_list<T> il);
template <class T, class Compare>
pair<T,T> minmax (initializer_list<T> il, Compare comp);

该函数返回一个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>
pair<ForwardIterator,ForwardIterator>
minmax_element (ForwardIterator first, ForwardIterator last);
custom (2)
template <class ForwardIterator, class Compare>
pair<ForwardIterator,ForwardIterator>
minmax_element (ForwardIterator first, ForwardIterator last, Compare comp);

该函数是返回指定范围内的最大最小值的元素的迭代器组成的一个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; }

执行截图:

能够看出,firstf返回的是第一个出现的最小值的迭代器。second返回的是最后一个出现的最大值的迭代器。


——————————————————————————————————————————————————————————————————

//写的错误或者不好的地方请多多指导。能够在以下留言或者点击左上方邮件地址给我发邮件。指出我的错误以及不足,以便我改动,更好的分享给大家,谢谢。

转载请注明出处:http://blog.csdn.net/qq844352155

author:天下无双

Email:coderguang@gmail.com

2014-9-18

于GDUT

——————————————————————————————————————————————————————————————————


STL algorithm算法minmax,minmax_element(36)的更多相关文章

  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算法min,min_element(35)

    min样板: std::min C++98 C++11 C++14 default (1) template <class T> const T& min (const T& ...

  6. 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& ...

  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. OCM读书笔记(2) - PL/SQL 基础

    1. % type 用法,提取% type所在字段的类型 declare     myid dept.deptno % type;    myname dept.dname % type;begin  ...

  2. go iota

    package main import ( "fmt" ) const ( a = 'A' b c = iota d ) func main() { fmt.Println(a) ...

  3. QT插件开发方式(没看懂)

    创建一个QT的库项目,删除自动生成的.h和.cpp文件,添加一个接口定义.h文件和一个接口实现类(一个.h一个.cpp).代码如下: 1.接口文件源码 #ifndef PLUGININTERFACE_ ...

  4. Windbg 32位版本和64位版本的选择

    习惯了Vsiual Studio的兄弟们可能会因为先入为主的原因以为所有的调试器都应该像它那样,其实不然,当你安装Debugging Tools for Windows的时候,你将发现有两个系列的工具 ...

  5. CodeForces 13E. Holes 分块处理

    正解是动态树,太难了,仅仅好分块处理水之.看了看status大概慢了一倍之多..     分块算法大体就是在找一个折衷点,使得查询和改动的时间复杂度都不算太高,均为o(sqrt(n)),所以总的时间复 ...

  6. 字符串string和内存流MemoryStream及比特数组byte[]互转

    原文:字符串string和内存流MemoryStream及比特数组byte[]互转   字符串string和内存流MemoryStream及比特数组byte[]互转比较 定义string变量为str, ...

  7. 快速学会搭建SVN服务器

    原文:快速学会搭建SVN服务器 SVN是一个版本控制工具,常用于我们软件开发项目中,用来管理我们团队共同使用的代码,文档等历史版本的管理,保持代码的更新,避免混乱. 需要工具: svn安装程序:免费下 ...

  8. 使用和制作patch文件

    使用和制作patch文件 发表时间: 2007-2-13 20:57    作者: superuser    来源: 迷茫人 字体: 小 中 大 | 打印 原文http://www.linuxsir. ...

  9. php获取分类以下的全部子类方法

    获取分类以下的全部子类方法: static function getMenuTree($arrCat, $parent_id = 0, $level = 0,$all=True) { static $ ...

  10. jQuery中的getJSON()

    json文件是一种轻量级的数据交互格式.一般在jQuery中使用getJSON()方法读取. $.getJSON(url,[data],[callback]) url:json文件地址 data:可选 ...