What is the best way to store node (i) .x and node (i) .y values ​​in C ++?

I need to process spatial data that are nodes in a graph. What data type / type / structure of data data allows me to access the values ​​of the i-th value of node x and i-th node y.

+3
source share
2 answers

std :: vector will do this.

t

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

Now, if you have a Node defined as n, you can access the ith Node stored in this Node as follows:

Node n;
// Populate Node
int x = n( 12 ).x;
int y = n( 14 ).y;
+3
source
struct Node{
    float x;
    float y;
}
std::vector<Node> nodes;
std::cout<<nodes.at(i).x;
+3
source

Source: https://habr.com/ru/post/1776250/


All Articles