Skipping in Range-based for based on 'index'?

后端 未结 7 1368
一向
一向 2021-01-02 01:10

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

7条回答
  •  [愿得一人]
    2021-01-02 01:53

    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++)
    

提交回复
热议问题