【发布时间】:2016-06-21 09:02:12
【问题描述】:
我有一个std::vector<vector<double>> 我想填写一个函数。
我需要存储一些 3 维坐标,以便稍后在我的程序中进行一些计算。
我的问题是如果我这样做:
//in classdefinition
std::vector<std::vector<double>> data;
myFunc()
{
std::vector<double> temp;
temp.push_back(1);
temp.push_back(2);
temp.push_back(3);
data.push_back(temp);
temp.clear();
//new data
temp.push_back(2);
temp.push_back(3);
temp.push_back(4);
data.push_back(temp);
}
temp的清空和refill会影响data中的值吗?
我已经找到了这个http://www.cplusplus.com/reference/vector/vector/push_back/ 但由于解释为“val 的内容被复制(或移动)到新元素。”我不知道该怎么想。对我来说,这听起来很矛盾。
我认为,如果将变量作为引用传递,那将没有多大意义,因为它们就像我的情况一样,只能在有限的范围内有效。 我的假设是否正确?
【问题讨论】: