STL algorithm算法min,min_element(35)
min样板:
std::min
| default (1) |
template <class T> const T& min (const T& a, const T& b); |
|---|---|
| custom (2) |
template <class T, class Compare> |
| initializer list (3) |
template <class T> T min (initializer_list<T> il); |
对于(1),返回两个元素中最小的那个,假设两者同样。则返回a.
使用operator<或者comp进行比較。
对于(3),返回最小的那个元素,假设有多个最小元素,则返回第一个。
其行为类似于:
|
|
一个简单的样例:
#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> |
|---|---|
| custom (2) |
template <class ForwardIterator, class Compare> |
返回值最大的元素的迭代器。假设有多个。则返回第一个。
其行为类似于:
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)的更多相关文章
- 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算法minmax,minmax_element(36)
minmax原型: std::minmax C++11 C++14 default (1) template <class T> pair <const T&,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 ...
随机推荐
- Eclipse项目崩溃,使用MyEclipse解决
在今天的项目,Eclipse 在Rwenjian崩溃,导致项目全红 叉 并且不提示任务的错误信息. 无奈之下想起MyEclipse老板. 复制项目MyEclipse文件夹下. 之后,在MyEclip ...
- REDGATE SQLPROMPT 6.0新功能
原文:REDGATE SQLPROMPT 6.0新功能 REDGATE SQLPROMPT 6.0新功能 下载地址:http://files.cnblogs.com/lyhabc/SQLPrompt6 ...
- IOS_多线程_ASI_AFN_UIWebView
H:/0730/00_多线程4票种_ViewController.h // // ViewController.h // 卖票 // // Created by apple on 13-7-29. / ...
- height:100%失败
height显然,设置100% 为什么不能看到效果.非常多的时间不是很扎实的时间的基础上,,经常会遇到这样的问题,原因很简单的事实 首先,你必须确保 html{height:100%;} body{h ...
- android-sdk-windows下载版
Android SDK 4.0.3 开发和执行环境配置 近期又装了一次最新版本号的ADK环境 眼下最新版是Android SDK 4.0.3 本文的插图和文本尽管是Android2.2的 步骤都是一样 ...
- Swing-email(转)
项目结构: 运行效果: 如果你感兴趣,请不要那我的邮箱做测试!!!! ========================================================== 下面是代码部 ...
- 【Web探索之旅】第三部分第三课:协议
内容简介 1.第三部分第三课:协议 2.第四部分预告:Web程序员 第三部分第三课:协议 之前的课,我们学习了Client-Server模型的客户端语言和服务器语言. 客户端语言有HTML,CSS和J ...
- 使用order by和rownum时特别注意
起因 在项目中有用到某表作为数据来源,在页面以列表的形式显示.使用的数据库是Oracle,分页的时候使用到了rownum这个关键字.列表有排序功能,自然也用到了order by.接下来问题出现了,我在 ...
- TI C66x DSP 系统events及其应用 - 5.8(ISTP)
中断服务表指针ISTP(Interrupt Service Table Pointer)位置寄存器用于定位的中断服务例程,那ISTP去哪里找要运行的程序,ISTP(当中的ISTB字段)就是指向IST表 ...
- Android应用开发:LoaderManager在Activity/Fragment中的使用分析
LoaderManager 外部接口initLoader:起始 public <D> Loader<D> initLoader(int id, Bundle args, Loa ...