포인터형의 Vector 클래스에 Sort 적용하기포인터형의 Vector 클래스에 Sort 적용하기

Posted at 2010. 12. 15. 22:30 | Posted in Computer Science/컴퓨터프로그래밍
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

bool compare(const int* lhs, const int* rhs)
{
	return *lhs < *rhs;
}

int main(int argc, char *argv[])
{
	vector pInt;

	unsigned int iCount = 0;


	int* a = new int(10);
	int* b = new int(5);
	int* c = new int(3);
	int* d = new int(8);
	int* e = new int(1);

	pInt.push_back( a );
	pInt.push_back( b );
	pInt.push_back( c );
	pInt.push_back( d );
	pInt.push_back( e );

	for(iCount = 0; iCount < pInt.size(); iCount++)
		cout << *pInt[iCount] << " ";

	cout << endl;

	sort( pInt.begin(), pInt.end(), compare );

	for(iCount = 0; iCount < pInt.size(); iCount++)
		cout << *pInt[iCount] << " ";

	return 0;
}

  프로젝트를 하다가 Sort는 Selection Sort를 구현한다고 STL을 사용하지 않았다. 근데 한가지 궁금증이 생겼는데 바로 포인트형을 담고 있는 Vector의 경우 어떻게 Sort를 구현할 것인가? 였다. 해답은 Sort의 세번째 인자에 있었다.

//

Class를 이용한 18자리의 Int 자료형 구현Class를 이용한 18자리의 Int 자료형 구현

Posted at 2010. 11. 12. 13:21

보호되어 있는 글입니다.
내용을 보시려면 비밀번호를 입력하세요.

UNLOCK!