Computer Science/컴퓨터프로그래밍
포인터형의 Vector 클래스에 Sort 적용하기
Theo Kim
2010. 12. 15. 22:30
#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[]) { vectorpInt; 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의 세번째 인자에 있었다.