In modern C++, the idiom for iterating a sequential collection like string or vector when you only need the value of each element is short and elegant:
for (auto x: xs)
When you also need the index, it’s a little less elegant:
for (size_t i = 0; i != xs.size() ++i)
… unless there’s some recent development I haven’t yet caught up with. Does C++11 have a preferred way of doing the latter, or is the above still as good as it gets?
The preferred and idiomatic way is the simple for loop.
Alternative methods include using an integer range:
Or an iterating function:
Just go with the simple loop whenever possible though. It’s superior in my opinion.