dimanche 8 novembre 2020

Read a 2d array from a text file into a container vector with correct format

file.txt

13 14 15
10 17 152
1  34 56

vector<vector<int>> v;     //2d vector
ifstream file("file.txt");
int num;
int counter = 0;
if (file.is_open()){
    while(file >> num)){
        v.push_back(num);

}
}
copy(begin(v), end(v),ostream_iterator<num>(cout, " "));

I want to read the 2d array given in the text file into a 2d container vector in the correct format. My current code only prints the text file into console like this: 1314151017.... I want to store the elements in the same format as in the text file into a 2d vector so that i can use this vector later in the code. How do i go about doing this?

Aucun commentaire:

Enregistrer un commentaire