簡體   English   中英

矢量push_back不起作用

[英]vector push_back doesn't work

我創建了一個非常簡單的代碼,但是push_back函數不想工作。 它給我的結果與預期完全不同。

這是代碼:

std::vector<std::string> words;
std::ifstream infile ("words.txt");
std::string temp;
while (std::getline(infile, temp))
{
    words.push_back(temp);
}
for (std::size_t i = 0; i < words.size(); i++)
{
    std::cout << words[i] << " ";
}

“ words.txt”文件僅包含4個單詞:

window
tyre
give
speaker

結果應該是“窗口輪胎給揚聲器”,但對我來說是“揚聲器”。 問題是什么?

事實證明這是潛在的問題:

您是否嘗試過轉儲輸入文件(例如使用hexdump -C或類似文件)以檢查流氓控制序列(例如\\r ,這可能解釋了您所看到的行為。

您的輸入文件可能是類似DOS / Windows的系統中的文本文件,並且您可能正在使用類似Unix的系統。

暫無
暫無

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

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