2013-10-04 5 views
1

우선 순위 대기열에 넣고 이벤트 시간별로 정렬해야하는 여러 유형의 이벤트가 여러 개 있습니다.priority_queue operator <implementation trouble

struct Event { 
    double event_time; 
    int type; 
}; 

나는 클래스 EventCompare과 같이 사용

class EventCompare { 
public: 
    bool operator()(Event &a, Event &b) { 
     return a.event_time > b.event_time; 
    } 
}; 

과 우선 순위 큐 초기화 :

priority_queue<Event, vector<Event>, EventCompare> event_scheduler; 

내가 우선 순위 큐에 이벤트를 밀어

, 그들은 아직 분류되지 않습니다. 구현에 문제가 있습니까?

나는 그런 식으로 내 이벤트를 생성합니다

srand((unsigned int)time(NULL)); 
while(action_time < 100) { 
    u = (double)rand()/(double)RAND_MAX; 
    action_time += -log(u)/25; 
    Event e = {action_time, 0}; 
    event_scheduler.push(e); 
} 

난 후, 다른 유사한 루프하지만 랜드 시드를 재설정을 0으로 다시 action_time 설정 및 유형 1과 이벤트에 대한 입력과 이벤트 1은 event_time의 순서로 배치되지 않습니다.

+1

무엇을 정렬하지 않았다는 뜻입니까? 어떻게 알았어? –

+0

큐의 헤드에서 터지는 것을 시작할 때 값이 가장 낮은 값에서 가장 높은 값으로 나오지 않습니다. – rcell

+0

당신은'const' 참조에 의한 인수를 취해야합니다 :'bool operator() (const Event & a, const Event & b);' –

답변

1

가장 오래된 이벤트 (event_time 최저)를 대기열의 맨 위에 놓으려는 경우 사용자 지정 비교를 되돌려 야합니다. 기본적으로 std :: priority_queue는 가장 위를 가장 위에 둡니다.

class EventCompare { 
public: 
    bool operator()(Event &a, Event &b) { 
     return a.event_time > b.event_time; 
    } 
}; 

이 방법은 정상적으로 작동합니다. 예 : coliru

+0

비교를 되돌리고 내 이벤트를 생성하는 방법에 대한 내 질문에 추가했습니다. – rcell

+0

이벤트 생성 방법을 사용하여 수정 된 coliru를 참조하십시오. http : //coliru.stacked-crooked.com/a/d80a0fae07062703 – goji