how to pop the event which i want from STL list *URGENT*

Posted on 2006-05-06
Medium Priority
Last Modified: 2010-04-01
I have a question for the STLlist.

Event::Event(){ uid_=UNKNOWN; event_time_=BEYOND; eh_=NULL; }
list<Event> elist;

If I push 3 events into the list, ids are 1,2,3, the eventtime are 1s,2s,3s. then I use elist.sort().  and elist.pop_front(), the event which pops out is id=1. During handle this event, i want to change the event time for id=3 to 1.5s. after i changed the value, and finished handling the event id=1, i use sort() again, and i think the event sequence should be 1,3,2 according to the eventtime, 1s, 1.5s,2s. But in fact it is still 1,2,3. So how can I make the sequence like 1,3,2? I tried use remove() to remove the event id=3, and write a push a new event, but still can not work. is there anybody can give me a hand to tell me how to write that codes? Thank you very much!~~^_________________^
Question by:Viviana1215
  • 2
LVL 30

Expert Comment

ID: 16623413
Can you please post your current code?

Author Comment

ID: 16624207
HI Axter
This is the link to my code.

my problem is in the destination() function. If the node is the destination, then it will change it's eventtime, and then when the currenttime==to eventtime, this event should be pop out.

Author Comment

ID: 16624402
My project is to simulate the CSMA/CA protocol. Before node going to send the package, it should wait for a DIFS time, and then random generate a number of slot times, which is backoff. after the difs, when the difs is finished, it goes to backoff status, before it going to wait a slot time, it should check the status of the channel, if it is busy, it changes to the receiver immediately, and wait a sendtime which is the time for sending a package. when the node finish the backoff, it starts to send the package, now all the node in the channel should be changed into receiver mode, and after the sendtime, eachnode will check if itself is the destination, if not, it will throw away the package, else it will wait a sifs time and send back the ACK. my problem is in here, when all the node change to the receiver, i need to change the eventtime to the currenttime+sendtime, and the sequence in the list should be changed as the eventtime. now, the sequence still the same after i sort.

Sorry that I can not express clearly the aim for this project, and thank you for all the help and I really appreciate it .

Accepted Solution

B1-66ER earned 750 total points
ID: 16624406
Hi Viviana1215.

>>finished handling the event id=1, i use sort() again, and i think the event sequence should be 1,3,2 according to the eventtime, 1s, 1.5s,2s.

you need to define operator< for your Event class, and then use list::sort method to sort all it elements correctly.

struct Event
 int id ;
 double event_time ;
 Event(int id_, int time)
  : id(id_), event_time(time) {}

 //* this is it
 bool operator<(const Event &ev) const
  return event_time < ev.event_time ;

} ;


int main ()
 list<Event> l ;
 l.push_back(Event(1,1)) ;
 l.push_back(Event(2,2)) ;
 l.push_back(Event(3,3)) ;

 list<Event>::iterator itr = l.begin() ;
 for (itr = l.begin() ;
      itr != l.end() ; itr++)
  cout << itr->id << " " << itr->event_time << endl ;

 itr = l.begin() ;

 itr->event_time = 1.4 ; //* set different value
 l.sort() ;
 for (itr = l.begin() ;
      itr != l.end() ; itr++)
  cout << itr->id << " " << itr->event_time << endl ;

while(1) ;
return 0;

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question