小编vec*_*nia的帖子

在矢量/列表中,您实际将它应用于对象吗?(C++)

假设我有一个向量:

vector<Block> myBlocks;
Run Code Online (Sandbox Code Playgroud)

让我们说我这样做:

Block b(10); //Let's assume this will set b's variable: x to 10
myBlocks.push_back(b); //Add it to the vector/list
Run Code Online (Sandbox Code Playgroud)

现在,如果我在其他地方做了这件事:

for(int i = 0; i<myBlocks.size(); i++){
    Block c = myBlocks.at(i);
    c.x = 15; //Will this work?
}
Run Code Online (Sandbox Code Playgroud)

我把它放在哪里c.x = 15;,是否真的将该块的x位置设置为15?或者只是将临时块的x位置设置为15?如果它只设置临时块,我该如何为实际块设置它?

c++ vector

2
推荐指数
1
解决办法
78
查看次数

标签 统计

c++ ×1

vector ×1