min样板:

std::min

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

对于(1),返回两个元素中最小的那个,假设两者同样。则返回a.

使用operator<或者comp进行比較。

对于(3),返回最小的那个元素,假设有多个最小元素,则返回第一个。

其行为类似于:

2
3
template <class T> const T& min (const T& a, const T& b) {
return !(b<a)? a:b; // or: return !comp(b,a)?a:b; for version (2)
}

一个简单的样例:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void min2(){
cout<<"min(10,22)="<<min(10,22)<<endl;
cout<<"min({1,2,5,7,9,999,888})="<<min({1,2,5,7,9,999,888})<<endl;
}

执行截图:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXE4NDQzNTIxNTU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

min_element原型:

std::min_element

default (1)
template <class ForwardIterator>
ForwardIterator min_element (ForwardIterator first, ForwardIterator last);
custom (2)
template <class ForwardIterator, class Compare>
ForwardIterator min_element (ForwardIterator first, ForwardIterator last,
Compare comp);

返回值最大的元素的迭代器。假设有多个。则返回第一个。

其行为类似于:

template <class ForwardIterator>
ForwardIterator min_element ( ForwardIterator first, ForwardIterator last )
{
if (first==last) return last;
ForwardIterator smallest = first; while (++first!=last)
if (*first<*smallest) // or: if (comp(*first,*smallest)) for version (2)
smallest=first;
return smallest;
}

一个简单的样例:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void minelement(){
vector<int> vi{1,1,2,3,4};
cout<<" vi=";
for(int i:vi)
cout<<i<<" ";
cout<<endl;
cout<<"min_element(vi.begin(),vi.end())="<<*min_element(vi.begin(),vi.end())<<endl;
cout<<"min_element(vi.begin(),vi.begin()+1)="<<*min_element(vi.begin(),vi.begin()+1)<<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算法min,min_element(35)的更多相关文章

  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算法minmax,minmax_element(36)

    minmax原型: std::minmax C++11 C++14 default (1) template <class T> pair <const T&,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. Objective C (iOS) for Qt C++ Developers(iOS开发,Qt开发人员需要了解什么?)

    Qt/C++开发人员眼中的Obj-C      对于我们第一次自己定义iOS应用来说,对于来自Qt/C++开发人员来说,我不得不学习Objective-C相关语法与知识 为了让读者可以更easy理解这 ...

  2. 开源Math.NET基础数学类库使用(16)C#计算矩阵秩

    原文:[原创]开源Math.NET基础数学类库使用(16)C#计算矩阵秩                本博客所有文章分类的总目录:http://www.cnblogs.com/asxinyu/p/4 ...

  3. Quartz CronTrigger应用

    CronTrigger配置格式: 格式: [第二] [支] [小时] [日本] [月] [周] [年]  序号 说明  是否必填  同意填写的值 同意的通配符  1  秒  是  0-59    , ...

  4. SICP 锻炼 (2.15)解决摘要:深入思考间隔误差

    SICP 2.15 是接着 题目 2.14 的, 题目 2.14中提到了Alyssa设计的区间计算模块在并联电阻计算时会出现故障,这个问题是Lem发现的. 接着,一个叫Eva的人也发现了这个问题.同一 ...

  5. 抓取csdn上的各类别的文章 (制作csdn app 二)

    转载请表明出处:http://blog.csdn.net/lmj623565791/article/details/23532797 这篇博客接着上一篇(Android 使用Fragment,View ...

  6. android imageButton 透明图片

    在Android有许多不规则button.例如: 这个时候,我们假设想做成不规则button的话.第一步就是搞一张边缘透明的png图片,然后用src指定到他.这个时候我们会发现,还没有达到要的效果.还 ...

  7. Ubuntu14.04下安装ZendStudio10.6.1+SVN出现Failed to load JavaHL Library

    Subclipse不能正常工作,打开后报错: Failed to load JavaHL Library. These are the errors that were encountered: no ...

  8. 玩转Web之Json(二)----jquery easy ui + Ajax +Json+SQL实现前后台数据交互

    最近在学Json,在网上也找过一些资料,觉得有点乱,在这里,我以easy ui的登录界面为例来说一下怎样用Json实现前后台的数据交互 使用Json,首先需要导入一些jar包,这些资源可以在网上下载到 ...

  9. java提高篇(十二)-----equals()

    equals() 超类Object中有这个equals()方法,该方法主要用于比较两个对象是否相等.该方法的源码如下: public boolean equals(Object obj) { retu ...

  10. ExtJS4 根据分配不同的树形菜单在不同的角色登录后

    继续我的最后.建立cookie后,带他们出去 var userName = Ext.util.Cookies.get('userName'); var userAuthority = Ext.util ...