从向量改变事物 [英] Changing things from a vector

查看:22
本文介绍了从向量改变事物的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从向量中取出某些内容并更改其值的最简单方法是什么?这不像我预期的那样工作.

What is the easiest way to take something out of an vector and changes its values? This isn't working the way I expected.

for(int i = 0; i < list.size(); i++) {
someType s = list.at(i);
s.x = 10;
s.y = 20;
s.z = 30;
}

然而,当我打印出 someType s 的 x,y,z 时,它并没有给我预期的结果.抱歉,我混淆了 Java 和 C++.

However, when I go to print out the x,y,z of that someType s, it doesn't give me what I expect. Sorry I am mixing up Java and C++.

推荐答案

someType s = list.at(i);

您正在制作索引 i 处的元素的副本.在 C++ 中,如果你想引用某个东西,你需要明确声明你想要一个引用:

You are making a copy of the element at index i. In C++, if you want a reference to something, you need to explicitly state that you want a reference:

someType& s = list.at(i);
     // ^ I'm a reference

正如现在所写,您只能操作您制作的副本,而不是容器中的对象.

As it is written now, you only manipulate the copy that you make, not the object in the container.

这篇关于从向量改变事物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆