STL algorithm算法min,min_element(35)
来源:互联网 发布:淘宝智能客服 编辑:程序博客网 时间:2024/06/07 11:00
min原型:
std::min
- C++98
- C++11
- C++14
template <class T> const T& min (const T& a, const T& b);
template <class T, class Compare> const T& min (const T& a, const T& b, Compare comp);
template <class T> T min (initializer_list<T> il);template <class T, class Compare> T min (initializer_list<T> il, Compare comp);
使用operator<或者comp进行比较。
对于(3),返回最小的那个元素,如果有多个最小元素,则返回第一个。
其行为类似于:
23
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;}运行截图:
min_element原型:
std::min_element
template <class ForwardIterator> ForwardIterator min_element (ForwardIterator first, ForwardIterator last);
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;}运行截图:
——————————————————————————————————————————————————————————————————
//写的错误或者不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。
转载请注明出处:http://blog.csdn.net/qq844352155
author:天下无双
Email:coderguang@gmail.com
2014-9-17
于GDUT
——————————————————————————————————————————————————————————————————
0 0
- STL algorithm算法min,min_element(35)
- STL 之count,count_if,max,max_element,min,min_element和random_shuffle
- STL 之count,count_if,max,max_element,min,min_element和random_shuffle
- 泛型算法系列16:max()&&min()&&min_element()&&max_element()
- STL algorithm算法集合
- 【STL】algorithm算法介绍
- STL常用算法: max_element,min_element, distance以及手动实现.
- stl min_element和max_element
- STL max_element,min_element
- STL algorithm算法binary_search(5)
- STL algorithm算法copy(6)
- STL algorithm算法copy_backward(7)
- STL algorithm算法copy_if(8)
- STL algorithm算法copy_n(9)
- STL algorithm算法count(10)
- STL algorithm算法count_if(11)
- STL algorithm算法equal(12)
- STL algorithm算法equal_range(13)
- 网页结构基础2
- hdu 2838(树状数组求逆序数)
- mysql递归查询树形的叶子
- poj 2135 (费用流) 水题
- android 如何隐藏应用程序图标
- STL algorithm算法min,min_element(35)
- ORACLE 12C PDB 维护基础介绍
- 利用自关联查找所有的blocking session
- 详细解说STL hash_map系列
- hdu4358 树形转线性 线段树
- 进程、线程、任务
- 【DP】 HDOJ 4933 Miaomiao's Function
- C++ 子类能不能改变父类中成员的访问权限?
- Eclipse与Android源码编译过程(针对如何将第三方so文件打包进apk中)