这是什么“用于”做?它是如何工作的? [英] What does this "for" do? How does it work?
问题描述
这是做什么的?它是如何工作的?
for(std :: vector< t>& column:mCells){column.resize(mHeight); }
我尝试了什么:
这是做什么的?它是如何工作的?
for(std :: vector< t>& column:mCells){column.resize(mHeight); }
What does this for do? How does it work?
for (std::vector<t>& column : mCells) { column.resize(mHeight); }
What I have tried:
What does this for do? How does it work?
for (std::vector<t>& column : mCells) { column.resize(mHeight); }
推荐答案
它适用于C ++中的每个循环,并迭代您(最有可能)网格中的列。它被称为基于范围的循环;因为你隐含了一个范围。
一旦获得专栏,它会将其调整到特定高度 - 同时注意参考符号。如果没有pass-by-reference,您的操作将对列没有影响。我创建的一个快速示例就是这里,
It is a for each loop in C++, and iterates over the columns that you have in your (most likely) grid. It is called a range-based loop; since you get a range implicitly in it.
And once it gets the column, it resizes it to a specific height—also notice the reference sign. Without the pass-by-reference your operation will have no effect on the columns. A quick example I created to demonstrate is here,
void processprint() {
int array[] = { 1, 2, 3, 4 };
// Increment the values, without modifying actual elements.
for (int item : array) {
item += 2;
}
for (int item : array) {
std::cout << item << std::endl;
}
}
这将打印相同的数组,不会改变,但是如果你更改了这行,
This will print the same array, unaltered, however if you change the line,
void processprint() {
int array[] = { 1, 2, 3, 4 };
// Increment the values, change actual values.
for (int& item : array) {
item += 2;
}
for (int item : array) {
std::cout << item << std::endl;
}
}
现在将打印递增的值。在这里查看, C ++ Shell [ ^ ]
基于范围的循环(自C ++ 11开始) - cppreference.com [ ^ ]
This will now print incremented values. Check it out here, C++ Shell[^]
Range-based for loop (since C++11) - cppreference.com[^]
它是如何工作的?
对你来说,它是神奇的工作!
你有没有计划问我们C ++的每一个不寻常的语法?
有人可以告诉我这是怎么回事声明有效吗? [ ^ ]
为什么在方法声明结束时它们是= 0? [ ^ ]
clone()是什么意思?为什么他们是< gamepiece>?之后的方法和定义[ ^ ]
我遇到了麻烦:意思。 [ ^ ]
怎么会发生这种情况? [ ^ ]
请告诉我这些例子中单个冒号的用途。 [ ^ ]
请告诉我这些单分号的用途是什么示例。 [ ^ ]
请帮助我理解&的使用在这个例子中。一直在搜索和搜索。 [ ^ ]
C ++提供了解释每种语法的文档。
您可以在互联网上找到教程。
For you, it work by magic !
Do you have plan to ask us every single unusual syntax of C++ ?
Can someone tell me how this if statement works?[^]
Why is their a "=0" at the end of the method declaration?[^]
What does clone() mean? And why is their a method and a definition after <gamepiece>?[^]
I am having trouble with : meaning.[^]
How is it that this can happen?[^]
Please tell me what the single colon is used for in these examples.[^]
Please tell me what the single semicolon is used for in these examples.[^]
Please help me understand the use of & in this example. Been searching and searching.[^]
C++ have documentation where every single syntax is explained.
Chances are that you can also find tutorials on internet.
这篇关于这是什么“用于”做?它是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!