map::erase(reverse_iterator) is not allowed???

Discussion in 'C++' started by noone@all.com, Mar 14, 2007.

  1. Guest

    string operator()(const bool clean=true) {
    string rv;
    MPEGQUEUE::reverse_iterator i=thequeue.rbegin();
    MPEGQUEUE::reverse_iterator t=thequeue.rend();
    while (i!=thequeue.rend()) {

    if (i->second->isComplete()) {
    t=i;
    i--;
    if (clean)
    for (MPEGQUEUE::reverse_iterator j=i;
    j!=thequeue.rend(); ) {
    delete j->second;
    thequeue.erase(j++);
    }
    } else {
    i--;
    }
    }
    return rv;
    }



    thequeue.erase(j++) says "no matching function call". Is this because the
    iterator is a reverse_iterator? Am I not allowed to delete map entries
    based on a reverse_iterator? That would be an ugly deficiency in the
    STL since I need to delete all keyed entries (less than) the last complete
    entry in the queue.

    The application essentially reassembles network fragmented MPEG4 packets
    that may arrive out of order, at indeterminant times, or not at all. the
    packets are timestamped, and only the most recent (complete) frame is
    valid. Everything in the queue earlier than it should be deleted upon
    complete reassembly of a newer frame. I also still have to loop through
    the container to delete remove frames and frame segments that have
    outlived their time to live value. Again, a reverse iterator would be
    preferable.
     
    , Mar 14, 2007
    #1
    1. Advertising

  2. wrote:
    > string operator()(const bool clean=true) {
    > string rv;
    > MPEGQUEUE::reverse_iterator i=thequeue.rbegin();
    > MPEGQUEUE::reverse_iterator t=thequeue.rend();
    > while (i!=thequeue.rend()) {
    >
    > if (i->second->isComplete()) {
    > t=i;
    > i--;
    > if (clean)
    > for (MPEGQUEUE::reverse_iterator j=i;
    > j!=thequeue.rend(); ) {
    > delete j->second;
    > thequeue.erase(j++);
    > }
    > } else {
    > i--;
    > }
    > }
    > return rv;


    My OE screws up formatting when tab chars are involved, sorry about
    that.

    > }
    >
    >
    >
    > thequeue.erase(j++) says "no matching function call". Is this
    > because the iterator is a reverse_iterator? Am I not allowed to
    > delete map entries based on a reverse_iterator? That would be an
    > ugly deficiency in the
    > STL since I need to delete all keyed entries (less than) the last
    > complete entry in the queue.


    You're using "map" and "queue" interchangeably here. Are you aware
    that 'queue' is a container adapter? Is your "queue" in fact a map?

    There is no requirement that _any_ standard container supported
    erasure from itself using a reverse_iterator. You can always get
    the real iterator from the reverse iterator and pass that:

    thequeue.erase((j++).base());

    > The application essentially reassembles network fragmented MPEG4
    > packets that may arrive out of order, at indeterminant times, or not
    > at all. the packets are timestamped, and only the most recent
    > (complete) frame is valid. Everything in the queue earlier than it
    > should be deleted upon complete reassembly of a newer frame. I also
    > still have to loop through the container to delete remove frames and
    > frame segments that have outlived their time to live value. Again, a
    > reverse iterator would be preferable.


    For some reason methinks that any algorithm you can write in terms of
    reverse iterators should be possible to rewrite in terms of normal
    iterators. Although I could be wrong...

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Mar 14, 2007
    #2
    1. Advertising

  3. Guest


    > You're using "map" and "queue" interchangeably here. Are you aware
    > that 'queue' is a container adapter? Is your "queue" in fact a map?


    Correct...the internals are a map based on my need for sort order
    controlled extraction and deletion, but random insertion, and
    quick searches...perhaps queue isn't the best term for the structure.

    >
    > There is no requirement that _any_ standard container supported
    > erasure from itself using a reverse_iterator. You can always get
    > the real iterator from the reverse iterator and pass that:
    >
    > thequeue.erase((j++).base());
    >


    Yup...I did something like

    i--; // reverse iterator;
    q.erase(q.begin(), i.base());

    and at least it compiles now. I think I need to insert some range
    checking though to cover the .end() method possibility when dealing with
    iterator ranges

    > For some reason methinks that any algorithm you can write in terms of
    > reverse iterators should be possible to rewrite in terms of normal
    > iterators. Although I could be wrong...


    you're probably right but the reverse_iterator more closely follows my
    thinking since I'm more interested in reverse sort order. I suppose I
    could have redefined the sort order for the container and used all forward
    iterators but I didn't think of that until just now.

    -Prowel
     
    , Mar 14, 2007
    #3
  4. Marcus Kwok Guest

    wrote:
    > string operator()(const bool clean=true) {
    > string rv;
    > MPEGQUEUE::reverse_iterator i=thequeue.rbegin();
    > MPEGQUEUE::reverse_iterator t=thequeue.rend();
    > while (i!=thequeue.rend()) {
    >
    > if (i->second->isComplete()) {
    > t=i;
    > i--;
    > if (clean)
    > for (MPEGQUEUE::reverse_iterator j=i;
    > j!=thequeue.rend(); ) {
    > delete j->second;
    > thequeue.erase(j++);
    > }
    > } else {
    > i--;
    > }
    > }
    > return rv;
    > }
    >
    >
    >
    > thequeue.erase(j++) says "no matching function call". Is this because the
    > iterator is a reverse_iterator? Am I not allowed to delete map entries
    > based on a reverse_iterator?


    That is correct; you need a regular iterator for erase().

    This article (specifically Guideline 3) helps explain the relationship
    between iterators and reverse_iterators and when you can just use the
    base() function and when you must adjust it to get what you want:

    http://www.ddj.com/dept/cpp/184401406

    --
    Marcus Kwok
    Replace 'invalid' with 'net' to reply
     
    Marcus Kwok, Mar 14, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. praetorian
    Replies:
    0
    Views:
    531
    praetorian
    Nov 18, 2003
  2. Buster
    Replies:
    0
    Views:
    2,907
    Buster
    Apr 1, 2004
  3. erase vs. erase

    , Mar 25, 2006, in forum: C++
    Replies:
    7
    Views:
    381
    Pete Becker
    Mar 30, 2006
  4. Dalbosco J-F
    Replies:
    3
    Views:
    900
    Marcus Kwok
    Aug 3, 2006
  5. Iñaki Baz Castillo
    Replies:
    13
    Views:
    535
    Iñaki Baz Castillo
    May 1, 2011
Loading...

Share This Page