-
April 7th, 2004, 01:10 AM
#1
URGENT: <algorithm> lower_bound() problem
I am currently working on a simulation program that uses discrete event simulation. Basically, the program generates events (in this case, the arrival and departure of customers in a store). So the events are placed in a priority queue, ordered by the times that the events are to occur (i.e. an event that is supposed to occur at time 25 would have a higher priority than an event that is supposed to occur at time 30). To make this ordering easy, I decided to use the vector STL to implement the queue.
To insert things in the order I want, I use the <algorithm> function, lower_bound(line.begin(), line.end(), e), where line is the vector, and Event* e is inserted in the appropriate location, based on ascending event time order. I have used lower_bound() before, and for it to work, I had to overload the == and < operators for the object (in this case, Event). However, when I run my program with the debugger, the overloaded == and < are never accessed and, consequently, the events are not ordered. Here are the relevant sections of code...
In Simulation class...
Code:
void Simulation::schedule(Event *e) {
//Create an iterator for the vector queue
vector<Event *>::iterator pos;
//Determine the position to insert the event
pos = lower_bound(eQueue.begin(), eQueue.end(), e);
//Insert the event at the determined position
eQueue.insert(pos, e);
}
In Event class...
Code:
bool Event:operator==(const Event& rhs) const {
//Return true if the event times are equal, false otherwise
return (time == rhs.time);
}
bool Event:operator<(const Event& rhs) const {
//Return true if this event time is less than rhs event time,
// false otherwise
return (time < rhs.time);
}
Do you have any idea why the lower_bound() function isn't looking for those two operators? Can you think of a better way to do this? Any help will be greatly appreciated. Thank you in advance!
Last edited by waverdr9; April 7th, 2004 at 01:12 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|