dimanche 30 septembre 2018

Delete first column of 2d vector matrix read in using sstream

I have a 2d vector where the rows are unequal. I have been trying to delete the first column but have no luck look at previous stackoverflow posts.

example data:

1 2 4 5 6 6
1 2 3 4 6 6 8

Code to read in data:

myfile.open("test.txt");
if(myfile.is_open())
{
    while(getline(myfile, line)){
        //cout << "This line: ";
        istringstream is(line);
        sortVec.push_back(std::vector<int>( std::istream_iterator<int>(is),
                                                std::istream_iterator<int>() ) );
    }
}
else
{
    cout << "Myfile is not open" << endl;
}

    myfile.close();

When I try to erase the first column using std:vector:

int columnIndex = 0;
    for(auto& row:sortVec){
        row.erase(next(row.begin(), columnIndex));
    }

I get a segmentation fault.

I have tried the following stackoverflow posts as well. How to delete column in 2d vector, c++

Additionally when I create a vector manually everything works perfect so I am lost at the moment.

Desired output:

2 4 5 6 6
2 3 4 6 6 8

Aucun commentaire:

Enregistrer un commentaire