簡體   English   中英

C ++程序無法正確打印二維矢量網格,但已關閉

[英]c++ program not printing 2d vector grid properly but is close

我目前正在嘗試將數字文本文件解析為2d向量,稍后將對其進行修改,但是到目前為止我的代碼都是如此。 我得到這個:

845630179
032918654
190745328
683074912
457201836
219863540
361429705
074186093
074186093�845630179

一切正常,除了重復第9行並在末尾放置垃圾。

如果我在文本的末尾輸入一個回車,它將輸出以下內容:

845630179
032918654
190745328
683074912
457201836
219863540
361429705
074186093
9203574619

(第9行中的第10個元素不應存在)

供參考,以下是文本文件的外觀:

845630179
032918654
190745328
683074912
457201836
219863540
361429705
074186093
920357461

到目前為止,這是我的代碼:

int main(int argc, char* argv[]) {
//parsing the textfile.
vector<vector<char>> grid;
fstream fin; char ch;
string name (argv[1]); //File Name.
// 2D Vector.
vector<char> temp;
// Temporary vector to be pushed 
// into vec, since its a vector of vectors.
fin.open(name.c_str(),ios::in);
// Assume name as an arbitary file.
while(fin)
{
    ch = fin.get();
    if(ch!='\n') {
        temp.push_back(ch);
        cout << ch;
    }
    else 
    { 
        grid.push_back(temp); 
        temp.clear(); 
        cout << ch;
    }
}
for (int i = 0; i < grid.size();i++) {
    for (int j = 0; j < grid[i].size();j++) {
        cout << grid[i][j];
    }
}
}

您的代碼對我來說效果很好。

如果文本文件的末尾有一個回車,它將最后一個數字推送到網格中。 否則,您需要在while循環之后將temp的內容推送到grid

一種快速修復解決了大部分問題的方法:

int main()
{
//parsing the textfile.
    vector<vector<char>> grid;
    fstream fin;
    char ch;

    // Hardcoded value. Typing the same thing in over and over while debugging is for suckers.
    string name("data.txt"); //File Name. 
// 2D Vector.
    vector<char> temp;
// Temporary vector to be pushed
// into vec, since its a vector of vectors.
    fin.open(name.c_str(), ios::in);
// Assume name as an arbitary file.
    while (fin.get(ch)) // the change: Slightly different get and getting in the loop
                        // condition. If nothing is gotten the loop doesn't enter
                        // solves almost all of the problems.
    {
        if (ch != '\n')
        {
            temp.push_back(ch);
            cout << ch;
        }
        else
        {
            grid.push_back(temp);
            temp.clear();
            cout << ch; 
        }
    }
    for (int i = 0; i < grid.size(); i++)
    {
        for (int j = 0; j < grid[i].size(); j++)
        {
            cout << grid[i][j];
        }
    }
}

產量

845630179
032918654
190745328
683074912
457201836
219863540
361429705
074186093
920357461845630179032918654190745328683074912457201836219863540361429705074186093

剩下的就是垃圾了。 那不是垃圾。 那就是您的實際輸出。 一切由

845630179
032918654
190745328
683074912
457201836
219863540
361429705
074186093
920357461

cout << ch;的結果cout << ch; 在進行收集的while循環中。

845630179032918654190745328683074912457201836219863540361429705074186093

最后是for循環。 由於未存儲換行符,因此將其打印為一大塊數字。

為了解決這個問題,我們將從while循環中刪除輸出,並在for循環中恢復換行符。

int main()
{
//parsing the textfile.
    vector<vector<char>> grid;
    fstream fin;
    char ch;
    string name("data.txt"); //File Name.
// 2D Vector.
    vector<char> temp;
// Temporary vector to be pushed
// into vec, since its a vector of vectors.
    fin.open(name.c_str(), ios::in);
// Assume name as an arbitary file.
    while (fin.get(ch))
    {
        if (ch != '\n')
        {
            temp.push_back(ch);
        }
        else
        {
            grid.push_back(temp);
            temp.clear();
        }
    }
    for (int i = 0; i < grid.size(); i++)
    {
        for (int j = 0; j < grid[i].size(); j++)
        {
            cout << grid[i][j];
        }
        cout << '\n'; // 
    }
}

如果我們使用vector<string> ,大約有四分之一的代碼會消失。

 int main()
{
//parsing the textfile.
    vector<string> grid;
    fstream fin;
// 2D Vector.
    vector<char> temp;
// Temporary vector to be pushed
// into vec, since its a vector of vectors.
    fin.open("data.txt", ios::in);
// Assume name as an arbitary file.
    string line;
    while (getline (fin, line))
    {
        grid.push_back(line);
        temp.clear();
    }
    for (int i = 0; i < grid.size(); i++)
    {
        for (int j = 0; j < grid[i].size(); j++)
        {
            cout << grid[i][j];
        }
        cout << '\n'; // getline ate the newline. Have to put it back
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM