Is there a way to access the iterator (suppose there\'s no loop index..?) in a C++11 range-based for loop?
Often we need to do something special with the fi
No, you can't get the iterator in a range-based for
loop (without looking up the element in the container, of course). The iterator is defined by the standard as being named __begin
but this is for exposition only. If you need the iterator, it is intended that you use the normal for
loop. The reason range-based for
loop exists is for those cases where you do not need to care about handling the iteration yourself.
With auto
and std::begin
and std::end
, your for
loop should still be very simple:
for (auto it = std::begin(container); it != std::end(container); it++)