【发布时间】:2014-09-05 19:30:04
【问题描述】:
背景:我是一个 hack,无论如何都不是一个好的程序员,FORTRAN 实际上是我的语言,需要一些关于这个 boost 代码的帮助。当我实现这个时,我在向量的第 779 行得到一个“向量下标超出范围”错误。这是我的代码,任何帮助将不胜感激!
程序从文件中读取边,构建图,然后我想计算一个顶点到所有其他顶点的距离。
这个问题看起来应该很容易解决,但我很苦恼,谢谢!
-杰夫
#include "stdafx.h"
#include <stdexcept>
#include <boost/graph/adjacency_list.hpp>
#include <iostream>
#include <utility>
#include <algorithm>
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/dijkstra_shortest_paths.hpp>
#include <boost/graph/compressed_sparse_row_graph.hpp>
#include <boost/graph/dijkstra_shortest_paths_no_color_map.hpp>
#include <fstream>
#include <string>
#include <stdio.h>
#include <time.h>
#include <iostream>
using namespace std;
using namespace boost;
int main()
{
long int num_nodes;
long int num_edges;
char *gfile ; //graph data file
char buffer[128] ;
istringstream instream ;
gfile="grid.out";
ifstream infile( gfile, ios::in );
infile.getline(buffer, 128);
instream.clear() ;
instream.str(buffer) ;
instream >> num_nodes >> num_edges;
float wt; // Allocate n ints and save ptr in a.
int num_arcs = num_edges;
int n1,n2;
cout << "READING graph into C++ program from file grid.out"; cout << endl;cout << endl; cout << endl;
cout << "Found ";cout << num_nodes; cout << " nodes, and "; cout << num_edges; cout << " edges";cout << endl;
typedef std::pair < int, int > Edge;
struct Edge_Cost
{
double weight;
};
struct vertex_data {
boost::graph_traits<
boost::compressed_sparse_row_graph< boost::directedS > >::vertex_descriptor p;
double d;
};
typedef boost::compressed_sparse_row_graph<
boost::directedS,
vertex_data,
Edge_Cost
> graph_t;
typedef boost::graph_traits < graph_t >::vertex_descriptor
vertex_descriptor;
graph_t* Graph;
std::vector<vertex_descriptor> p;//path
std::vector<double> d; //distance
int numb_edges = num_edges;
Edge* edge_list = new Edge [numb_edges];
Edge* ptr_edge_list = edge_list;
Edge_Cost* edge_weight_list = new Edge_Cost[numb_edges];
Edge_Cost* ptr_edge_weight_list = edge_weight_list;
for (int i=0; i<num_edges; ++i)
{
//get a line from the file, vertex n1 is linked to vertex n2 by weight wt, but first vertex in file is 1 not 0, so -1
//here is an example of the file:
//grid.out: first line is nvertex, nedges
//10 10
//1 2 0.296033428
//2 3 0.038954928
//3 4 0.080953663
//4 5 0.917876269
//5 6 0.026168687
//6 7 0.037261078
//7 8 0.409351058
//8 9 0.398426038
//9 10 0.942112529
//10 11 0.006422981
infile.getline(buffer, 128);
instream.clear() ;
instream.str(buffer) ;
instream >> n1 >> n2 >> wt;
*ptr_edge_list++ = Edge(n1-1,n2-1);
(ptr_edge_weight_list++)->weight =wt;
}
Graph = new graph_t(boost::edges_are_unsorted,edge_list,ptr_edge_list,edge_weight_list,num_nodes);
vertex_descriptor s = vertex(1,*Graph);
//get one set of distances
boost::dijkstra_shortest_paths_no_color_map
(*Graph,s,
boost::predecessor_map(&p[0]).
distance_map(&d[0]).
weight_map(boost::get(&Edge_Cost::weight, *Graph))
);
return 0;
}
【问题讨论】:
-
"'向量下标超出范围' ..." 学习如何调试代码的良好起点。启动调试器,并逐行执行! (“我是个黑客,不是一个好的程序员”不,你不是,黑客首先应该知道如何编写好的程序,但要探索克服这个问题的技术)
-
谢谢,我确实知道该怎么做(是的,我只是半个黑客),仍然没有帮助,似乎图表构建正确,节点数,边等。