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

std::min_element

提供: cppreference.com
< cpp‎ | algorithm
 
 
アルゴリズムライブラリ
実行ポリシー (C++17)
非変更シーケンス操作
(C++11)(C++11)(C++11)
(C++17)
変更シーケンス操作
未初期化記憶域の操作
分割操作
ソート操作
バイナリサーチ操作
集合操作 (ソート済み範囲用)
ヒープ操作
(C++11)
最小/最大演算
min_element
(C++11)
(C++17)
順列
数値演算
C のライブラリ
 
ヘッダ <algorithm> で定義
(1)
template<class ForwardIt >
ForwardIt min_element( ForwardIt first, ForwardIt last );
(C++17以前)
template<class ForwardIt >
constexpr ForwardIt min_element( ForwardIt first, ForwardIt last );
(C++17およびそれ以降)
template<class ExecutionPolicy, class ForwardIt >
ForwardIt min_element( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last );
(2) (C++17およびそれ以降)
(3)
template<class ForwardIt, class Compare >
ForwardIt min_element( ForwardIt first, ForwardIt last, Compare comp );
(C++17以前)
template<class ForwardIt, class Compare >
constexpr ForwardIt min_element( ForwardIt first, ForwardIt last, Compare comp );
(C++17およびそれ以降)
template<class ExecutionPolicy, class ForwardIt, class Compare >
ForwardIt min_element( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, Compare comp );
(4) (C++17およびそれ以降)

範囲 [first, last) 内の最も小さな要素を探します。

1) 要素は operator< を用いて比較されます。
3) 要素は指定された二項比較関数 comp を用いて比較されます。
2,4)(1,3) と同じですが、 policy に従って実行されます。 これらのオーバーロードは、 std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> が true でなければ、オーバーロード解決に参加しません。

目次

[編集]引数

first, last - 調べる範囲を定義する前方イテレータ
policy - 使用する実行ポリシー。 詳細は実行ポリシーを参照してください
comp - ab より小さい場合に true を返す、比較関数オブジェクト (Compare の要件を満たすオブジェクト)。

比較関数のシグネチャは以下と同等であるべきです。

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

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

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

[編集]戻り値

範囲 [first, last) 内の最も小さな要素を指すイテレータ。 範囲内に最も小さな同等な値が複数ある場合は、最初のそのような要素を指すイテレータを返します。 範囲が空の場合は last を返します。

[編集]計算量

ちょうど max(N-1,0) 回の比較、ただし N =std::distance(first, last) です。

[編集]例外

テンプレート引数 ExecutionPolicy を持つオーバーロードは以下のようにエラーを報告します。

  • アルゴリズムの一部として呼び出された関数の実行が例外を投げ、 ExecutionPolicy が3つの標準のポリシーのいずれかの場合は、 std::terminate が呼ばれます。 それ以外のあらゆる ExecutionPolicy については、動作は処理系定義です。
  • アルゴリズムがメモリの確保に失敗した場合は、 std::bad_alloc が投げられます。

[編集]実装例

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

[編集]

#include <algorithm>#include <iostream>#include <vector>   int main(){std::vector<int> v{3, 1, 4, 1, 5, 9};   std::vector<int>::iterator result = std::min_element(std::begin(v), std::end(v));std::cout<<"min element at: "<<std::distance(std::begin(v), result);}

出力:

min element at: 1

[編集]関連項目

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