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.
l4zarusAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Infinity08Commented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mlmccCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Algorithms

From novice to tech pro — start learning today.