views:

526

answers:

2

I'm having trouble iterating in reverse over a map in gcc c++. When I use a reverse iterator, it seems I can't assign anything to it - the compiler complains. I'm working around it with some awkward code using a forward iterator, but it's not very elegant. Any thoughts?

+3  A: 

You mean something like:

std::map<std::string, std::string> theMap;
/* do stuff */

for (std::map<std::string, std::string>::reverse_iterator iter = theMap.rbegin(); iter != theMap.rend(); ++iter)
{
    /* do more stuff */
}

Does not work? When you say "the compiler complains", we can only guess so much. How does it complain? Error? Warning?

GMan
typename is not required there
anon
Oops, I've been writing a lot of templated stuff lately, I removed it, thanks. :X
GMan
-1 because?... :(
GMan
+3  A: 

You should check, how you obtain iterator range ( it should be rbegin()/rend() instead begin()/end() ). Do you really use reverse_iterator ( not const_reverse_iterator )?

bb