[STL] min_max

STL/정렬 관련 알고리즘 2010. 5. 17. 15:37 Posted by zetz

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
 
제너릭 min 알고리즘과 max 알고리즘은 두 개의 원소를 받아 각각 둘 중 더 작은 값과
더 큰 값을 리턴한다.
 
min_element 알고리즘과 max_element 알고리즘은 각각 입력 시퀀스에 담긴 원소들의
최소값과 최대 값을 가리키는 반복자를 리턴한다.
 
*/
 
 
#include <iostream>
#include <cassert>
#include <algorithm>
#include <vector>
 
using namespace std;
 
int main()
{
    cout<< "Illustrating the generic min_element and"
        << " max_element algorithms."<< endl;
 
    // 정수 벡터를 초기화
    vector<int> vector1(5);
    for(int i=0; i< 5; ++i)
        vector1[i] = i;
 
    random_shuffle(vector1.begin(), vector1.end());
 
    // vector1의 원소 중 최대 원소를 찾는다.
    vector<int>::iterator k =
        max_element(vector1.begin(), vector1.end());
    assert (*k == 4);
 
    // vector1의 원소 중 최소 원소를 찾는다.
    k = min_element(vector1.begin(), vector1.end());
    assert (*k == 0);
 
    cout<< " --- Ok."<< endl;
 
    return 0;
}

'STL > 정렬 관련 알고리즘' 카테고리의 다른 글

[STL] permutation  (0) 2010.05.17
[STL] nth_element  (0) 2010.05.17
[STL] merge  (0) 2010.05.17
[STL] lexicographical_compare  (0) 2010.05.17
[STL] heap  (0) 2010.05.17