我试图使用递归函数在有向图上应用连通图算法。递归函数在Graph类中,称为StrongDFS()
。当程序到达for循环时,它会崩溃并给我/ *错误消息:
"libc++abi.dylib: terminating with uncaught exception of type std::out_of_range: vector
check1check1Run Command: line 1: 72739 Abort trap: 6 ./"$2" "${@:3}""
我无法弄清楚为什么程序中的矢量会出现问题。任何帮助将不胜感激。
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
class Vertex {
public:
int currentDist; // needs to be updated
int id; // use id as index
int pred;
int num;
int in_stack;
vector<int> neighbors; // These need to be parallel
void setId (const int& x){
id = x;
setNum(0);
setPred(0);
}
void onStack(int os){
in_stack = os;
}
void addToniegh (const int& n){
neighbors.push_back(n);
}
void setNum(int sn){
num = sn;
}
void setPred(int p){
pred = p;
}
friend ostream& operator << (ostream& out, Vertex ver);
};
class Graph { // this class is going to contain the dequeue and print out the graph
public:
vector<Vertex*> verticies; // a list of all verticies
int w;// a counter
int c2;
int count;
int temp;
stack<int> theStack; // the numbers wait, if less than the head add to front
Vertex vertex;
void addVert (Vertex* v){
verticies.push_back(v);
}
int StrongDFS(int sd){
count++;
verticies[sd-1]->setPred(count);
verticies[sd-1]->setNum(count);
theStack.push(verticies[sd-1]->id);
cout << "Stack: " << theStack.top() << endl;
verticies[sd - 1]->onStack(1);
cout << verticies[sd-1]->num << " " << verticies[sd-1]->pred << " " << verticies[sd-1]->neighbors.size() << endl;
cout << "hello"<< endl;
for (int i = 0; i < verticies[sd - 1]->neighbors.size(); i++){
cout << "check1";
if (verticies[sd - 1]->num == 0){
cout << "CHECK";
StrongDFS(verticies[sd-1]->neighbors.at(i));
if (verticies[sd - 1]->pred > verticies[verticies[sd - 1]->neighbors.at(i) - 1]->pred)
verticies[sd - 1]->setPred(verticies[verticies[sd - 1]->neighbors.at(i) - 1]->pred);
}
else if (verticies[verticies[sd - 1]->neighbors.at(i)]->num < verticies[sd - 1]->num && verticies[verticies[sd]->neighbors.at(i)]->in_stack == 1){
if (verticies[sd - 1]->pred > verticies[verticies[sd - 1]->neighbors.at(i) - 1]->num)
verticies[sd - 1]->setPred(verticies[verticies[sd - 1]->neighbors.at(i) - 1]->num);
}}
if (verticies[sd-1]->pred == verticies[sd - 1]->num){
w = theStack.top();
verticies[w - 1]->onStack(0);
theStack.pop();
while (w != sd){
cout << "output " << char(w + 'a' - 1) << endl;
w = theStack.top();
verticies[w - 1]->onStack(0);
theStack.pop();
}
cout << "output " << char(w + 'a' - 1) << endl;
}
return w;
}
//return NULL;
};
ostream& operator << (ostream& out, Vertex ver) {
out << char(ver.id+'a'-1) << " (" ;
for (int i = 0; i < ver.neighbors.size(); i++){
if (i != ver.neighbors.size()- 1)
cout << char(ver.neighbors.at(i)+'a'-1) << ", ";
else
cout << char(ver.neighbors.at(i)+'a'-1);
}
cout << ") "<< "current Distance: " << ver.currentDist << endl;
return out;
}
int main(int argc, char *argv[]) {
Vertex v1; // A
Vertex v2; // B
Vertex v3; // C
Vertex v4; // D
Vertex v5; // E
Vertex v6; // F
Vertex v7; // G
Vertex v8; // H
Graph g1;
// - - - - - - - - - - - - A
v1.setId(1);
v1.addToniegh(3); // c
v1.addToniegh(4); // d
// - - - - - - - -- - - - - B
v2.setId(2);
v2.addToniegh(6); // f
// - - - - - - - - - - - - - C
v3.setId(3);
v3.addToniegh(1); // a
v3.addToniegh(5); // e
// - - - - - - - - - - - - - D
v4.setId(4);
v4.addToniegh(2); // b
v4.addToniegh(5); // e
// - - - - - - - - - - - - - E
v5.setId(5);
v5.addToniegh(6); // f
// - - - - - - - - - - - - - - F
v6.setId(6);
v6.addToniegh(7); // g
// - - - - - - - - - - - - - - G
v7.setId(7);
// - - - - - - - - - - - - - - H
v8.setId(8);
v8.addToniegh(6); // f
//----Adding them to vector in Graph ---
g1.addVert(&v1);
g1.addVert(&v2);
g1.addVert(&v3);
g1.addVert(&v4);
g1.addVert(&v5);
g1.addVert(&v6);
g1.addVert(&v7);
g1.addVert(&v8);
g1.StrongDFS(1);
//cout << endl;
//cout << g1; // I print here
//g1.proc(); // I start the graph here
//cout << g1; // I print here
}
答案 0 :(得分:1)
我看到的一个问题是你没有初始化Vertex
的成员变量。
构造一个Vertex
对象,使成员处于未初始化状态。然后问题出现在这样的行上:
verticies[sd - 1]->num
当我使用Visual Studio时,我得到了num
的狂野数字,所有这些都归因于成员变量未初始化。在输出中,我得到了这个:
Stack: 1
-858993459 -858993459 2
您应该编写一个初始化成员变量的默认构造函数:
class Vertex {
public:
int currentDist; // needs to be updated
int id; // use id as index
int pred;
int num;
int in_stack;
vector<int> neighbors; // These need to be parallel
Vertex() : currentDist(0), id(0), pred(0), num(0), in_stack(0) {}
//...
};
您对Graph
对象也做同样的事情。您创建一个包含未初始化成员的图表。 Graph
应该有一个默认构造函数:
Graph() : count(0), temp(0), c2(0), w(0) {}
如果没有这个,count
未初始化,您的StrongDFS
函数会在此处开始混乱:
count++;
由于count
未初始化,您不知道count
将是什么。
拥有未初始化的成员并不是一种好的做法,特别是如果您以后要使用这些成员。
现在,这些变化是否最终解决了您的问题,我不知道。我所知道的是,由于变量在具有初始值之前显然被使用,因此必须进行这些更改。