I've just written the following code, and was very surprised it doesn't compile:
std::deque<int> container;
// filling the container...
for (auto it = container.rbegin(); it != container.rend(); ++it)
if (*it == 5)
{
container.erase(it);
break;
}
As you can see, I want to delete the last element matching a certain criteria, if any.
The error is
no matching function for call to std::deque::erase(std::reverse_iterator...
At first I didn't believe it was caused by the reverse iterator, but that is indeed the case since replacing rbegin
/rend
with begin
/end
solves it.
So, 2 questions:
- Why isn't this supported? Is it merely one of those little things the C++ committee forgot to include in the standard, or is there a justification for this overload missing?
- What is the most elegant way to do what I want? Am I stuck with iterating by index?