遍历对的C ++列表生成核心转储

时间:2017-03-08 04:10:24

标签: c++

我已经将用于插入边缘的小程序写入图形并且它正在生成代码转储。我试图遍历列表数据。 gdb调试器向我显示核心转储位置"cout<<it->first<<endl"这对我来说很奇怪任何输入

#include<iostream>
 #include<utility>
 #include<vector>
 #include<string>
 #include<list>

 using namespace std;

 class Graph {
     private:
         int V;
         list<pair <int,int> > *adj;
     public:
         Graph(int V);
         void addedge(int V, int U, int w);
         void printGraph();
 };

 Graph::Graph(int V)
 {
     this->V = V;
     adj = new list<pair <int, int> >[V];
 }

 void Graph::addedge(int V, int U, int W) {
     adj[V].push_back(make_pair(U,V));
 }

 void Graph::printGraph() {
     for(int i=0; i<V; i++){
         string s = to_string(V) + "->";
         for(list<pair<int,int> >::iterator it = adj[V].begin(); it != adj[V].end(); ++it) {
             cout<<it->first<<endl;
         }
     }
 }
 int main() {
     Graph g(10);
     g.addedge(0, 1, 2);
     g.addedge(1, 1, 2);
     g.addedge(2, 1, 2);
     g.printGraph();
     return 0;
 }

2 个答案:

答案 0 :(得分:1)

在函数void Graph::printGraph()中,在for循环中使用V,这对于所有迭代都是相同的。它应该是,

for(list<pair<int,int> >::iterator it = adj[i].begin(); it != adj[i].end(); ++it)

您已声明string s并且未在程序中的任何位置使用它。

答案 1 :(得分:0)

function millisToMidnight() { var date = new Date(); date.setDate(date.getDate() + 1); date.setHours(0); date.setMinutes(0); date.setSeconds(0); return date.getTime() - (new Date()).getTime(); } 中:printGraph()不应在循环中用作索引(需要使用V)。 下面的代码有效:

i