#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
: