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. android应用开发--------------看RadioGroup源代码,写相似单选选项卡的集成控件(如底部导航,tab等等)

    博客为 有时个哥 原创.如需转载请标明出处:http://blog.csdn.net/ls703/article/details/46694967 watermark/2/text/aHR0cDovL ...

  2. BZOJ 1150 CTSC2007 数据备份Backup 堆+馋

    标题效果:给定一个长度n−1n-1的序列,要求选出kk个不相邻的数使得和最小 费用流显然能跑.并且显然过不去- - 考虑用堆模拟费用流 一个错误的贪心是每次取最小.这样显然过不去例子 我们把[每次取最 ...

  3. 【Unity技能】做一个简单的NPC

    1. 写在前面 前几天我看到cgcookie一个教程.学习了下怎么依据已有人物模型制作一个仿版的NPC人物.感觉挺好玩的,整理一下放到博客里! 先看一下教程里面的终于效果. 是不是非常像个幽灵~ 以下 ...

  4. Web Service简单入门示例

    Web Service简单入门示例     我们一般实现Web Service的方法有非常多种.当中我主要使用了CXF Apache插件和Axis 2两种. Web Service是应用服务商为了解决 ...

  5. sql系列(基础)-第二章 限制和排序数据

    更好的看↑代码点击VIEW PLAN 第二章 约束和排序数据 1. 在 emp 表中选择工资介于 1500 到 2500 的员工的信息: 注意:使用 between 下边界 and 上边界时.条件包括 ...

  6. JSP 获得Spring 注射对象

    <%@ page import="org.springframework.web.context.support.WebApplicationContextUtils"%&g ...

  7. freemarker错误九

    1.错误叙述性说明 五月 30, 2014 11:52:04 下午 freemarker.log.JDK14LoggerFactory$JDK14Logger error 严重: Template p ...

  8. 【Android基础】listview控件的使用(2)-------继承自ListActivity的普通listview

    由于listview在android控件中的重要性,所以android为我们直接封装了一个类ListviewActivity,直接将listview封装在了activity之中,在本篇中,我将介绍在L ...

  9. 什么是Cyclomatic Complexity(圈复杂度)?

    Campwood Software SourceMonitor Version 3.5 The freeware program SourceMonitor lets you see inside y ...

  10. 一个用于每一天JavaScript示例-使用缓存计算(memoization)为了提高应用程序性能

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...