当前位置: 动力学知识库 > 问答 > 编程问答 >

c++ - What does the following code do?

问题描述:

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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: