What means that expression?

I came across this entry:
int main()
{
 std::list<int> x = {1, 2, 2, 3, 3, 2, 1, 1, 2};

 std::cout << "contents before:";
 for (auto val : x)
 std::cout << '' << val;
 std::cout << '\n';

}</int>


Can't understand the meaning:
for (auto val : x)

And is for should not have such a template?
for (; ;)
July 4th 19 at 22:52
2 answers
July 4th 19 at 22:54
Solution
Discover C++11. This is a new language standard, which has an improved syntax.

for (auto val : x) is a cycle in the collection x, the variable x will be copied to each item in the collection.
Do not tell me where can I read all the innovations in the 11 and 14 versions? - Chadd_Hane commented on July 4th 19 at 22:57
: https://habrahabr.ru/post/248137/ - orin.Runte commented on July 4th 19 at 23:00
: Thanks! - Chadd_Hane commented on July 4th 19 at 23:03
July 4th 19 at 22:56
Solution
The loop is equivalent to this:
for (auto val = x.begin(); val != x.end(); val++)
which is a loop using iterators
auto - automatic selection of the type of the variable. I do not recommend to abuse it.
Do not have.
Feature from C++11;
Equivalence is wrong - your version will be an iterator and not a copy of the item. - Chadd_Hane commented on July 4th 19 at 22:59
val ++implementation of operator faster - orin.Runte commented on July 4th 19 at 23:02
It is equivalent to this:
{

 auto && __range = range_expression ;
 auto __begin = begin_expr ;
 auto __end = end_expr ;
 for ( ; __begin != __end; ++__begin) {

 range_declaration = *__begin;
loop_statement

}

}
- Chadd_Hane commented on July 4th 19 at 23:05

Find more questions by tags C++