Namespaces
Variants

std:: min

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17) (C++11)
(C++20) (C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
min
(C++11)
(C++17)
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
ヘッダーで定義 <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
ilist. size ( ) がゼロの場合、または T CopyConstructible でない場合、動作は未定義です。
3) 値の比較に operator < を使用します。
T LessThanComparable でない場合、動作は未定義です。
4) 比較関数 comp を使用して値を比較します。

目次

翻訳の説明: - 「Contents」を「目次」に翻訳しました - C++関連の専門用語(Parameters、Return value、Complexity、Possible implementation、Notes、Example、Defect reports、See also)は原文のまま保持しました - HTMLタグ、属性、クラス名、IDなどは完全に保持されています - 数値や構造は変更していません

パラメータ

a, b - 比較する値
ilist - 比較する値を含む初期化子リスト
cmp - 比較関数オブジェクト(すなわち Compare 要件を満たすオブジェクト)。 true を返す場合は a b より 小さい ことを示す。

比較関数のシグネチャは以下と同等であること:

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

シグネチャが const & を持つ必要はないが、関数は渡されたオブジェクトを変更してはならず、 値カテゴリ に関係なく型 Type1 Type2 のすべての値を受け入れ可能でなければならない(したがって Type1 & は許可されない Type1 Type1 に対してムーブがコピーと等価である場合を除き許可されない (C++11以降) )。
Type1 Type2 は、 T 型のオブジェクトが両方に暗黙変換可能でなければならない。

戻り値

1,2) a b の小さい方の値。値が等価な場合は a を返す。
3,4) ilist 内の最小値。複数の値が最小値と等しい場合、最も左側の値を返します。

計算量

1) 正確に1回の比較を operator < を使用して行う。
2) 比較関数の適用は正確に1回 comp .
3,4) 与えられた N ilist. size ( ) として:
3) 正確に N-1 回の比較を operator < を使用して行う。
4) 厳密に N-1 回の比較関数 comp の適用。

実装例

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);
}

注記

std::min の結果を参照でキャプチャすると、一方のパラメータが一時オブジェクトであり、かつそのパラメータが返される場合、ダングリング参照が生成されます:

int n = -1;
const int& r = std::min(n + 2, n * 2); // r はダングリング参照となる

#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"

不具合報告

以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。

DR 適用対象 公開時の動作 正しい動作
LWG 281 C++98 T CopyConstructible 要件を満たすことが
オーバーロード ( 1,2 ) で要求されていた
要求されない
LWG 2239 C++98
C++11
1. T LessThanComparable 要件を満たすことが
オーバーロード ( 2 ) (C++98) および ( 4 ) (C++11) で要求されていた
2. 計算量要件が欠落していた
1. 要求されない
2. 要件を追加

関連項目

指定された値のうち大きい方を返す
(関数テンプレート)
(C++11)
2つの要素のうち小さい方と大きい方を返す
(関数テンプレート)
範囲内の最小要素を返す
(関数テンプレート)
(C++17)
値を境界値のペアの間にクランプする
(関数テンプレート)
指定された値のうち小さい方を返す
(アルゴリズム関数オブジェクト)