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

Posted at 2010. 12. 15. 22:30 | Posted in Computer Science/컴퓨터프로그래밍
01.#include <iostream>
02.#include <algorithm>
03.#include <vector>
04.using namespace std;
05. 
06.bool compare(const int* lhs, const int* rhs)
07.{
08.    return *lhs < *rhs;
09.}
10. 
11.int main(int argc, char *argv[])
12.{
13.    vector<int*> pInt;
14. 
15.    unsigned int iCount = 0;
16. 
17. 
18.    int* a = new int(10);
19.    int* b = new int(5);
20.    int* c = new int(3);
21.    int* d = new int(8);
22.    int* e = new int(1);
23. 
24.    pInt.push_back( a );
25.    pInt.push_back( b );
26.    pInt.push_back( c );
27.    pInt.push_back( d );
28.    pInt.push_back( e );
29. 
30.    for(iCount = 0; iCount < pInt.size(); iCount++)
31.        cout << *pInt[iCount] << " ";
32. 
33.    cout << endl;
34. 
35.    sort( pInt.begin(), pInt.end(), compare );
36. 
37.    for(iCount = 0; iCount < pInt.size(); iCount++)
38.        cout << *pInt[iCount] << " ";
39. 
40.    return 0;
41.}
42.</int*>

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

//

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

Posted at 2010. 11. 12. 13:21

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

UNLOCK!