I'm an experienced programmer but new to C++. I'm following the C++ Primer fifth edition text book and it encourages the use of the 'auto' keyword in range based for loops. For example:
string s = "hello";
for (auto c : s){
//do some stuff
}
instead of
string s = "hello";
for (char c : s){
//do some stuff
}
While this is certainly convenient, I don't see why it's sensible to use auto when the actual type is known. Or is there some special case where the type may not be known? I've looked around the web and this does indeed seem to be the done thing, but I couldn't find a good explanation. And the otherwise excellent book hasn't offered one so far either.
Can you help? Thanks!
Edit: Turns out there are many discussions on this already. Some are linked to in the comments. Thanks for the tips.