STLalgorithm算法min,min_element(35)
min原型:
std::min
- C++98
- C++11
- C++14
default (1)
template
custom (2)
template <class T, class Compare>
const T& min (const T& a, const T& b, Compare comp);
initializer list (3)
template
template <class T, class Compare>
T min (initializer_list
对于(1),返回两个元素中最小的那个,如果两者相同,则返回a.
使用operator<或者comp进行比较。
对于(3),返回最小的那个元素,如果有多个最小元素,则返回第一个。
其行为类似于:
1 | 2 |
1 | template <class T> const T& min (const T& a, const T& b) { |
一个简单的例子:
#include
#include
#include
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
default (1)
template
ForwardIterator min_element (ForwardIterator first, ForwardIterator last);
custom (2)
template <class ForwardIterator, class Compare>
ForwardIterator min_element (ForwardIterator first, ForwardIterator last,
Compare comp);
返回值最大的元素的迭代器,如果有多个,则返回第一个。
其行为类似于:
template
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
#include
#include
using namespace std;
void minelement(){
vector
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
——————————————————————————————————————————————————————————————————
- 本文作者: royalchen
- 本文链接: http://www.royalchen.com/2016/02/24/stlalgorithm算法minmin-element35/
- 版权声明: 本博客所有文章除特别声明外,均采用 MIT 许可协议。转载请注明出处!