C++ 读取矩阵中可变大小的列

C++ 读取矩阵中可变大小的列,c++,fstream,getline,C++,Fstream,Getline,我有一个文件,在不同的行中包含不同的列。比如说 10 20 30 60 60 20 90 100 40 80 20 50 60 30 90 .... 我想读每行的最后三个数字。因此,输出将是 20 30 60 100 40 80 60 30 90 我不能使用以下结构,因为每行的大小可变 结构1: std::ifstream fin ("data.txt"); while (fin >> a >> b >> c) {...} 结构2: string l

我有一个文件,在不同的行中包含不同的列。比如说

10 20 30 60 
60 20 90 100 40 80
20 50 60 30 90
....
我想读每行的最后三个数字。因此,输出将是

20 30 60 
100 40 80
60 30 90
我不能使用以下结构,因为每行的大小可变

结构1:

std::ifstream fin ("data.txt");
while (fin >> a >> b >> c) {...}
结构2:

string line;
stringstream ss;
getline(fin, line);
ss << line;
int x, y, z;
ss >> x >> y >> z >> line;
字符串行;
细流ss;
getline(fin,line);
ss>x>>y>>z>>线;

那我该怎么办呢?

把它们读入一个
std::vector
并删掉除最后3项以外的所有内容

std::string line;
while (getline(fin, line))
{
    std::vector<int> vLine;
    istringstream iss(line);
    std::copy(std::istream_iterator<int>(iss), std::istream_iterator<int>(), std::back_inserter(vLine));
    if (vLine.size() > 3)
    {
        vLine.erase(vLine.begin(), vLine.begin() + (vLine.size() - 3));
    }
    // store vLine somewhere useful
}
std::字符串行;
while(getline(fin,line))
{
std::矢量线;
istringstream iss(线);
std::copy(std::istream_迭代器(iss),std::istream_迭代器(),std::back_插入器(vLine));
如果(vLine.size()>3)
{
擦除(vLine.begin(),vLine.begin()+(vLine.size()-3));
}
//把线存储在有用的地方
}

我必须首先标记行,问题是,我不知道每行中的确切标记数