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

std::min_element

提供: cppreference.com
< cpp‎ | algorithm

 
 
アルゴリズムライブラリ
機能します
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
シーケンス動作を非改変
Original:
Non-modifying sequence operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
シーケンス動作を変更する
Original:
Modifying sequence operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
操作を仕切る
Original:
Partitioning operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
(ソートされた範囲で)ソート操作
Original:
Sorting operations (on sorted ranges)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
バイナリ検索操作(ソート範囲で)
Original:
Binary search operations (on sorted ranges)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
(ソートされた範囲で)操作を設定します
Original:
Set operations (on sorted ranges)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
ヒープ操作
Original:
Heap operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
最小値/最大値操作
Original:
Minimum/maximum operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
max
max_element
min
min_element
minmax(C++11)
minmax_element(C++11)
数値演算
Original:
Numeric operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Cライブラリ
Original:
C library
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
 
Defined in header <algorithm>
template<class ForwardIt >
ForwardIt min_element( ForwardIt first, ForwardIt last );
(1)
template<class ForwardIt, class Compare >
ForwardIt min_element( ForwardIt first, ForwardIt last, Compare comp );
(2)
レンジ[first, last)で最小の要素を検索します。最初のバージョンは、値を比較するoperator<使用して、2番目のバージョンは、指定された比較関数compを使用しています.
Original:
Finds the smallest element in the range [first, last). The first version uses operator< to compare the values, the second version uses the given comparison function comp.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

目次

[編集]パラメータ

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を含まなくても構いませんが, 比較関数は渡されたオブジェクトを変更してはなりません.
The types Type1 and Type2 must be such that an object of type ForwardIt can be dereferenced and then implicitly converted to both of them. ​

型の要件
-
ForwardItForwardIterator

の要求を満足しなければなりません。

[編集]値を返します

レンジ[first, last)で最小の要素を指すイテレータ。範囲内のいくつかの要素が最小の要素と同等である場合は、まずそのような要素を指すイテレータを返す。戻りlast範囲が空の場合は.
Original:
Iterator to the smallest element in the range [first, last). If several elements in the range are equivalent to the smallest element, returns the iterator to the first such element. Returns last if the range is empty.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[編集]複雑性

まさに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.

[編集]可能な実装

First version
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;}
Second version
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(v.begin(), v.end());std::cout<<"min element at: "<<std::distance(v.begin(), result);}

出力:

min element at: 1

[編集]参照

範囲内の最大の要素を返します
Original:
returns 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.

(関数テンプレート)[edit]
範囲内の最小と最大の要素を返します
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.

(関数テンプレート)[edit]
二つの要素のうち、小さい方を返します
Original:
returns the smaller 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.

(関数テンプレート)[edit]
close