[英]C++, When getting values from a list that I populate the arrays inside are returning nothing
好吧,對於我想在算法類中的一個項目,從.txt文件讀取迪士尼樂園地圖中的所有點,然后使用prims算法解決MST問題。
我的問題是,我使用''分隔符將文件中的值解析為臨時數組,然后將其推送到列表中。 一切工作正常且繁瑣,直到將數組推入列表,然后在程序中稍后接收到該值時,它不返回任何值。 我知道它有些愚蠢,但希望大家都能提供幫助。
我的代碼: http : //pastebin.com/rS6VJ6iJ
disneyland.txt: http : //pastebin.com/f78D0qrF
Output:
//testing arrays' value before pushing into list
id: 1 ,x: 957 ,y: 685 ,name: RailRoadMainStreet
id: 2 ,x: 1009 ,y: 593 ,name: MainStreetCinema
id: 3 ,x: 930 ,y: 661 ,name: FireEngine
id: 4 ,x: 991 ,y: 665 ,name: HorseDrawnStreetcars
id: 5 ,x: 945 ,y: 673 ,name: HorselessCarriage
id: 6 ,x: 1038 ,y: 668 ,name: Omnibus
id: 7 ,x: 1062 ,y: 670 ,name: DisneyGallery
id: 8 ,x: 1063 ,y: 649 ,name: GreatMomentsWithMrLincoln
id: 9 ,x: 969 ,y: 562 ,name: BlueRibbonBakery
id: 10 ,x: 968 ,y: 579 ,name: CarnationCafe
... to 84 id
//now retreving values from list after been pushed(empty)
id: ,x: ,y: ,name:
id: ,x: ,y: ,name:
id: ,x: ,y: ,name:
id: ,x: ,y: ,name:
id: ,x: ,y: ,name:
id: ,x: ,y: ,name:
id: ,x: ,y: ,name:
id: ,x: ,y: ,name:
id: ,x: ,y: ,name:
id: ,x: ,y: ,name:
id: ,x: ,y: ,name:
id: ,x: ,y: ,name:
id: ,x: ,y: ,name:
id: ,x: ,y: ,name:
id: ,x: ,y: ,name:
id: ,x: ,y: ,name:
id: ,x: ,y: ,name:
... to 84 id
我知道這很愚蠢,但是我暫時無法解決。
編輯:
現在我變得亂碼,因為程序正在讀取文件的結尾的空白行,我不想被讀取,因為沒有值亂碼:id:84. 1222 422)明天世界露台
更新的代碼部分導致錯誤:
if (data.is_open())
{
while (!data.eof())
{
getline(data,output);
if (counter == 0) //grabbing the total amount of vertcies
{
total = atoi(output.c_str());
}else if(counter == total+1){
//no nothing , blank line. THIS IS CAUSING ERRORS
}
else{ // now parsing line into an array then pushing it into the remaining list.
infoVert = new string[4];
temp = parseLine(infoVert,output,' ');
tmpVert.push_front(temp);
}
counter++;
}
}
//---------------------
//cleaning up the mess.
data.close();
delete [] infoVert;
//---------------------
問題是您要刪除已添加到列表中的數組
string* parseLine(string* ary,string line,char delim)
{
...
return ary;
}
infoVert = new string[4];
getline(data,output);
temp = parseLine(infoVert,output,' ');
cout << "id: " << temp[0] << " ,x: " << temp[1] << " ,y: " << temp[2] << " ,name: " << temp[3] << endl;
rVert.push_front(temp);
delete [] infoVert;
看一下parseLine
,它的書寫方式意味着temp == infoVert
,因此實際上您是將infoVert
推送到列表中,但是在下一行刪除了infoVert
。
您無法delete[] infoVert
但實際上您應該有一個向量列表而不是指針列表。
list<vector<string> > rVert;
list<vector<string> > tVert;
不使用指針,編程更容易。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.