min_Element()とmax_element()
所在ヘッダファイル:#include
関数の分類:Min/Max
関数機能:min_element()は、範囲内の[first,last]の最小要素を指す反復器ポインタを返し、max_element()は、範囲内の[first,last]の最小要素を指す反復器ポインタを返します.この条件を満たす要素が1つ以上ある場合、返される反復器は、最初の要素を指します.
min_Element()関数テンプレートの機能は、次のとおりです.
max_Element()関数テンプレートの機能は、次のとおりです.
関数テンプレート:
関数の分類:Min/Max
関数機能:min_element()は、範囲内の[first,last]の最小要素を指す反復器ポインタを返し、max_element()は、範囲内の[first,last]の最小要素を指す反復器ポインタを返します.この条件を満たす要素が1つ以上ある場合、返される反復器は、最初の要素を指します.
min_Element()関数テンプレートの機能は、次のとおりです.
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;
}
max_Element()関数テンプレートの機能は、次のとおりです.
template <class ForwardIterator>
ForwardIterator max_element ( ForwardIterator first, ForwardIterator last )
{
if (first==last) return last;
ForwardIterator largest = first;
while (++first!=last)
if (*largest<*first) // or: if (comp(*largest,*first)) for version (2)
largest=first;
return largest;
}
関数テンプレート:
/**
**author :Or_me **
╭︿︿︿╮
{/ A C /}
( (OO) )
︶︶︶
** **
**min_element/max_element example**
** 2014 7 2 **
**/
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cctype>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
bool myfn(int i, int j)
{
return i<j;
}
struct myclass
{
bool operator() (int i,int j)
{
return i<j;
}
} myobj;
int main ()
{
system("color 5b");
int myints[] = {3,7,2,5,6,4,9};
cout<<"using default comparison:"<<endl;
cout<<"The smallest element is "<<*min_element(myints,myints+7)<<endl;
cout<<"The largest element is " <<*max_element(myints,myints+7)<<endl<<endl;
cout<<"using function myfn as comp:"<<endl;
cout<<"The smallest element is "<<*min_element(myints,myints+7,myfn)<<endl;
cout<<"The largest element is " <<*max_element(myints,myints+7,myfn)<<endl<<endl;
cout<<"using object myobj as comp:"<<endl;
cout<<"The smallest element is "<<*min_element(myints,myints+7,myobj)<<endl;
cout<<"The largest element is " <<*max_element(myints,myints+7,myobj)<<endl<<endl;
return 0;
}