Home > other >  What was the idiomatic way of reverse traversal of an iterable before C 11?
What was the idiomatic way of reverse traversal of an iterable before C 11?

Time:08-09

void rev(string& str)
{
    for (auto i = str.end() -1; i != str.begin() -1; i--)
        cout << *i;

    cout << '\n';
}

The code above works on my system however str.begin() -1 invokes undefined behaviour as per the standard. So what is the idiomatic way of reverse traversal using iterator's but not reverse_iterator's?

CodePudding user response:

This works

for (auto i = str.end(); i != str.begin(); )
{
    --i;
    ...
}
  •  Tags:  
  • c
  • Related