How can I iterate through a string and also know the index (current position)?
Like this:
std::string s("Test string");
std::string::iterator it = s.begin();
//Use the iterator...
++it;
//...
std::cout << "index is: " << std::distance(s.begin(), it) << std::endl;
I've never heard of a best practice for this specific question. However, one best practice in general is to use the simplest solution that solves the problem. In this case the array-style access (or c-style if you want to call it that) is the simplest way to iterate while having the index value available. So I would certainly recommend that way.
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];