Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 矢量推回不';行不通_C++ - Fatal编程技术网

C++ 矢量推回不';行不通

C++ 矢量推回不';行不通,c++,C++,我创建了一个非常简单的代码,但是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

我创建了一个非常简单的代码,但是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] << " ";
}
结果应该是“窗户轮胎给扬声器”,但对我来说是“扬声器”。
有什么问题

这被证明是根本问题:


您是否尝试过转储输入文件(例如使用
hextump-C
或类似工具)以检查恶意控制序列,如
\r
,这可能解释您看到的行为


您的输入文件可能是来自DOS/Windows类系统的文本文件,您可能正在使用Unix类系统。

您确定读取的文件正确吗?您是否尝试过转储输入文件(例如,使用
hextdump-C
或类似工具)检查恶意控制序列,如
\r
,这可能解释您看到的行为。(您的输入文件可能是来自类似DOS/Windows系统的文本文件,您可能正在使用类似unix的系统。)是的,我是。我试图编辑它,但我总是得到最后一个字。请检查
std::getline(infle,temp,“\r”)
非常感谢:“\r”导致了问题。
window
tyre
give
speaker