我的结构是这样的:C++ - 操纵STL向量中的对象?
struct Rz3DContourNode {
float x;
float y;
float z;
float nx;
float ny;
float nz;
};
我存储在一个STL向量元素如下:
std::vector <Rz3DContourNode> nodes;
现在我想改变NX,在矢量节点纽约compoent。我尝试如下:
*(&nodes[i].nx)=newNXValue;
*(&nodes[i].ny)=newNYValue;
*(&nodes[i].nz)=newNZValue;
这没有工作。是因为节点[我]返回对象的副本?
有没有这种期望使用指针的解决方案?
我不能使用指针(Rz3DContourNode *),因为我在OpenGL中使用该载体作为
glNormalPointer(GL_FLOAT,sizeof(Rz3DContourNode), &nodes[0].nx);
编辑 - 我很抱歉。其实保持的QList和获得的std ::向量每次。这就是原因
std::vector<Rz3DContourNode> nodeVector = nodes.toVector().toStdVector();
'节点[I]'不返回对象的副本,你可以发布一些失败的代码? '*(&nodes [i] .nx)= newNXValue;'可以/应该简化为'nodes [i] .nx = newNXValue;'。 –
谢谢,我每次都得到了这个向量的副本。这就是原因。 –