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

Posted on 2006-05-06
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
    LVL 30

    Expert Comment

    Can you please post your current code?

    Author Comment

    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

    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 .
    LVL 5

    Accepted Solution

    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

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
    This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
    The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
    The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

    728 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now