我試圖使用遞歸函數在有向圖上應用連接圖算法。遞歸函數在Graph類中,被稱爲StrongDFS()
。當程序到達for循環崩潰,並給了我/ *錯誤信息:以std :: out_of_range類型的未捕獲異常終止:向量C++
"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
}
請將其縮小。如果你錯過了任何事實,你需要自己做調試,然後找到關於編程語言的問題。乾杯 –