std::max_element
![]() |
このページは、Google 翻訳を使って英語版から機械翻訳されました。
翻訳には誤りや奇妙な言い回しがあるかもしれません。文章の上にポインタをおくと、元の文章が見れます。誤りを修正して翻訳を改善する手助けをしてください。翻訳についての説明は、ここをクリックしてください。 |
Defined in header <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); シグネチャは |
型の要件 | ||
-ForwardIt は ForwardIterator
の要求を満足しなければなりません。 |
[編集] 値を返します
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.
[編集] 複雑
You can help to correct and verify the translation. Click here for instructions.
[編集] 可能な実装
First version |
---|
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; } |
Second version |
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> static bool 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
[編集] も参照してください
範囲内の最小の要素を返します Original: returns the smallest element in a range The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (関数テンプレート) | |
(C++11) |
範囲内の最小と最大の要素を返します Original: returns the smallest and the largest element in a range The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (関数テンプレート) |
二つの要素のうちの大きい方を返します Original: returns the larger of two elements The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (関数テンプレート) |