问题描述:

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.

网友答案:

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.

网友答案:

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.

相关阅读:
Top