C++ 将std::ifstream读取为行向量

C++ 将std::ifstream读取为行向量,c++,file-io,C++,File Io,我如何读取一个文件,其中每一行都是一个数字,然后将该数字输出到一个行向量中 例如:file.txt包含: 314 159 265 123 456 我尝试过这种实现: vector<int> ifstream_lines(ifstream& fs) { vector<int> out; int temp; getline(fs,temp); while (!fs.eof()) { out.push_back(temp

我如何读取一个文件,其中每一行都是一个数字,然后将该数字输出到一个行向量中

例如:file.txt包含:

314
159
265
123
456
我尝试过这种实现:

vector<int> ifstream_lines(ifstream& fs) {
    vector<int> out;
    int temp;
    getline(fs,temp);
    while (!fs.eof()) {
        out.push_back(temp);
        getline(fs,temp);
    }
    fs.seekg(0,ios::beg);
    fs.clear();
    return out;
}
矢量ifstream\u线(ifstream&fs){
矢量输出;
内部温度;
getline(fs,temp);
而(!fs.eof()){
向外。推回(温度);
getline(fs,temp);
}
fs.seekg(0,ios::beg);
fs.clear();
返回;
}
但当我尝试编译时,会出现如下错误:

error C2784: 'std::basic_istream<_Elem,_Traits> &std::getline
(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : 
could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::ifstream'
错误C2784:'std::basic\u istream&std::getline
(std::basic_istream&,std::basic_string&)':
无法从“std::ifstream”推断“std::basic_istream&”的模板参数
所以,很明显,有些地方出了问题。有没有比我正在尝试的更优雅的解决方案?(假设Boost等第三方库不可用)


谢谢

我怀疑你想要这样的东西:

#include <vector>
#include <fstream>
#include <iterator>

std::vector<int> out;

std::ifstream fs("file.txt");

std::copy(
    std::istream_iterator<int>(fs), 
    std::istream_iterator<int>(), 
    std::back_inserter(out));
#包括
#包括
#包括
std::向量输出;
std::ifstream fs(“file.txt”);
复制(
std::istream_迭代器(fs),
std::istream_迭代器(),
标准:背向插入器(输出);
std::getline(stream,var)
读入var的std::string。我建议改用stream操作符读入int,必要时检查错误:

vector<int> ifstream_lines(ifstream& fs) {
  vector<int> out;
  int temp;
  while (!(fs >> temp).fail()) {
    out.push_back(temp);
  }
  fs.seekg(0,ios::beg);
  fs.clear();
  return out;
}
矢量ifstream\u线(ifstream&fs){
矢量输出;
内部温度;
而(!(fs>>temp).fail(){
向外。推回(温度);
}
fs.seekg(0,ios::beg);
fs.clear();
返回;
}

Tim Sylvester所描述的标准迭代器是最好的答案

但是如果您想要手动循环,
只是提供一个反例:“jamuraa”

vector<int> ifstream_lines(ifstream& fs)
{
    vector<int> out;
    int temp;

    while(fs >> temp)
    {
        // Loop only entered if the fs >> temp succeeded.
        // That means when you hit eof the loop is not entered.
        //
        // Why this works:
        // The result of the >> is an 'ifstream'. When an 'ifstream'
        // is used in a boolean context it is converted into a type
        // that is usable in a bool context by calling good() and returning
        // somthing that is equivalent to true if it works or somthing that 
        // is equivalent to false if it fails.
        //
        out.push_back(temp);
    }
    return out;
}
矢量ifstream\u线(ifstream&fs)
{
矢量输出;
内部温度;
而(fs>>温度)
{
//循环仅在fs>>临时成功时输入。
//这意味着当你点击eof时,循环不会被输入。
//
//为什么会这样:
//>>的结果是一个“ifstream”。当一个“ifstream”
//在布尔上下文中使用,并将其转换为类型
//通过调用good()并返回
//如果它起作用,那么它等于真,或者
//如果失败,则相当于false。
//
向外。推回(温度);
}
返回;
}

那太可怕了。切勿在该条件下测试eof()。最好在条件中测试操作。您应该注意,它不会检测到输入文件中的错误(即同一行上的两个数字)。流迭代器将从一行中读取多个值,以空格分隔,但如果遇到数字或空格以外的内容,它将停止,将流保留为
fs.eof()==false
fs.fail()==true