8.3(금) C++ - queue에서의 단위전략

from Study/C++ 2007/08/07 21:26 view 17054

#include <queue>       // Q 또는우선순위Q를사용.

                       // 내부적으로 사용 할 container의 헤더도include 해야 한다.

#include <functional>

 

 

template<typename T, typename Cont = vector<T>, typename Pred = less<T> >

class priority_queue

{

        Pred f; // 조건자함수 객체생성

public:

        void foo()

        {

               // 크기비교가 필요하다면

               bool b = f(1, 2);

        }

};

 

// 절대값으로 크기를 비교하는 함수객체

template<typename T> struct abs_less

{

        bool operator()( T a, T b )

        {

               return abs(a) < abs(b);

        }

};

 

void main()

{

// greater<int> 는 템플릿 인자를 넘기기 때문에 타입을 넘긴다. , () 를 쓰지 않는다.!!!

// 결국 단위전략의 크기 비교객체를 다양하게 활용 할 수 있다.

        // 디폴트로 vector를 사용.
       
priority_queue<int, vector<int>, abs_less<int> > pq;
 

        pq.push(10);

        pq.push(20);

        pq.push(-50);

        pq.push(40);

        pq.push(30);

 

        cout << pq.top() << endl;
}

Tag |

Trackback Address :: 이 글에는 트랙백을 보낼 수 없습니다