#include <iostream>
#include <vector>
using namespace std;
struct Neighbor
{
int index;
int weight;
Neighbor(int, int);
};
Neighbor::Neighbor(int index, int weight)
{
Neighbor::index = index;
Neighbor::weight = weight;
}
void addEdge(vector<vector<Neighbor> > &graph, int v1, int v2, int weight)
{
graph[v1].push_back(Neighbor(v2, weight));
graph[v2].push_back(Neighbor(v1, weight));
}
int main()
{
vector<vector<Neighbor> > graph;
vector<vector<Neighbor> > graphIterator;
graph[0].push_back(Neighbor(1, 5));
graph[0].push_back(Neighbor(3, 3));
graph[0].push_back(Neighbor(4, 2.5));
graph[1].push_back(Neighbor(0, 5));
graph[1].push_back(Neighbor(2, 3));
graph[2].push_back(Neighbor(1, 3));
graph[2].push_back(Neighbor(4, 2.5));
graph[2].push_back(Neighbor(3, 5));
graph[3].push_back(Neighbor(0, 3));
graph[3].push_back(Neighbor(2, 5));
graph[3].push_back(Neighbor(4, 2.5));
graph[4].push_back(Neighbor(0, 2.5));
graph[4].push_back(Neighbor(2, 2.5));
graph[4].push_back(Neighbor(3, 2.5));
return 0;
}
上面是我的代碼,運行時似乎崩潰了。儘管聲明向量圖似乎工作正常,但即使包含我的第一個push_back語句,程序也會崩潰。有人可以提醒我嗎?C++崩潰:嵌套向量上的push_back
謝謝。你的第二個解決方案完美運作 –