質問

struggling a bit with getting ahold of map keys. I am wanting this method to modify a vector of edges where the index is the dest node and the value at that index is the weight. The vector "edges" is already initialized large enough to hold the edges. The compiler is complaining that it cannot convert iterator type to int. Anyone have any advice or thoughts on how to handle this conversion if it is even possible? Thanks. This is part of an assignment I am working on implementing Dijkstra's shortest path for a MOOC.

void CompleteGraph::GetNodeEdges(int Node, std::vector<int> &edges){
    // Modifies a vector of edges given the source node sorted by edge weight

    // Iterate over a map. Grab all edges that have the given start node(map's 1st key)
    typedef std::map<int, std::map<int, int> >::iterator iter;
    for(iter i = Graph.begin(); i != Graph.end(); i++){
        if (i->first == Node){
            edges[i->second.begin()] =  i->second.end();
        }
    }

    // Sort vector here: will do this next
}
役に立ちましたか?

解決

Your problem is obviously on this line:

edges[i->second.begin()] =  i->second.end();

The type of the key of edges is int, but i->second.begin() is returning an iterator, because i->second returns a map. I guess you need something like:

edges[i->second.begin()->first] =  i->second.end();

Depending on what information from Graph you want to use, as you haven't told us about what the Graph represents.

他のヒント

I suspect you are looking for something like this:

iter i = Graph.find(Node);
if (i != Graph.end()) {
  map<int, int>& edges_map = i->second;
  for (map<int, int>::iterator m = edges_map.begin();
       m != edges_map.end(); ++m) {
    if (edges.size() <= m->first) {
      edges.resize(m->first + 1);
    }
    edges[m->first] = m->second;
  }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top