I'm trying to figure out the best way to determine whether I'm in the last iteration of a loop over a map in order to do something like the following:
for (iter = someMap.begin(); iter != someMap.end(); ++iter) {
bool last_iteration;
// do something for all iterations
if (!last_iteration) {
// do something for all but the last iteration
}
}
There seem to be several ways of doing this: random access iterators, the distance
function, etc. What's the canonical method?
Edit: no random access iterators for maps!
Modified Mark Ransom's so it actually work as intended.
Here's my optimized take:
Full program:
This program yields:
Surprised no one mentioned it yet, but of course boost has something ;)
Boost.Next (and the equivalent Boost.Prior)
Your example would look like:
How about this, no one mentioning but...
this seems simple, mm...
If you just want to use a ForwardIterator, this should work: