γλͺ©μ°¨γ
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