std::min
From cppreference.com
| ヘッダー <algorithm> で定義 |
||
template< class T > const T& min( const T& a, const T& b ); |
(1) | (C++14以降constexpr) |
template< class T, class Compare > const T& min( const T& a, const T& b, Compare comp ); |
(2) | (C++14以降constexpr) |
template< class T > T min( std::initializer_list<T> ilist ); |
(3) | (C++11以降) (C++14以降constexpr) |
template< class T, class Compare > T min( std::initializer_list<T> ilist, Compare comp ); |
(4) | (C++11以降) (C++14以降constexpr) |
与えられた値のうち小さい方を返します。
1,2) a と b のうち小さい方を返します。
1) operator< を使用して値を比較します。
もし
TがLessThanComparableでない場合、動作は未定義です。2) 比較関数 comp を使用して値を比較します。
3,4) 初期化リスト ilist の値のうち最小のものを返します。
3) operator< を使用して値を比較します。
もし
TがLessThanComparableでない場合、動作は未定義です。4) 比較関数 comp を使用して値を比較します。
目次 |
[編集] Parameters
| a, b | - | 比較する値 |
| ilist | - | 比較する値を含む初期化子リスト |
| cmp | - | 比較関数オブジェクト(すなわち、Compare の要件を満たすオブジェクト)で、a が b より *小さい* 場合に true を返します。 比較関数のシグネチャは、以下と同等でなければならない。 bool cmp(const Type1& a, const Type2& b); シグネチャにconst&は必須ではないが、関数は渡されたオブジェクトを変更してはならず、値カテゴリに関係なく、 |
[編集] Return value
1,2) a と b のうち小さい方。値が等しい場合は a を返します。
3,4) ilist の中の最小値。最小値と等しい値が複数ある場合、最も左にあるものを返します。
[編集] Complexity
1) operator< を用いた厳密に1回の比較。
2) 比較関数 comp の厳密に1回の適用。
3,4) ilist.size() をNとする。
3) N-1 回の operator< を用いた比較。
4) N-1 回の比較関数 comp の適用。
[編集] Possible implementation
| min (1) |
|---|
template<class T> const T& min(const T& a, const T& b) { return (b < a) ? b : a; } |
| min (2) |
template<class T, class Compare> const T& min(const T& a, const T& b, Compare comp) { return (comp(b, a)) ? b : a; } |
| min (3) |
template<class T> T min(std::initializer_list<T> ilist) { return *std::min_element(ilist.begin(), ilist.end()); } |
| min (4) |
template<class T, class Compare> T min(std::initializer_list<T> ilist, Compare comp) { return *std::min_element(ilist.begin(), ilist.end(), comp); } |
[編集] Notes
std::min の結果を参照でキャプチャすると、パラメータのいずれかが一時オブジェクトであり、その一時オブジェクトが返される場合に、ダングリング参照が生じます。
int n = -1; const int& r = std::min(n + 2, n * 2); // r is dangling
[編集] Example
このコードを実行
#include <algorithm> #include <iostream> #include <string_view> int main() { std::cout << "smaller of 10 and 010 is " << std::min(10, 010) << '\n' << "smaller of 'd' and 'b' is '" << std::min('d', 'b') << "'\n" << "shortest of \"foo\", \"bar\", and \"hello\" is \"" << std::min({"foo", "bar", "hello"}, [](const std::string_view s1, const std::string_view s2) { return s1.size() < s2.size(); }) << "\"\n"; }
出力
smaller of 10 and 010 is 8 smaller of 'd' and 'b' is 'b' shortest of "foo", "bar", and "hello" is "foo"
[編集] Defect reports
以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。
| DR | 適用対象 | 公開された動作 | 正しい動作 |
|---|---|---|---|
| LWG 281 | C++98 | オーバーロード (1,2) では、`T` が CopyConstructible である必要がありました。 | 要求されない |
| LWG 2239 | C++98 C++11 |
1. オーバーロード (2) (C++98) および (4) (C++11) では、`T` が LessThanComparable である必要がありました。 オーバーロード (2) (C++98) および (4) (C++11) 2. 複雑性に関する要件が欠落していました。 |
1. 必要ない 2. 要件が追加されました。 |
[編集] See also
| 与えられた値のうち大きい方を返す (関数テンプレート) | |
| (C++11) |
2つの要素の小さい方と大きい方を返す (関数テンプレート) |
| 範囲内で最小の要素を返す (関数テンプレート) | |
| (C++17) |
値を一対の境界値の間に制限する (関数テンプレート) |
| (C++20) |
与えられた値のうち小さい方を返す (アルゴリズム関数オブジェクト) |