The Wayback Machine - https://web.archive.org/web/20180513052752/http://ja.cppreference.com:80/w/cpp/algorithm/max_element
名前空間
変種
操作

std::max_element

提供: cppreference.com
< cpp‎ | algorithm

 
 
アルゴリズムライブラリ
実行ポリシー (C++17)
非変更シーケンス操作
(C++11)(C++11)(C++11)
(C++17)
変更シーケンス操作
未初期化記憶域の操作
分割操作
ソート操作
バイナリサーチ操作
集合操作 (ソート済み範囲に対する)
ヒープ操作
(C++11)
最小/最大演算
max_element
(C++11)
(C++17)
順列
数値演算
C のライブラリ
 
ヘッダ <algorithm> で定義
template<class ForwardIt >
ForwardIt max_element( ForwardIt first, ForwardIt last );
(1)
template<class ForwardIt, class Compare >
ForwardIt max_element( ForwardIt first, ForwardIt last, Compare comp );
(2)

Finds the greatest element in the range [first, last). The first version uses operator< to compare the values, the second version uses the given comparison function comp.

目次

[編集]パラメータ

first, last -
検討する範囲を定義前方反復子
Original:
forward iterators defining the range to examine
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
cmp - 比較関数. if *a is less than *b 場合、 ​trueを返します.

比較関数のシグネチャは以下と同等でなければなりません.

 bool cmp(const Type1 &a, const Type2 &b);

シグネチャはconstを含まなくても構いませんが, 比較関数は渡されたオブジェクトを変更してはなりません.
Type1 および Type2 は、どちらも ForwardIt 型のオブジェクトの逆参照から暗黙に変換可能なものでなければなりません。 ​

型の要件
-
ForwardItForwardIterator の要件を満たさなければなりません。

[編集]値を返します

Iterator to the greatest element in the range [first, last). If several elements in the range are equivalent to the greatest element, returns the iterator to the first such element. Returns last if the range is empty.

[編集]複雑性

まさにmax(N-1,0)比較、どこN =std::distance(first, last).
Original:
Exactly max(N-1,0) comparisons, where N =std::distance(first, last).
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[編集]可能な実装

1つめのバージョン
template<class ForwardIt> ForwardIt max_element(ForwardIt first, ForwardIt last){if(first == last){return last;} ForwardIt largest = first;++first;for(; first != last;++first){if(*largest <*first){ largest = first;}}return largest;}
2つめのバージョン
template<class ForwardIt, class Compare> ForwardIt max_element(ForwardIt first, ForwardIt last, Compare comp){if(first == last){return last;} ForwardIt largest = first;++first;for(; first != last;++first){if(comp(*largest, *first)){ largest = first;}}return largest;}

[編集]

#include <algorithm>#include <iostream>#include <vector>#include <cmath>   staticbool abs_compare(int a, int b){return(std::abs(a)< std::abs(b));}   int main(){std::vector<int> v{3, 1, -14, 1, 5, 9};std::vector<int>::iterator result;   result = std::max_element(v.begin(), v.end());std::cout<<"max element at: "<<std::distance(v.begin(), result)<<'\n';   result = std::max_element(v.begin(), v.end(), abs_compare);std::cout<<"max element (absolute) at: "<<std::distance(v.begin(), result);}

出力:

max element at: 5 max element (absolute) at: 2

[編集]参照

指定範囲の最も小さな要素を返します
(関数テンプレート)[edit]
指定範囲の最も小さな要素と最も大きな要素を返します
(関数テンプレート)[edit]
指定された値の大きい方を返します
(関数テンプレート)[edit]
close