How to implement node references in a node class

I am trying to write a small quadege class in C ++ (think of it as some kind of graph). Each node must keep track of its neighbors. I should only track outgoing arcs.

The first idea is to use pointers for this:

struct Node {
         //....
         Node * n1,n2,... nk;
};

However, this approach is especially painful when you need to implement the copy constructor * (first copy all the nodes, then map each pointer to the old nodes with a relative pointer to the new nodes),

I think using integer indices instead of pointers would be a better idea in this case.

 struct Node {
        //....
        int n1,n2,...nk;
 };

Is this approach general and correct? If so, what is the proper container for mapping indices to nodes?

std::vector<Node>, , , Node, , , node ( ).

std::unordered_map<int,Node> , ( 1,2,3, 2, , 2 ).

, , . , .

- ( Boost )?

* , ,

+3
2

int , . : , , node, node (, index) , .

, vector<Node*> unordered_map<Node*,int>. : node node . , node , .

+1

, node .

node , node int.

, std:: unordered_map, , , .

a node ; , , .

0

All Articles