0

ファイルから 2D ベクトルへの読み込みで問題が発生しています。私は c++ が初めてで、コードに問題が見られません。

ファイルはこのパターンに従います。

5
0 0 0 0 1
0 0 0 1 0
0 0 0 1 1
0 0 1 0 0
...

それを読むための私のアルゴリズム...

void Similarity::readData(Scanner& inStream){
        dataLength = inStream.nextInt();
        while(inStream.hasNext()){
                vector<int> temp;
                int tempInt = 0;
                for(int i = 0; i < dataLength; ++i){
                        tempInt = inStream.nextInt();
                        temp.push_back(tempInt);
                        temp.clear();
                }
                theData.push_back(temp);
                theData.clear();
        }
}

そして、それを印刷する私のアルゴリズム。

string Similarity::toString(){
        string result = "";
        for(int i = 0; i < theData.size(); ++i){
                for(int j = 0; j < dataLength; ++j){
                        result += convertInt(theData[i][j]);
                }
                result += "\n";
        }
        return result;
}

string Similarity::convertInt(int number){
        stringstream s;
        s << number;
        return s.str();
}

toString からの出力はありません。作業が必要なのは readData ですか、それとも toString ですか?

ありがとうございました。

4

1 に答える 1

4

コードのこのセクションは無意味です (少なくとも、ストリームから数値を消費するのを避けるため):

                    tempInt = inStream.nextInt();
                    temp.push_back(tempInt);
                    temp.clear();

push_back temp.clear()() で挿入されたオブジェクトをすぐに削除するためです。

これも同じ

            theData.push_back(temp);
            theData.clear();

temp.clear()私はあなたが今持っている場所が必要だと思うtheData.clear().

于 2013-09-12T20:50:31.387 に答える