从文件中读取数据

use*_*567 11 c++ file

我有.txt文件,其中包含如下数据[12,25],[36,45],即数字用方括号括起来,用逗号分隔,我想通过C++程序读取这样的文件

我提到了字符串工具包可用,特别是regex工具可以使用,但我不能放入程序可以有人请帮帮我?

BLU*_*IXY -3

#include <iostream>
#include <iterator>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

bool isSeparator(const char c){
    return c == '[' || c == ']' || c == ',';
}

int main(){
    const char filename[] = "data.txt";

    ifstream fin(filename);
    vector<int> v;
    string buff;
    while(getline(fin, buff)){
        replace_if(buff.begin(), buff.end(), isSeparator, ' ');
        istringstream sin(buff);
        for(int n;sin >> n;){
            v.push_back(n);
        }
    }
    copy(v.begin(), v.end(), ostream_iterator<int>(cout,"\n"));
    //for(int i=0;i<v.size();++i) cout << v[i] << endl;
}
Run Code Online (Sandbox Code Playgroud)