【发布时间】:2013-01-25 09:52:20
【问题描述】:
我在问自己是否有办法直接在参数中传递向量,我的意思是,像这样:
int xPOS = 5, yPOS = 6, zPOS = 2;
//^this is actually a struct but
//I simplified the code to this
std::vector <std::vector<int>> NodePoints;
NodePoints.push_back(
std::vector<int> {xPOS,yPOS,zPOS}
);
此代码当然会出错; typename 不允许,并且需要一个 ')'
我会使用结构,但我必须将数据传递到抽象虚拟机,我需要在其中以Array[index][index] 的形式访问节点位置,例如:
public GPS_WhenRouteIsCalculated(...)
{
for(new i = 0; i < amount_of_nodes; ++i)
{
printf("Point(%d)=NodeID(%d), Position(X;Y;Z):{%f;%f;%f}",i,node_id_array[i],NodePosition[i][0],NodePosition[i][1],NodePosition[i][2]);
}
return 1;
}
当然我可以这样做:
std::vector <std::vector<int>> NodePoints;//global
std::vector<int> x;//local
x.push_back(xPOS);
x.push_back(yPOS);
x.push_back(zPOS);
NodePoints.push_back(x);
或者这个:
std::vector <std::vector<int>> NodePoints;//global
std::vector<int> x;//global
x.push_back(xPOS);
x.push_back(yPOS);
x.push_back(zPOS);
NodePoints.push_back(x);
x.clear()
但是我想知道这两者中的哪一个会更快/更高效/更好? 或者有没有办法让我的初始代码工作(第一个 sn-p)?
【问题讨论】:
标签: c++ stl vector multidimensional-array