How to modify custom class key in map of maps C++?

I am making a graph using an adjacency list represented by a map with Vertex keys, and a map of weighted edges as a value. This looks like so:
map<Vertex, map<Vertex, double> > m;

Open in new window

The program loads a set of verticies an edges from a file in to this. Then I have to use Dijkstra's shortest path algorithm, given a starting an ending node in the Graph. In order to do this, you need to store certain attributes for each vertex, such as the min_distance and a pointer to the previous_node (so that you can trace the path after the algorithm finishes). The question is, how do I make it so that the keys' attributes in the vertex map are modifiable?

Selecting a key using an iterator and changing a variable in the Vertex object did not reflect any change.
map<Vertex, map<Vertex,double> >::iterator i;
i = m.find(Vertex);
Vertex v = i->first;
v.min_dist = 2;
v.prevNode = pnode; // this would point to a vertex obtained in a similar fashon to v

Open in new window

I can't get that to work, and am wasting space storing it in a separate map<Vertex,distance> and map<Vertex,prevNode> right now, there has to be a smarter way.
Who is Participating?
How about you store the adjacency list in a way that looks something like this instead :
typedef double Weight;
typedef double Distance;

class Node {
    Vertex vertex;
    Distance distanceFromStart;
    Node* previous;
    std::list<std::pair<Node*, Weight> > neighbors;

typedef std::vector<Node*> Graph;

class CompareNodeDist {
  public :
    bool operator()(const Node* node1, const Node* node2) const {
        return (node1->distance > node2->distance);

typedef std::priority_queue<Node*, std::vector<Node*>, CompareNodeDist> NodeQueue;

Graph graph;                 // holds the whole graph
NodeQueue unvisitedNodes;    // holds all nodes that still need to be visited (and have a distance that is not infinity)

Open in new window

This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.