Sometimes, we all need a way to iterate over a container in the opposite direction. There are several ways to reverse-iterate a container, and in this article, we’ll explore them.
Reverse Iterations
by Coral Kashri
From the article:
Probably the simplest way, taken from C is to iterate using an index location:
for
(
int64_t
index = ssize(container); index >= 0; --index) {
// do something with `container[index]`
}
This way is highly not recommended as it might lead to infinite loops if done incorrectly (for example by usinguint64_t
orsize_t
for the index type), and you can find more issues with this way in some previous articles about iterators in this blog.
Add a Comment
Comments are closed.