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

前端 未结 7 703
小蘑菇
小蘑菇 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<string1.length();i++) string1[i];
    
    0 讨论(0)
提交回复
热议问题