Nick
Nick

Reputation: 308

What was the idiomatic way of reverse traversal of an iterable before C++11?

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?

Upvotes: 0

Views: 79

Answers (1)

john
john

Reputation: 88007

This works

for (auto i = str.end(); i != str.begin(); )
{
    --i;
    ...
}

Upvotes: 3

Related Questions