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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include
#include
 
#include
#include
 
using namespace std;
 
class POINT
{
public:
    int x;
    int y;
};
 
bool PointComp(const POINT* pa, const POINT* pb)
{
    return ((pa->x) < (pb->x));
}
 
void main()
{
    register int i;
 
    vector points;
    vector::iterator iter;
 
    //------------------------------------------------------------------------
    // 데이터 입력
    //------------------------------------------------------------------------
 
    // 랜덤 발생 초기화
    srand((unsigned int)time(0));
 
    for (i = 0; i < 5; i++)
    {
        POINT* pt = new POINT;
 
        pt->x = rand() % 100;
        pt->y = rand() % 100;
 
        points.push_back(pt);
    }
 
    //------------------------------------------------------------------------
    // 데이터 참조 (이터레이터 사용)
    //------------------------------------------------------------------------
 
    printf("Original data (Randomly generated):\n");
    for (i = 0, iter = points.begin(); iter != points.end(); ++iter, ++i)
    {
        printf("x[%d] = %2d, y[%d] = %2d\n", i, (*iter)->x, i, (*iter)->y);
    }
 
    //------------------------------------------------------------------------
    // 데이터 정렬 (배열처럼 중괄호 연산자 사용)
    //------------------------------------------------------------------------
 
    sort(points.begin(), points.end(), PointComp);
 
    printf("\nSorting:\n");
    for (i = 0; i < points.size(); i++)
    {
        printf("x[%d] = %2d, y[%d] = %2d\n", i, points[i]->x, i, points[i]->y);
    }
 
    //------------------------------------------------------------------------
    // 데이터 삭제
    //------------------------------------------------------------------------
 
    POINT* p;
 
    for (iter = points.begin(); iter != points.end(); ++iter)
    {
        p = *iter;
        if (p != NULL)
        {
            delete p;
            p = NULL;
        }
    }
 
    points.clear();
 
    printf("\nRemoving all the data:\n");
    printf("points.size() = %d\n", points.size());
}


아래는 실제 실행 결과의 예.

Original data (Randomly generated):

x[0] = 14, y[0] = 54
x[1] = 93, y[1] = 57
x[2] = 61, y[2] = 18
x[3] = 94, y[3] = 46
x[4] = 78, y[4] = 6

Sorting:

x[0] = 14, y[0] = 54
x[1] = 61, y[1] = 18
x[2] = 78, y[2] = 6
x[3] = 93, y[3] = 57
x[4] = 94, y[4] = 46

Removing all the data:

points.size() = 0


Posted by kkokkal
: