우선 순위 대기열에 넣고 이벤트 시간별로 정렬해야하는 여러 유형의 이벤트가 여러 개 있습니다.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의 순서로 배치되지 않습니다.
무엇을 정렬하지 않았다는 뜻입니까? 어떻게 알았어? –
큐의 헤드에서 터지는 것을 시작할 때 값이 가장 낮은 값에서 가장 높은 값으로 나오지 않습니다. – rcell
당신은'const' 참조에 의한 인수를 취해야합니다 :'bool operator() (const Event & a, const Event & b);' –