当从列表中获取值时,我填充其中的数组不返回任何值
C++, When getting values from a list that I populate the arrays inside are returning nothing
对于我的算法类中的一个项目,我假设从。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;
不使用指针编程更容易。
相关文章:
- 为什么 C++ 中的字符数组返回其项目而不是地址?
- C++ bool 数组返回 1 或 0 以外的值
- C++ ostream 从对象数组返回数据
- 类中的重载运算符 [],因此它可以从模板类中的数组返回对象
- 指向对象的指针数组返回jibberish
- 具有基本结构的结构数组返回错误的答案
- 数组返回乐趣 movie_check()
- C++从字符数组返回类型为T的指针
- C++-将数组返回到主函数,并将数组传递给另一个函数
- C++:如何通过异常处理从越界数组返回值
- 整数数组返回类型
- 从字符串流复制到 char * 数组返回为函数参数
- C++函数从对象数组返回对象的副本.这是默认行为吗?
- 8字节数组返回长(C#到C++)
- 向c#数组返回一个字符数组c++
- 从并行数组返回值,并在以后的函数中使用这些值
- 将c++中的浮点数数组返回给Python
- 如何在gsoap中使字符串数组返回值
- 将两个字符相乘并向数组返回int
- C++套接字 - 从结构数组返回单个地址信息