簡體   English   中英

為什么不通過std :: cin復制並粘貼到控制台中產生與從std :: ifstream讀取相同的結果?

[英]Why doesnt copy and paste into console reading through std::cin produce same results as reading from std::ifstream?

我在Linux上使用Eclipse IDE。 我的問題是,當我將輸入文件復制並粘貼到控制台中時,使用std::cin;時會得到不正確的答案std::cin; 使用std::cin復制並粘貼對於較小的示例效果很好,但是對於較大的示例卻失敗了。

為什么通過std::cin讀取的復制和粘貼不起作用? 我將如何通過std::cin獲得復制並粘貼使其正常工作?

當我打開並通過std::ifstream讀取輸入文件時,一切正常。

這是我遇到的舊的Google代碼卡紙引起的問題。 https://code.google.com/codejam/contest/351101/dashboard#s=p0

這是兩者共同的:

#include <iostream>
#include <ios>
#include <string>
#include <vector>
#include <sstream>
#include <fstream>
#include <limits>

class test {
public:
    int credit;
    int numListItems;
    std::vector<int> itemList;
    int result[2];

    test(int c, int i, std::string s)
    {
        std::istringstream iss(s);
        int temp;
        credit = c;
        numListItems = i;
        itemList.clear();
        while (!iss.eof())
        {
            iss >> temp;
            itemList.push_back(temp);
        }

    }

    void process()
    {
        for (int i = 0; i < this->numListItems; ++i)
            for (int j = i+1; j < this->numListItems; ++j)
            {
                if ((this->itemList[i] + this->itemList[j]) == this->credit)
                {
                    this->result[0]=i;
                    this->result[1]=j;
                    i=this->numListItems+1;
                    break;
                }
            }
    }

private:
};

std::ostream& operator<<(std::ostream& os, const test& obj)
{
    os << obj.result[0]+1 << " " << obj.result[1]+1;
    return os;
}

這是我的主要std::cin版本,當我復制並粘貼小輸入時可以工作,但是在我復制並粘貼大輸入時失敗:

int main(int argc, char* argv[]) {

    std::vector<test> testArr;
    std::string listLine, tempStr;
    int numCases,c,nItems;

    std::cin >> numCases;
    for (int i = 0; i < numCases; ++i)
    {
        std::cin >> c;
        std::cin >> nItems;
        listLine="";
        for (int j =0; j < nItems; j++)
        {
            std::cin >> tempStr;
            if (j < (nItems-1))
                listLine = listLine + tempStr + " ";
            else
            {
                listLine = listLine +tempStr;
            }
        }
        testArr.push_back(test(c,nItems,listLine));
    }

    for (int i = 0; i < numCases; ++i)
    {
        testArr[i].process();
        std::cout << "Case #" << i+1 << ": "
                    << testArr[i] << std::endl;
    }

    for (int i = 0; i < numCases; ++i)
    {
        delete &testArr[i];
    }

    return 0;
}

這是我從剪切和粘貼中獲得的不正確的大輸出:

Case #1: 2 3
Case #2: 1 4
Case #3: 4 5
Case #4: 29 46
Case #5: 11 56
Case #6: 84 240
Case #7: 413 584
Case #8: 28 80
Case #9: 381 634
Case #10: 17 18
Case #11: 8 447
Case #12: 402 619
Case #13: 43 61
Case #14: 2 27
Case #15: 18 69
Case #16: 3 85
Case #17: 7 173
Case #18: 4 555
Case #19: 4 476
Case #20: 9 303
Case #21: 5 70
Case #22: 16 869
Case #23: 3 1
Case #24: 3 1
Case #25: 156 327
Case #26: 3 198
Case #27: 1 303
Case #28: 24 36
Case #29: 1 79
Case #30: 1 356
Case #31: 1 3
Case #32: 4 319
Case #33: 10 41
Case #34: 16 335
Case #35: 8 205
Case #36: 98 314
Case #37: 28 57
Case #38: 1 396
Case #39: 12 30
Case #40: 1 57
Case #41: 15 75
Case #42: 33 57
Case #43: 3 1
Case #44: 2 152
Case #45: 9 68
Case #46: 8 122
Case #47: 17 48
Case #48: 7 11
Case #49: 1 76
Case #50: 27 278

這是一個可以直接讀取輸入文件而不是剪切和粘貼的版本:

int main(int argc, char* argv[]) {

    std::vector<test> testArr;
    std::string listLine, tempStr;
    int numCases, c, nItems;

    std::ifstream myFile("./A-large-practice.in");

    if (myFile.is_open())
    {
        myFile >> numCases;
        for (int i = 0; i < numCases; ++i)
        {
            myFile >> c;
            myFile >> nItems;

            listLine="";
            for (int j =0; j < nItems; j++)
            {
                myFile >> tempStr;
                if (j < (nItems-1))
                    listLine = listLine + tempStr + " ";
                else
                    listLine = listLine +tempStr;

            }
            testArr.push_back(test(c,nItems,listLine));
        }
        myFile.close();
    }
    else
    {
        std::cout << "unable to open file!" << std::endl;
    }


    for (int i = 0; i < numCases; ++i) {
        testArr[i].process();
        std::cout << "Case #" << i+1 << ": "
                    << testArr[i] << std::endl;
    }

    return 0;
}

在控制台中復制/粘貼文本與程序無關,這是由外殼程序處理的,它將把粘貼的數據傳遞給程序(因此,鍵入或粘貼文本與程序所涉及的相同) )。


但是,您的代碼存在問題。

  • 首先,它具有未定義的行為 :當您delete &testArr[i]; ,您在堆棧分配的對象上調用delete ,這是不允許的(也是不必要的)。 您應該只對new ed對象調用delete

  • 其次,在您的test構造函數中, 您不應該在while (!iss.eof())時進行讀取,您可能正在為上一次迭代讀取垃圾。

你應該做 :

while (iss >> temp)
{
    itemList.push_back(temp);
} 

暫無
暫無

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

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