dimanche 24 avril 2016

C++ regex parsing numbers out of string

I am trying to extract all the numbers from a string in the same row format. The following code extracts the numbers, but does not record the last row of 4s. How should I change my regex to get repeated row of numbers?

    #include <iostream>
#include <vector>
#include <string>
#include <regex>
using namespace std;

const string data  ="4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,"
"4,8,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,7,8,3,3,3,3,3,3,7,8,3,3,3,3,3,3,7,4,"
"4,6,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,5,6,1,1,1,1,1,1,5,6,1,1,1,1,1,1,5,4,"
"4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4";
int main() {
    regex e("[1-9]");
    sregex_token_iterator rit(data.begin(), data.end(), e);
    sregex_token_iterator rend;
    int i = 0;
    std::vector<std::vector<int>> double_array;
    double_array.reserve(4);
    std::vector<int> row;
    row.reserve(40);
    while(rit != rend)
    {
        if( i ==  40)
        {
            double_array.push_back(row);
            row.clear();
            row.reserve(40);
            i = 0;
        }
        row.push_back(stoi(rit->str()));
        ++i;
        ++rit;
    }

    for(auto & disp_row : double_array)
    {
        for(auto & c : disp_row)
        {
            cout<< c << " ";
        }
        cout << "\n";
    }
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire