M
Markus Svilans
Hi,
There seems to be some functionality missing from the STL.
I am iterating through a linked list (std::list) using a reverse
iterator and attempting to erase certain items from the list. It is
important that I iterate through the list backwards, because the items
in it have to be processed in reverse order before erasing. However,
there does not appear to be an std::list::erase() method defined for
reverse iterators.
I am using STLport 4.5 with Borland C++ Builder 6.
The following example code shows what I am trying to do:
// Arbitrary value to remove from the list
const int VALUE_TO_ERASE = 12;
std::list<int> data;
// [ code here to load data with values ]
std::list<int>::reverse_iterator ri = data.rbegin();
while (ri != data.rend())
{
// Get next iterator in case ri is erased.
std::list<int>::reverse_iterator next = ri;
++next;
// Check if ri needs to be erased
if (*ri == VALUE_TO_ERASE)
{
// [ code here to process *ri before erasing ]
data.erase(ri); // <-- Causes compiler error
}
ri = next;
}
Obviously an erase method is not defined for reverse iterators in
std::list.
Is there a nice solution to this problem? Do I need to get a more
recent version of STLport?
Thanks,
Markus.
There seems to be some functionality missing from the STL.
I am iterating through a linked list (std::list) using a reverse
iterator and attempting to erase certain items from the list. It is
important that I iterate through the list backwards, because the items
in it have to be processed in reverse order before erasing. However,
there does not appear to be an std::list::erase() method defined for
reverse iterators.
I am using STLport 4.5 with Borland C++ Builder 6.
The following example code shows what I am trying to do:
// Arbitrary value to remove from the list
const int VALUE_TO_ERASE = 12;
std::list<int> data;
// [ code here to load data with values ]
std::list<int>::reverse_iterator ri = data.rbegin();
while (ri != data.rend())
{
// Get next iterator in case ri is erased.
std::list<int>::reverse_iterator next = ri;
++next;
// Check if ri needs to be erased
if (*ri == VALUE_TO_ERASE)
{
// [ code here to process *ri before erasing ]
data.erase(ri); // <-- Causes compiler error
}
ri = next;
}
Obviously an erase method is not defined for reverse iterators in
std::list.
Is there a nice solution to this problem? Do I need to get a more
recent version of STLport?
Thanks,
Markus.