0

I was wondering what the following code does:

for (auto x:m) std::cout << x << " ";

I already know that auto is a way to leave it to the compiler to decide the type of the variable but I don't know what :m does.

Sergey K.
  • 24,894
  • 13
  • 106
  • 174
fer y
  • 505
  • 4
  • 19

2 Answers2

0

It is a C++11 range-based for loop syntax described here: http://www.cprogramming.com/c++11/c++11-ranged-for-loop.html

Here m should be a container, like std::vector. The code will iterate the container and put every element (accessed as x inside the loop) into the std::cout stream. Elements will be separated by space.

Sergey K.
  • 24,894
  • 13
  • 106
  • 174
0

m is any type that follows the ranged concept (i.e. Container concept).

The loop iterates over all elements of m where x represents the currently iterated value.

Pixelchemist
  • 24,090
  • 7
  • 47
  • 71