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. 源代码分析:LayoutParams的wrap_content, match_parent, 而详细的价值观

    问题: 慢慢地熟悉android 的过程中.发现view 要么layout初始化,建或者生产活动是很清楚.被添加到父控制,然后开始了相应的生命周期.但父控件的整个界面.还是第一个系统view. 怎么来 ...

  2. 【Win7】【磁盘管理】删除相似“33fbc1d57e9aaf1ea88e6f08”缓存目录

    一般,在计算机磁盘.移动硬盘.或U盘出现,类别似"33fbc1d57e9aaf1ea88e6f08",你不能删除缓存目录.能Win7删除. (1).使用管理员,打开命令提示符窗口( ...

  3. TCP/IP-协议族----17、应用层简单

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGVrZXdhbmd6aQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...

  4. HDU 1051:Wooden Sticks

    Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  5. 重新想象 Windows 8 Store Apps (32) - 加密解密: 非对称算法, 数据转换的辅助类

    原文:重新想象 Windows 8 Store Apps (32) - 加密解密: 非对称算法, 数据转换的辅助类 [源码下载] 重新想象 Windows 8 Store Apps (32) - 加密 ...

  6. A Mathematical Curiosity

    A Mathematical Curiosity Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/O ...

  7. BootStrap布局案例

    BootStrap布局 bootstrap 2.3版与3.0版的使用区别 http://www.weste.net/2013/8-20/93261.html 以一个博客系统的首页,来介绍如何布局 1, ...

  8. android对app代码混淆

    接到新任务.现有项目的代码混淆.在此之前混淆了一些理解,但还不够具体和全面,我知道有些东西混起来相当棘手. 但幸运的是,现在这个项目是不是太复杂(对于这有些混乱).提前完成--这是总结. 第一部分 介 ...

  9. POJ3623:Best Cow Line, Gold(后缀数组)

    Description FJ is about to take his N (1 ≤ N ≤ 30,000) cows to the annual"Farmer of the Year&qu ...

  10. JavaFX它ListView使用

    ListView它是通过同一控制非.在JavaFX在.ListView此外,它拥有非常丰富的功能.下列.让我们来看看如何使用ListView. ListView位于javafx.scene.contr ...