【发布时间】:2014-11-18 21:54:19
【问题描述】:
我有一个
我的代码基本上是一个 BFS 图搜索,用于查找 2 个顶点之间的最短路径。
queue<Vertex*> myqueue;
unordered_map<Vertex*,Vertex*> mymap;
std::vector<Vertex*>* v;
//std::vector<Vertex*>* d;
Vertex* k;
Vertex* f;
Vertex* l;
//here I will store the visited vertices(ones that've been put into the queue.) as well as the the previous nodes of this unordered map.
if(!containsVertex(v1)||!containsVertex(v2))
{
cout<<"The vertices you wish to find the shortest path between, don't exist. You might want to check them."<<endl;
return NULL;
}
myqueue.push(v1);
mymap[v1]=NULL;
cout<<"Pushed v1"<<endl;
while(!myqueue.empty())
{
cout<<"Entered while loop"<<endl;
f=myqueue.front();
myqueue.pop();
cout<<"executed popping"<<endl;
for(int i=0;i<neighbors(f).size();i++)
{
cout<<"entered for loop"<<endl;
if(neighbors(f).at(i)==v2)
{
cout<<"found the guy. Preparing to output the vector"<<endl;
k=v2;
mymap[v2]=f;
cout<<"Is the fault here?"<<endl;
while(mymap[k]!=v1)
{
cout<<"Entered the while loop"<<endl;
v->push_back(k);
cout<<"After the pushback?"<<endl;
cout<<v->size()<<endl;
cout<<"After outputting the size?"<<endl;
l=mymap[k];
cout<<"After the mapping retrieval?"<<endl;
k=l;
cout<<"after the end assignment?"<<endl;
}
cout<<"Exited the while loop"<<endl;
v->push_back(v1);
return v;
}
if(mymap.find(neighbors(f).at(i))==mymap.end())
{
myqueue.push(neighbors(f).at(i));
mymap[neighbors(f).at(i)]=f;
}
}
}
cout<<"There doesn't exist a shortest path between these two vertices."<<endl;
return NULL;
段错误出现在我尝试开始推动向量中的元素的部分。
【问题讨论】:
-
所以,您只想清除指标,而不是修复错误?无论如何,需要一个MCVE。
标签: c++ vector segmentation-fault breadth-first-search