vendredi 9 octobre 2020

How to populate a vector of vector of set from file?

I have a 3x3 matrix into my txt file with some numbers and some zeroes like this

0 0 3
1 0 0
0 2 0

I want to read it as a vector of vectors of sets. And where I have zeroes I want to populate that cell with numbers 123. Like

1231233
1123123
1232123

I would like to know if I print it correctly.

I've written this code:

class Matrix {
  private:
    vector<vector<set<int> > > cell;
  public:
    Matrix(const string& filename)  {
        ifstream file("matrix.txt");
        if (!file.good())   {
                cerr<<"Error";
            }

        int x;
        int y;
        for(x=0; x<3; x++)  {
            for(y=0; y<3; y++)  {
              for(vector<vector<set<int> > >::iterator it = cell.begin(); it != cell.end(); it++)   {
                if(file != 0)   {
                 cell[x][y] = file; // Don't think this part of code is good because I get error here
                }   else    {
                   int z;
                   for(z = 1; z<4; z++) {
                     cell[x][y].insert(z);
                     cout<<cell[x][y];  // Of course I get an error here too.       
                        }
                    }
                }
            }
        }
    }
}

Is that iteration of useful for somewhat? I'm not using "it" anywhere but I think that into a set I have to use it.

Of course something is not correct or is missing but I'm not understanding what and why. I have to use vectors and sets.

I would prefer printing it to know if I do it well.

Edit Trying to write file>> cell[x][y]; gives me this error: [Error] cannot bind 'std::basic_istream' lvalue to 'std::basic_istream&&'.

Aucun commentaire:

Enregistrer un commentaire