std::min, std::max, std::minmax

2022. 6. 3. 13:54C++/C++ 정리

<algorithm> 헤더를 include 한다. 

std::min, std::max, std::minmax 함수는 값 또는 이니셜라이저 리스트를 입력받아 결과를 리턴한다. 

std::minmax는 결과를 std::pair로 리턴한다. pair의 첫 번째 원소는 최솟값이고 두 번째 원소는 최댓값이다. > (m,M)

 

std::min, std::max, std::minmax 함수를 간략히 정리하면 다음 표와 같다. 

함수 설명
min(a,b) a와 b 중에서 최솟값을 리턴한다.
min(a,b,comp) 프레디케이트 comp를 이용해 a와 b 중에서 최솟값을 찾아 리턴한다. 
min(이니셜라이저_리스트) 이니셜라이저 리스트에 있는 값 중에서 최솟값을 리턴한다. 
min(이니셜라이저_리스트, comp) 프레디케이트 comp를 이용해 이니셜 라이저 리스트 값 중에서 최솟값을 구한다.
max(a,b) a와 b 중에서 최댓값을 리턴한다.
max(a,b,comp) 프레디케이트 comp를 이용해 a와 b 중에서 최댓값을 찾아 리턴한다. 
max(이니셜라이저_리스트) 이니셜라이저 리스트에 있는 값 중에서 최댓값을 리턴한다. 
max(이니셜라이저_리스트, comp) 프레디케이트 comp를 이용해 이니셜 라이저 리스트 값 중에서 최댓값을 구한다.
minmax(a,b) a와 b중에서 최솟값과 최댓값을 리턴한다. 
minmax(a,b,comp) 프레디케이트 comp를 이용해 a와 b 중에서 최솟값과 최댓값을 리턴한다.
minmax(이니셜라이저_리스트) 이니셜라이저 리스트에 있는 값 중에서 최솟값과 최댓값을 리턴한다. 
minmax(이니셜라이저_리스트, comp) 프레디케이트 comp를 이용해 이니셜라이저 리스트의 값 중에서 최솟값과 최댓값을 리턴한다. 

>  true나 false를 리턴하는 함수를 프레디케이트(predicate)라고 부른다

 

#include <algorithm>
#include <iostream>
int main(void)
{
	std::string first{ "first" };
	std::string second{ "second" };

	auto minInt = std::min({3,1,2011,2014,-5});	
	// std::min(1,2,3,4)에러 발생 -> (1,2,3,4) 이니셜라이저리스트 아님

	std::cout << "std::min(2011, 2014): " << std::min(2011, 2014) << std::endl;//2011
	std::cout << "std::min(first, second): " << std::min(first, second) << std::endl;//first
	std::cout << "std::min({3,1,2011,2014,-5}): " << std::min({ 3,1,2011,2014,-5 }) << std::endl;//-5
	std::cout << "std::min(-10,-5, [](int a, int b){return std::abs(a) < std::abs(b);}): " << std::min(-10, -5, [](int a, int b) {return std::abs(a) < std::abs(b); }) << std::endl;//
	// -5
	// lambda! // abs() -> 절대값

	std::cout << std::endl;

	std::cout << "std::max(2011, 2014): " << std::max(2011, 2014) << std::endl; // 2014
	std::cout << "std::max(first, second): " << std::max(first, second) << std::endl; // second
	std::cout << "std::max({3,1,2011,2014,-5}): " << std::max({ 3,1,2011,2014,-5 }) << std::endl;// 2014
	std::cout << "std::max(-10,-5, [](int a, int b){return std::abs(a) < std::abs(b);}): " << std::max(-10, -5, [](int a, int b) {return std::abs(a) < std::abs(b); }) << std::endl;
	// -5

	std::cout << std::endl;

	auto pairInt = std::minmax(2011, 2014);	
	auto pairStr = std::minmax(first, second);
	auto pairSeq = std::minmax({ 3,1,2011,2014,-5 });
	auto pairAbs = std::minmax({ -10, -5, 2011, 2014, 3 }, [](int a, int b) {return std::abs(a) < std::abs(b); });

	std::cout << "(pairInt.first, pairInt.second): (" << pairInt.first << ", " << pairInt.second << ")" << std::endl; // (2011,2014)
	std::cout << "(pairStr.first, pairStr.second): (" << pairStr.first << ", " << pairStr.second << ")" << std::endl; // (first, second)
	std::cout << "(pairSeq.first, pairSeq.second): (" << pairSeq.first << ", " << pairSeq.second << ")" << std::endl; // (-5, 2014)
	std::cout << "(pairAbs.first, pairAbs.second): (" << pairAbs.first << ", " << pairAbs.second << ")" << std::endl; // (3, 2014)

	return 0;
}