在C++中存储节点(i).x和节点(i).y数组值的最佳方式是什么?

在C++中存储节点(i).x和节点(i).y数组值的最佳方式是什么?

问题描述:

我必须处理图中节点的空间数据。数据类型/变量类型/数据结构允许我访问第i个节点的x值和第i个节点的y值的值。在C++中存储节点(i).x和节点(i).y数组值的最佳方式是什么?

std :: vector会做到这一点。

class Node 
{ 
    std::vector<NODE> mNodes; 
public: 
    int x, y; 
    Node& operator(int i) 
    { 
     return mNodes[i]; 
    } 
} 

现在如果你有一个节点定义为n可以如下访问存储在该节点的第i个节点:

Node n; 
// Populate Node 
int x = n(12).x; 
int y = n(14).y; 
+0

内的一个类的矢量相同的类可能无法编译,具体取决于实现或分配器。例如,尝试用deque替换它与 – Inverse 2010-11-22 15:54:34

struct Node{ 
    float x; 
    float y; 
} 
std::vector<Node> nodes; 
std::cout<<nodes.at(i).x;