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 ...
 
随机推荐
- Connecting Docker for Cloud Services using SDN and Network Virtualization
			
 Abstract The explosive scale of container CPUs needs highly efficient network virtualization Chal ...
 - 重新想象 Windows 8 Store Apps (4) - 控件之提示控件: ProgressRing; 范围控件: ProgressBar, Slider
			
原文:重新想象 Windows 8 Store Apps (4) - 控件之提示控件: ProgressRing; 范围控件: ProgressBar, Slider [源码下载] 重新想象 Wind ...
 - 【leetCode百题成就】Gas Station解题报告
			
题目: There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. ...
 - ios pop 折叠动画
			
今天写了一个很有趣的电影太,我们可以去githoub下载. 这部动画是高级写作,我参考了它.而凝视,我希望你能看的懂. 各种动画.事实上,一些不起眼的开始.我也只是摸索. 我希望有更多的交流.[ ...
 - Objective-C代码块语法(block)使用
			
和其它变量本质上相似的代码块.所不同的是,数据存储代码块是主体的函数. 使用的代码块被,你可以像打电话一样等标准功能,传入参数的数量,并获得返回值. 插入符号(^)语法标记块.我们熟悉的参数按照规约定 ...
 - effective c++  条款12 copy all parts of an object
			
这经常发生在更改代码的时候,当有自己的copy 赋值函数或者copy 构造函数时,编译器就不会维护这两个函数.导致发生遗忘. 可能出现的场景 class Customer { private: std ...
 - nodejs添加路由route步骤详解
			
首先,毋庸置疑, 新建一个基础express站点.建好之后, 有三个文件需要code(当然,三个步骤顺序随意). 下面以添加一个'/about'为例说明. 1. 在views文件夹里,新建一个jade ...
 - Hdu-1565 电网接入(1) (国家压缩dp获得冠军
			
正方形格通路(1) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
 - 长方柱类【C++ 类定义】
			
Description 编写基于对象的程序,求长方柱(Bulk)的体积.数据成员包括长(length).宽(width).高(heigth).体积,要求用成员函数实现下面的功能: (1)由键盘输入长方 ...
 - iOS的图表显示的实现
			
在app通常有家居展览的照片,显示广告.或者头条新闻.通常网易新闻client 如图,红框框的位置就是一个典型的图展, 熟悉iOS的人肯定知道,这个是个UIScrollview,里面加几张图片就可以实 ...