当从列表中获取值时,我填充其中的数组不返回任何值

C++, When getting values from a list that I populate the arrays inside are returning nothing

本文关键字:数组 返回 填充 任何值 列表 获取      更新时间:2023-10-16

对于我的算法类中的一个项目,我假设从。txt文件中读取迪斯尼地图中的所有点,然后使用prims算法来解决MST问题。

我的问题是,我解析的值从文件到一个临时数组使用' '分隔符,然后把它们推入一个列表。一切都工作得很好,直到将数组压入列表,然后在程序稍后接收值时,它不返回任何值。我知道这有点傻,但希望你们都能帮忙。

my code: 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

我知道这事很傻,但我就是一时想不出来。

编辑:

现在我得到了jibberish,因为程序正在读取文件末尾的空白行,我不想被读取,因为没有值jibberish:id: 84�������1222����422����TomorrowlandTerrace�����ӿ�����

更新了导致错误的部分代码:

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;

不使用指针编程更容易。