2

I have a C++ problem here.

I have three classes: Graph, Nodes and Edges, to model a simple Graph. Now an object of the Graph class has a vector with objects of the Node class and a vector with objects of the Edge class.

An object of the Node class has to know all incoming and outgoing edges. And an object of the edge class has to know from which node it comes and to which node it goes. Now i have the following problem: All edges in the graph are stored in the vector in the edge class. Objects of the node class have to know some edges as well, so I gave them a vector of pointer to point to the belonging edges in the vector of the graph. But my problem is now: If I remove an edge from the vector of the graph the pointers are wrong.

I don't want to copy eache edge into two vectors, so i think i have to do this that way but I really see the problem of removing something from the vector.

Is there a solution or do I have to copy each edge? Could someone help me please?

5
  • So that our help can be most valuable, can you tell us is this homework? That is, is this a learning exercise, or is this a work or pleasure exercise? Commented Nov 1, 2011 at 20:41
  • 1
    @Rob: 99% says not homework. Perhaps academic, but not homework Commented Nov 1, 2011 at 20:42
  • Well it is academic. The only problem I have is this. Everything else in this exercise is clear for me and will be written easily but I have problems with this vector thing. Because I have to do that with vectors and I may not copy too many objects. Commented Nov 1, 2011 at 20:50
  • 2
    Tell us more about "I have to do that with vectors". Why not use, e.g., std::list? Commented Nov 1, 2011 at 21:03
  • In the exercise are some declarations of the functions shown which I have to use as they are shown there. And those which give back all available edges or nodes are have 'vector' as functiontype. Commented Nov 2, 2011 at 6:14

1 Answer 1

1

I'd use descriptors or simply use shared_ptr's for all containers.

Descriptors are weapon of choice in Boost Graph Library

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.