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 ( ) 가 0이거나, T CopyConstructible 가 아닌 경우, 동작은 정의되지 않습니다.
3) 값 비교를 위해 operator < 를 사용합니다.
만약 T LessThanComparable 이 아니라면, 동작은 정의되지 않습니다.
4) 값을 비교하기 위해 비교 함수 comp 를 사용하십시오.

목차

매개변수

a, b - 비교할 값들
ilist - 비교할 값들을 가진 초기화자 리스트
cmp - 비교 함수 객체(즉, Compare 요구 사항을 만족하는 객체)로, true 를 반환하는 경우 a b 보다 작음 을 나타냅니다.

비교 함수의 시그니처는 다음과 동등해야 합니다:

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

시그니처에 const & 가 명시되지 않아도 되지만, 함수는 전달된 객체를 수정해서는 안 되며, 값 범주 에 관계없이 (가능한 const) Type1 Type2 타입의 모든 값을 받아들일 수 있어야 합니다 (따라서 Type1 & 는 허용되지 않으며, Type1 또한 Type1 에 대해 이동이 복사와 동등하지 않는 한 허용되지 않습니다 (C++11부터) ).
Type1 Type2 타입은 T 타입의 객체가 두 타입 모두로 암시적으로 변환될 수 있어야 합니다.

반환값

1,2) a b 중 더 작은 값을 반환합니다. 값이 동일한 경우 a 를 반환합니다.
3,4) ilist 에서 가장 작은 값. 여러 값이 가장 작은 값과 동등한 경우, 가장 왼쪽에 있는 값을 반환합니다.

복잡도

1) 정확히 한 번의 비교를 operator < 를 사용하여 수행합니다.
2) 비교 함수를 정확히 한 번 적용 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);
}
**참고:** 모든 C++ 코드는 원본 그대로 유지되었으며, HTML 태그와 속성도 수정되지 않았습니다.

참고 사항

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. 요구사항이 추가됨

참고 항목

주어진 값 중 더 큰 값을 반환합니다
(function template)
(C++11)
두 요소 중 더 작은 값과 더 큰 값을 반환합니다
(function template)
범위 내 가장 작은 요소를 반환합니다
(function template)
(C++17)
값을 한 쌍의 경계 값 사이로 고정합니다
(function template)
주어진 값 중 더 작은 값을 반환합니다
(algorithm function object)