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

N

noone

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.
 
V

Victor Bazarov

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
 
N

noone

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
 
M

Marcus Kwok

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top