How can I iterate through a string and also know the index (current position)?

前端 未结 7 709
小蘑菇
小蘑菇 2020-12-14 14:27

Often when iterating through a string (or any enumerable object), we are not only interested in the current value, but also the position (index). To accomplish this by using

7条回答
  •  隐瞒了意图╮
    2020-12-14 15:18

    You can use standard STL function distance as mentioned before

    index = std::distance(s.begin(), it);
    

    Also, you can access string and some other containers with the c-like interface:

    for (i=0;i

提交回复
热议问题