π•ƒπ•¦π•„π•šπ•£

γ€ŒβŒ¨οΈα΄„s」STUDY/C++

[C++][header][algorithm] min max

by λ£¨λ°€π•ƒπ•¦π•„π•šπ•£2023. 2. 22.
728x90
λ°˜μ‘ν˜•
γ€Žλͺ©μ°¨γ€
0. μ„€λͺ…에 μ•žμ„œ
1. Default Constructor
2. Custom Constructor 
3. Initializer List Constructor

0. std::min & std::max γ€Žμ„€λͺ…에 μ•žμ„œγ€

μ°Έκ³ 

1. β‘ λΉ„ꡐ할 값듀이 λ§Žκ±°λ‚˜β‘‘ArrayㆍVector와 같은 일련의 μ»¨ν…Œμ΄λ„ˆμ— μ €μž₯λ˜μ–΄ μžˆλ‹€λ©΄, μ΅œμ†Œκ°’γ†μ΅œλŒ€κ°’μ„ κ΅¬ν•˜κΈ° μœ„ν•΄ min_element λ˜λŠ” max_element ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•  수 μžˆλ‹€. (ν•΄λ‹Ή ν•¨μˆ˜μ— λŒ€ν•΄μ„œλŠ” λ‚˜μ€‘μ— ν¬μŠ€νŒ… ν•˜κ² λ‹€.)

 

2. std::min와 std::maxλŠ” algorithm λΌμ΄λΈŒλŸ¬λ¦¬μ— 3가지 ν˜•νƒœλ‘œ μ‘΄μž¬ν•œλ‹€. γ€Žβ‘  Default Constructor 』 γ€Žβ‘‘ Custom Constructor 』 γ€Žβ‘’ Initializer List Constructor 』 κ°€ 이에 ν•΄λ‹Ήν•œλ‹€.

λ°˜μ‘ν˜•

1. std::min & std::max γ€Žβ‘  Default Constructor 』

ν•¨μˆ˜ μ›ν˜•
/* -- Default Constructor -- */
#include <algorithm> // min max ν•¨μˆ˜λŠ” algorithm λΌμ΄λΈŒλŸ¬λ¦¬μ— κ΅¬ν˜„λ˜μ–΄ μžˆλ‹€.

/* min */
template <class T>
const T& min (const T& a, const T& b);

/* max */
template <class T>
const T& max (const T& a, const T& b);
λ°˜ν™˜ κ°’
/* min */
std::min(a, b);

/* max */
std::max(a, b);

min : aκ°’κ³Ό b값을 λΉ„κ΅ν•˜μ—¬, μž‘μ€κ°’μ„ λ°˜ν™˜ν•˜λŠ” ν•¨μˆ˜.

max : aκ°’κ³Ό b값을 λΉ„κ΅ν•˜μ—¬, ν°κ°’을 λ°˜ν™˜ν•˜λŠ” ν•¨μˆ˜.

3개 μ΄μƒμ˜ κ°’ 비ꡐ 방법
/* min */
std::min(a, b);			// a, b 쀑 μ΅œμ†Œκ°’ λ°˜ν™˜.
std::min({a, b, c});		// a, b, c 쀑 μ΅œμ†Œκ°’ λ°˜ν™˜.
std::min({a, b, c, d});		// a, b, c, d 쀑 μ΅œμ†Œκ°’ λ°˜ν™˜.
std::min({a, b, c, d, e}); 	// a, b, c, d, e 쀑 μ΅œμ†Œκ°’ λ°˜ν™˜.
...

/* max */
std::max(a, b); 		// a, b 쀑 μ΅œλŒ€κ°’ λ°˜ν™˜.
std::max({a, b, c}); 		// a, b, c 쀑 μ΅œλŒ€κ°’ λ°˜ν™˜.
std::max({a, b, c, d}); 	// a, b, c, d 쀑 μ΅œλŒ€κ°’ λ°˜ν™˜.
std::max({a, b, c, d, e}); 	// a, b, c, d, e 쀑 μ΅œλŒ€κ°’ λ°˜ν™˜.
...

{ }λ₯Ό μ΄μš©ν•˜μ—¬, 3개 μ΄μƒμ˜ 값듀에 λŒ€ν•˜μ—¬ μ΅œμ†Œκ°’γ†μ΅œλŒ€κ°’μ„ ꡬ할 수 μžˆλ‹€. (μ΄λŠ” 사싀, γ€Žβ‘’ Initializer List Constructor 』λ₯Ό μ΄μš©ν•œ 방법이닀.)

객체(클래슀)λ₯Ό 인자둜 μ „λ‹¬ν•˜λŠ” 방법 : operator< 이용
/* Input */
#include<iostream>
#include<algorithm>
using namespace std;

class Animal {
private:
    int age;    //λ‚˜μ΄
    int legs;   //닀리 개수

public:
    Animal(int age, int legs) : age(age), legs(legs) {};

    //λ‚˜μ΄κ°€ 적은지 ν™•μΈν•˜κ³ , λ‚˜μ΄κ°€ κ°™λ‹€λ©΄ λ‹€λ¦¬κ°œμˆ˜κ°€ 적은지 νŒλ‹¨.
    bool operator<(const Animal& animal) const
    {
        if (age != animal.age)
            return age < animal.age;
        else
            return legs < animal.legs;
    }

    //cout 좜λ ₯을 μœ„ν•œ "<<" μž¬μ •μ˜.
    friend ostream& operator<<(ostream& os, const Animal& animal);
};

ostream& operator<<(ostream& os, const Animal& animal) {
    os << animal.age << '/' << animal.legs;
    return os;
}

int main(void) {
    cout << "고양이와 닭을 비ꡐ" << endl << endl;
    Animal cat1(10, 4);
    Animal cat2(20, 4);
    Animal chicken1(15, 2);
    Animal chicken2(20, 2);

    cout << "min(cat1(10,4),     cat2(20,4)) : " << min(cat1,     cat2) << endl;
    cout << "min(cat1(10,4), chicken1(15,2)) : " << min(cat1, chicken1) << endl;
    cout << "min(chicken2(20,2), cat1(10,4)) : " << min(chicken2, cat1) << endl;
    cout << "min(cat2(20,4), chicken2(20,2)) : " << min(cat2, chicken2) << endl << endl;

    cout << "max(cat1(10,4),     cat2(20,4)) : " << max(cat1,     cat2) << endl;
    cout << "max(cat1(10,4), chicken1(15,2)) : " << max(cat1, chicken1) << endl;
    cout << "max(chicken2(20,2), cat1(10,4)) : " << max(chicken2, cat1) << endl;
    cout << "max(cat2(20,4), chicken2(20,2)) : " << max(cat2, chicken2) << endl;

    return 0;
}
/* Output */
고양이와 닭을 비ꡐ

min(cat1(10,4),     cat2(20,4)) : 10/4
min(cat1(10,4), chicken1(15,2)) : 10/4
min(chicken2(20,2), cat1(10,4)) : 10/4
min(cat2(20,4), chicken2(20,2)) : 20/2

max(cat1(10,4),     cat2(20,4)) : 20/4
max(cat1(10,4), chicken1(15,2)) : 15/2
max(chicken2(20,2), cat1(10,4)) : 20/2
max(cat2(20,4), chicken2(20,2)) : 20/4

λŒ€μ†ŒλΉ„κ΅λ₯Ό μœ„ν•œ operator< κ°€ ν΄λž˜μŠ€μ— μ •μ˜λ˜μ–΄ μžˆλ‹€λ©΄,  min max에 객체(클래슀)λ₯Ό 집어넣어 μ‚¬μš©ν•  수 μžˆλ‹€.

예제

γ€Žμ˜ˆμ œ1』

/* Input */
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
	cout << "max(1, 2) == " << max(1, 2) << endl;
	cout << "min('a', 'z') == " << min('a', 'z') << endl;
	cout << "max(3.14, 2.73 == " << max(3.14, 2.73) << endl;

	return 0;
}
/* Output */
max(1, 2) == 2
min('a', 'z') == a
max(3.14, 2.73 == 3.14

γ€Žμ˜ˆμ œ2』

/* Input */
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
	int a = 10, b = 30, c = 50, d = 20, e = 444;

	cout << a << ", " << e << "쀑 " << max(a, e) << "κ°€ 더 크닀.\n";
	cout << b << ", " << c << ", " << d << "쀑 " << min({ b, c, d }) << "이 제일 μž‘λ‹€.\n";

	pair<int, int> mm = minmax({ a, b, c, d, e });

	cout << a << ", " << b << ", " << c << ", " << d << ", " << e;
	cout << "쀑 κ°€μž₯ μž‘μ€ κ°’ = " << mm.first << "이며, κ°€μž₯ 큰 κ°’ = " << mm.second << " 이닀.\n";

	return 0;
}
/* Output */
10, 444쀑 444κ°€ 더 크닀.
30, 50, 20쀑 20이 제일 μž‘λ‹€.
10, 30, 50, 20, 444쀑 κ°€μž₯ μž‘μ€ κ°’ = 10이며, κ°€μž₯ 큰 κ°’ = 444 이닀.
λ°˜μ‘ν˜•

2. std::min & std::max γ€Žβ‘‘ Custom Constructor 』

ν•¨μˆ˜ μ›ν˜•
/* -- Custom Constructor -- */
#include <algorithm> // min max ν•¨μˆ˜λŠ” algorithm λΌμ΄λΈŒλŸ¬λ¦¬μ— κ΅¬ν˜„λ˜μ–΄ μžˆλ‹€.

/* min */
template <class T, class Compare>
const T& min (const T& a, const T& b, Compare comp);

/* max */
template <class T, class Compare>
const T& max (const T& a, const T& b, Compare comp);
λ°˜ν™˜ κ°’

β‘  Compare Class(Functor)λ₯Ό μ΄μš©ν•˜λŠ” 방법.

'Compare comp' μΈμžμ— ν•΄λ‹Ήν•˜λŠ” Compare Class(Functor)λ₯Ό λ§Œλ“€μ–΄, μžμ‹ μ΄ μ›ν•˜λŠ” λ°©μ‹μœΌλ‘œ λŒ€γ†μ†Œ 비ꡐλ₯Ό μ§„ν–‰ν•œ ν›„ 값을 λ°˜ν™˜λ°›μ„ 수 μžˆλ‹€.

 

β‘‘ Compare Function을 μ΄μš©ν•˜λŠ” 방법.

'Compare comp' μΈμžμ— ν•΄λ‹Ήν•˜λŠ” Compare Function을 λ§Œλ“€μ–΄, μžμ‹ μ΄ μ›ν•˜λŠ” λ°©μ‹μœΌλ‘œ λŒ€γ†μ†Œ 비ꡐλ₯Ό μ§„ν–‰ν•œ ν›„ 값을 λ°˜ν™˜ 받을 수 μžˆλ‹€.

예제

γ€Žμ˜ˆμ œ1』 : β‘  Compare Class(Functor)λ₯Ό μ΄μš©ν•˜λŠ” 방법.

/* β‘  Compare Class(Functor)λ₯Ό μ΄μš©ν•˜λŠ” 방법. */
/* Input */
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

class Student {
public:
	string name;
	int num;
	
public:
	Student(string name, int num) : name(name), num(num) {}
};

class compare_class_sl { // sl = small large
public:
	bool operator() (const Student& a, const Student& b) {
		return a.num < b.num;
	}
};

class compare_class_ls { // ls = large small
public:
	bool operator() (const Student& a, const Student& b) {
		return a.num > b.num;
	}
};

int main(void) {
	Student s1("Hong", 1);
	Student s2("Park", 2);

	/* μž„μ‹œκ°μ²΄ 생성을 ν†΅ν•œ 방식 : compare_class_sl() 및 compare_class_ls()λ₯Ό μž„μ‹œκ°μ²΄λ‘œ λ„˜κ²¨μ€€λ‹€. */
	cout << max(s1, s2, compare_class_sl()).name << ": " << max(s1, s2, compare_class_sl()).num << endl;
	cout << max(s1, s2, compare_class_ls()).name << ": " << max(s1, s2, compare_class_ls()).num << endl;
	cout << min(s1, s2, compare_class_sl()).name << ": " << min(s1, s2, compare_class_sl()).num << endl;
	cout << min(s1, s2, compare_class_ls()).name << ": " << min(s1, s2, compare_class_ls()).num << endl;
	cout << endl;

	/* 객체 생성을 ν†΅ν•œ 방식 */
	compare_class_sl _compare_class_sl;
	compare_class_ls _compare_class_ls;
	cout << max(s1, s2, _compare_class_sl).name << ": " << max(s1, s2, _compare_class_sl).num << endl;
	cout << max(s1, s2, _compare_class_ls).name << ": " << max(s1, s2, _compare_class_ls).num << endl;
	cout << min(s1, s2, _compare_class_sl).name << ": " << min(s1, s2, _compare_class_sl).num << endl;
	cout << min(s1, s2, _compare_class_ls).name << ": " << min(s1, s2, _compare_class_ls).num << endl;

	return 0;
}
/* Output */
Park: 2
Hong: 1
Hong: 1
Park: 2

Park: 2
Hong: 1
Hong: 1
Park: 2

γ€Žμ˜ˆμ œ2』 : β‘‘ Compare Function을 μ΄μš©ν•˜λŠ” 방법.

/* β‘‘ Compare Function을 μ΄μš©ν•˜λŠ” 방법. */
/* Input */
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

class Student {
public:
	string name;
	int num;

public:
	Student(string name, int num) : name(name), num(num) {}
};

bool compare_function_sl (const Student& a, const Student& b) { // sl = small large
	return a.num < b.num;
}

bool compare_function_ls(const Student& a, const Student& b) { // ls = large small
	return a.num > b.num;
}

int main(void) {
	Student s1("Hong", 1);
	Student s2("Park", 2);

	cout << max(s1, s2, compare_function_sl).name << ": " << max(s1, s2, compare_function_sl).num << endl;
	cout << max(s1, s2, compare_function_ls).name << ": " << max(s1, s2, compare_function_ls).num << endl;
	cout << min(s1, s2, compare_function_sl).name << ": " << min(s1, s2, compare_function_sl).num << endl;
	cout << min(s1, s2, compare_function_ls).name << ": " << min(s1, s2, compare_function_ls).num << endl;

	return 0;
}
/* Output */
Park: 2
Hong: 1
Hong: 1
Park: 2
λ°˜μ‘ν˜•

3. std::min & std::max γ€Žβ‘’ Initializer List Constructor γ€

ν•¨μˆ˜ μ›ν˜•
/* -- Initializer List Constructor -- */
#include <algorithm> // min max ν•¨μˆ˜λŠ” algorithm λΌμ΄λΈŒλŸ¬λ¦¬μ— κ΅¬ν˜„λ˜μ–΄ μžˆλ‹€.

/* min */
template <class T> T max (initializer_list<T> il);
template <class T, class Compare>
T min (initializer_list<T> il, Compare comp); // Compare compλŠ” μƒλž΅ κ°€λŠ₯.

/* max */
template <class T> T max (initializer_list<T> il);
template <class T, class Compare>
T max (initializer_list<T> il, Compare comp); // Compare compλŠ” μƒλž΅ κ°€λŠ₯.
λ°˜ν™˜ κ°’

initializer_list<T> il 에 ν•΄λ‹Ήν•˜λŠ” 인자둜 { }λ₯Ό 이용, μ—¬λŸ¬ 값듀을 λ¬Άμ–΄ μ „λ‹¬ν•˜λ©΄, ν•΄λ‹Ή 값듀을 λΉ„κ΅ν•˜μ—¬ max ν˜Ήμ€ min 값을 λ°˜ν™˜λ°›λŠ”λ‹€. (Compare comp에 ν•΄λ‹Ήν•˜λŠ” μΈμžλŠ” μƒλž΅ κ°€λŠ₯ν•˜λ‹€.)

예제
/* Input */
#include <iostream>
#include <algorithm>
using namespace std;

int main(void) {
	cout << min({ 2, 4, 6, 8, 10 }) << endl;
	cout << max({ 2, 4, 6, 8, 10 }) << endl;

	return 0;
}
/* Output */
2
10

 

728x90
λ°˜μ‘ν˜•