Range based for loops

Aug 7, 2014 at 5:12pm
Can anybody explain Range based for loops ?
Aug 7, 2014 at 5:20pm
I typed 'C++ range based for loop' into google and this was the first link:

http://www.cprogramming.com/c++11/c++11-ranged-for-loop.html

It explains it in detail.
Last edited on Aug 7, 2014 at 5:20pm
Aug 8, 2014 at 4:17pm
I am studying C++ from this site and I havent studied arrays, STL, vector and all that stuff . Obviously I visited that link but it was of no use for me .
Aug 8, 2014 at 4:22pm
Did you check the tutorial on this site? Scroll down this page for the range based for loop info.

http://www.cplusplus.com/doc/tutorial/control/
Aug 8, 2014 at 4:23pm
Range-based for loops are used to iterate over each element in a container such as an array, vector, list, etc.
Last edited on Aug 8, 2014 at 5:42pm
Aug 8, 2014 at 4:38pm
In this syntax
// range-based for loop
#include <iostream>
#include <string>
using namespace std;

int main ()
{
string str {"Hello!"};
for (char c : str)
{
std::cout << "[" << c << "]";
}
std::cout << '\n';
}
WHY IS STD::COUT USED WHEN USING NAMESPACE STD DECLARATION IS ALREADY MENTIONED BEFORE INT MAIN ? ()
Aug 8, 2014 at 5:45pm
It wouldn't be necessary. Maybe the person writing that section is accustomed to using the std:: prefix where necessary instead of pulling the whole standard namespace into programs?
Aug 8, 2014 at 6:33pm
It's a bit inconsistent compared to the rest of the tutorial code.
Topic archived. No new replies allowed.