【问题标题】:Reading lines from a file using std:: istream_iterator. Who? [duplicate]使用 std::istream_iterator 从文件中读取行。谁? [复制]
【发布时间】:2010-05-17 15:56:24
【问题描述】:

可能重复:
How do I iterate over cin line by line in C++?

我需要从文件中读取所有行:

std::ifstream file("...");
std::vector<std::string> svec(
   (std::istream_iterator<std::string>(file)),
   (std::istream_iterator<std::string>()),
);

但它被视为文字。

【问题讨论】:

标签: c++ stl std


【解决方案1】:

我认为问题在于std::string 的输入法将读取直到找到空格字符,然后终止。

您是否尝试过在循环中使用std::getline

查看C++ FAQ

【讨论】:

  • 感谢您的回复。我知道 std::getline()。但这个问题更多是出于好奇。那么,有什么方法不能用迭代器逐行读取文件?
猜你喜欢
  • 1970-01-01
  • 2019-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-06
  • 1970-01-01
相关资源
最近更新 更多