C++ 增强C++;分段错误
每次我执行程序时(顺便说一句,没有错误),它都会给我一个分段错误。上面写着“分段故障堆芯倾倒”。所以我添加了一些cout,propertyC++ 增强C++;分段错误,c++,boost,runtime-error,C++,Boost,Runtime Error,每次我执行程序时(顺便说一句,没有错误),它都会给我一个分段错误。上面写着“分段故障堆芯倾倒”。所以我添加了一些cout,property>图; int no_树,no_边,start_树,finish_树,no_物种,w,min_边=0,答案[100]; 国际蜂箱[10]; 对于(int i=0;i>test; int m=0; while(m!=测试) { cin>>无树>>无边>>无物种>>开始树>>完成树;//获取输入 图g(无树); 属性映射类型:距离; 属性映射::类型索引; 属性映
对于(int j=0;j0您应该添加
我们需要阅读和理解的代码太多了。它在哪里出现故障?您是否附加了调试器?它也很难阅读。缩进很难理解。@Fredrarson很抱歉识别。@sam Miller在输入后出现故障。我在hives for loop后将其显示为故障。结果显示为seg所有的输入都有错误。我只是不知道我做错了哪个声明或循环。我知道一些内存有错误。只是找不到它。
[[A[rsridhar@stud13-h56 ~]$ ./ants
1
3 3 3 1 2
2 0 3 3 3
loope started
loop ended
loope started
loop ended
loope started
loop ended
2 1 4 4 4
loope started
loop ended
loope started
loop ended
loope started
loop ended
0 1 2 2 2
loope started
loop ended
loope started
loop ended
loope started
loop ended
hives started
0 1 2
hives ended
hives started
hives ended
hives started
hives ended
Segmentation fault (core dumped)
int main()
{
typedef adjacency_list< vecS, vecS, undirectedS, property<vertex_distance_t,int, property< vertex_index_t, int> >, property< edge_weight_t, int> > Graph;
int no_trees,no_edges,start_tree,finish_tree,no_species,w,min_edge=0,answers[100];
int hives[10];
for(int i=0;i<10;i++)
hives[i]=0;
int test=0;
cin>>test;
int m=0;
while(m!=test)
{
cin>>no_trees>>no_edges>>no_species>>start_tree>>finish_tree; //get input
Graph g(no_trees);
property_map< Graph,vertex_distance_t>:: type distance;
property_map< Graph,vertex_index_t>:: type index ;
property_map<Graph, edge_weight_t>::type weights[10];
for(int i=0;i<no_species;i++) //constructor for weights property
{
weights[i]=get(edge_weight,g);
}
typedef graph_traits<Graph>::edge_descriptor edge_desc;
for(int i=0;i<no_edges;i++) //input weights into weights array
{
int a=0,b=0;
cin>>a>>b;
edge_desc e;
bool bol;
tie(e,bol)=add_edge(a,b,g);
for(int j=0;j<no_species;j++)
{
cout<<"loope started"<<endl;
w=0;
cin>>w;
weights[j][e]=w;
cout<<"loop ended"<<endl;
}
}
for(int k=0;k<no_species;k++)
{ cout<<"hives started"<<endl;
w=0;
cin>>w;
hives[k]=w;
cout<<"hives ended"<<endl;
}
//find the minimum spanning tree for each of the weights in the graph
cout<<"section 1";
vector < vector< graph_traits < Graph >::vertex_descriptor> > p(10, vector<graph_traits < Graph >::vertex_descriptor>(num_vertices(g),0)) ;//predecessor vector
cout<<"section2";
//calling prim function on all diff prop_edges
for(int i=0;i<no_species;i++)
{
cout<<"section3";
prim_minimum_spanning_tree( g, hives[i], &p[i][0], distance, weights[i], index, default_dijkstra_visitor());
cout<<"section4";
}
Graph g1(no_trees);
property_map<Graph, edge_weight_t>:: type weight=get(edge_weight, g1);
//join all the spanning trees into one
int vertice_1,vertice_2;
typedef graph_traits<Graph>::edge_iterator edge_itr;
edge_itr ei,ee;
for(tie(ei,ee)=edges(g);ei!=ee;++ei)
{ min_edge=0;
cout<<"section5";
vertice_1=source(*ei,g);
vertice_2=target(*ei,g);
for(int j=0;j<no_species;j++)
{
if(p[j][vertice_1]==vertice_2 || p[j][vertice_2]==vertice_1)
{
if(weights[j][*ei]<min_edge)
min_edge=weights[j][*ei];
}
}
weight[*ei]=min_edge;
bool bl;
edge_desc c;
cout<<"section6";
tie(c,bl)=add_edge(vertice_1,vertice_2,g1);
}
std::vector<graph_traits<Graph>::vertex_descriptor> predecessors(num_vertices(g1));
std::vector<int> distances(num_vertices(g1));
//call dijkstra algorithm()
dijkstra_shortest_paths(g1,start_tree,predecessor_map(&predecessors[0]).distance_map(&distances[0]));
answers[m]=distances[finish_tree];
m++;
}
for( int i=0;i<test;i++)
cout<<answers[i]<<endl;
return 0;
}