0

私のアルゴリズム クラスのプロジェクトでは、ディズニーランド マップのすべてのポイントを .txt ファイルから読み取り、プリム アルゴリズムを使用して MST 問題を解決するとします。

私の問題は、ファイルの値を ' ' 区切り文字を使用して一時配列に解析し、それらをリストにプッシュすることです。配列をリストにプッシュするまで、すべてがうまく機能しており、プログラムの後半で値を受け取ったときに値が返されません。私はそれがばかげていることを知っていますが、うまくいけば皆さんが助けてくれます.

私のコード: http://pastebin.com/rS6VJ6iJ

ディズニーランド.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

これがばかげていることはわかっていますが、現時点では理解できません。

編集:

プログラムがファイルの最後に空白行を読み込んでいるため、今はおかしくなっています。 ����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;
//---------------------
4

1 に答える 1

0

問題は、リストに追加したアレイを削除していることです

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;

ポインタを使わない方がプログラミングは簡単です。

于 2013-04-28T20:55:29.063 に答える