std::minmax_element
ヘッダ <algorithm> で定義 | ||
(1) | ||
template<class ForwardIt > std::pair<ForwardIt,ForwardIt> | (C++11およびそれ以降) (C++17以前) | |
template<class ForwardIt > constexprstd::pair<ForwardIt,ForwardIt> | (C++17およびそれ以降) | |
template<class ExecutionPolicy, class ForwardIt > std::pair<ForwardIt,ForwardIt> | (2) | (C++17およびそれ以降) |
(3) | ||
template<class ForwardIt, class Compare > std::pair<ForwardIt,ForwardIt> | (C++11およびそれ以降) (C++17以前) | |
template<class ForwardIt, class Compare > constexprstd::pair<ForwardIt,ForwardIt> | (C++17およびそれ以降) | |
template<class ExecutionPolicy, class ForwardIt, class Compare > std::pair<ForwardIt,ForwardIt> | (4) | (C++17およびそれ以降) |
範囲 [first, last)
内の最も小さな要素と最も大きな要素を探します。
operator<
を用いて比較されます。comp
を用いて比較されます。policy
に従って実行されます。 これらのオーバーロードは、 std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> が true でなければ、オーバーロード解決に参加しません。目次 |
[編集]引数
first, last | - | 調べる範囲を定義する前方イテレータ |
policy | - | 使用する実行ポリシー。 詳細は実行ポリシーを参照してください |
cmp | - | *a が *b より小さい場合に true を返す、比較関数オブジェクト (Compare の要件を満たすオブジェクト)。比較関数のシグネチャは以下と同等であるべきです。 bool cmp(const Type1 &a, const Type2 &b); シグネチャが const& を持つ必要はありませんが、関数オブジェクトは渡されたオブジェクトを変更してはなりません。 |
型の要件 | ||
-ForwardIt は ForwardIterator の要件を満たさなければなりません。 |
[編集]戻り値
第1要素として最も小さな要素を指すイテレータを持ち第2要素として最も大きな要素を指すイテレータを持つペア。 範囲が空の場合は std::make_pair(first, first) を返します。 最も小さな同等な要素が複数ある場合は、最初のそのような要素を指すイテレータが返されます。 最も大きな同等な要素が複数ある場合は、最後のそのような要素を指すイテレータが返されます。
[編集]計算量
多くとも max(floor(3/2(N−1)), 0) 回の述語の適用、ただし N =std::distance(first, last) です。
[編集]例外
テンプレート引数 ExecutionPolicy
を持つオーバーロードは以下のようにエラーを報告します。
- アルゴリズムの一部として呼び出された関数の実行が例外を投げ、
ExecutionPolicy
が3つの標準のポリシーのいずれかの場合は、 std::terminate が呼ばれます。 それ以外のあらゆるExecutionPolicy
については、動作は処理系定義です。 - アルゴリズムがメモリの確保に失敗した場合は、 std::bad_alloc が投げられます。
[編集]ノート
このアルゴリズムは std::make_pair(std::min_element(), std::max_element()) と異なります。 効率性だけでなく、このアルゴリズムは最も大きな最後の要素を探しますが、 std::max_element は最も大きな最初の要素を探します。
[編集]実装例
1つめのバージョン |
---|
2つめのバージョン |
template<class ForwardIt, class Compare>std::pair<ForwardIt, ForwardIt> minmax_element(ForwardIt first, ForwardIt last, Compare comp){std::pair<ForwardIt, ForwardIt> result(first, first); if(first == last)return result;if(++first == last)return result; if(comp(*first, *result.first)){ result.first= first;}else{ result.second= first;}while(++first != last){ ForwardIt i = first;if(++first == last){if(comp(*i, *result.first)) result.first= i;elseif(!(comp(*i, *result.second))) result.second= i;break;}else{if(comp(*first, *i)){if(comp(*first, *result.first)) result.first= first;if(!(comp(*i, *result.second))) result.second= i;}else{if(comp(*i, *result.first)) result.first= i;if(!(comp(*first, *result.second))) result.second= first;}}}return result;} |
[編集]例
#include <algorithm>#include <iostream>#include <vector> int main(){std::vector<int> v ={3, 9, 1, 4, 2, 5, 9}; auto result = std::minmax_element(v.begin(), v.end());std::cout<<"min element at: "<<(result.first- v.begin())<<'\n';std::cout<<"max element at: "<<(result.second- v.begin())<<'\n';}
出力:
min element at: 2 max element at: 6
[編集]関連項目
指定範囲の最も小さな要素を返します (関数テンプレート) | |
指定範囲の最も大きな要素を返します (関数テンプレート) |