rahman
rahman

Reputation: 4948

is it possible to iteration from .begin() to .rbegin()

I need to iterate from the beginning of a container to one element before the end. I can put an if condition inside the loop to bypass the last element but I was wondering if it would be possible to write the for loop like this:

for (it = C.begin(); it != C.rbegin(); it++){...}

if not, is there any suggestions?

Is it container dependent? (for now, I am using std::vector but it may change)

Upvotes: 0

Views: 295

Answers (1)

David G
David G

Reputation: 96845

In addition to the comment by Tony D, you can use std::prev(C.end()) to get the iterator that precedes the end iterator:

for (it = C.begin(); it != std::prev(C.end()); it++);
//                         ^^^^^^^^^^^^^^^^^^

Upvotes: 1

Related Questions