我認爲我錯誤地創建了這對,因爲我在使用DDD進行調試時遇到段錯誤。任何人都可以看到我犯了什麼錯誤嗎?謝謝!處理輸入文件時,段錯誤發生:如何初始化一對
char vertex = 'a';
Vertex* newVertex = new Vertex();
map.insert(pair<char,Vertex*>(vertex,newVertex));
代碼:
void MSTapp::processFile()
{
int pos1;
int pos2;
map<char, Vertex*> adjacencyList;
vector<char> listOrder;
string input;
bool test = false;
while (getline(cin, input)) {
pos1 = pos2 = 0;
if(std::string::npos != input.find_first_of(""))
{
char source = input[0];
char destination = input[2];
stringstream ss(input.substr(4));
int weight;
ss >> weight;
Edge newEdge(destination, weight);
adjacencyList[source]->addEdge(destination, newEdge);
Edge roadBack(source, weight);
adjacencyList[destination]->addEdge(source, roadBack);
}
else
{
while(input.find(' ', pos1) != string::npos)
{
pos2 = input.find(' ', pos1);
char vertex = input[pos1];
listOrder.push_back(vertex);
Vertex* newVertex = new Vertex(vertex);
adjacencyList[vertex] = newVertex;
pos1 = pos2;
};
};
};
Graph graph(listOrder, adjacencyList);
prim(graph, adjacencyList[listOrder[0]]);
}
輸入:
A B C D E F G
A B 3
A E 4
B C 7
B E 6
B F 5
C D 9
C F 8
D F 9
D G 4
E F 6
F G 8
你在哪一行得到你的段錯誤? –
你可能不需要指針。只要'pair'通常應該這樣做,否則就是智能指針。 –
chris
我在map.insert行添加了segfault。我正在嘗試爲無向圖創建一個鄰接列表,以便我可以將prim的算法應用於創建最小生成樹 – CodeManiak