People help please!

There is a two-dimensional vector described in class. Cited only 2 methods of all, the extra did not write. Saving the matrix to the file works, but I tried to do the same way to load the matrix from the file into the program - it does not work! In the file, the matrix is ​​of unknown size, but always rectangular.

typedef vector<vector<int>>::iterator matrix_iterator; class Matrix { public: vector<vector<int>>v; Matrix(); void SaveMatrix(int err); void LoadMatrix(); }; // если err=0 матрица не записывается, // err используется, чтоб не было бесконечной рекурсии void Matrix::SaveMatrix(int err) { if (!FileExists(FNAME_MATRIX) || err != 0) { ofstream ofst(FNAME_MATRIX); for (matrix_iterator it = v.begin(); it != v.end(); ++it) { copy(it->begin(), it->end(), ostream_iterator <int>(ofst, " ")); ofst <<endl; } cout <<"Матрица сохранена\n"; } else { char ch; cout << "Вы уверены, что хотите перезаписать матрицу? + или -\n"; cin>>ch; if (ch == '+') SaveMatrix(1); else cout <<"Матрица не сохранена\n"; } } void Matrix::LoadMatrix() { ifstream ifst(FNAME_MATRIX); while (!ifst.eof()) { int temp; ifst>>temp; matrix_iterator iterlvl2; vector<int>::iterator iterlvl1; for (iterlvl2 = v.begin(); iterlvl2 != v.end(); iterlvl2++) { iterlvl1 = (*iterlvl2).begin(); (*iterlvl2).push_back(temp); } } } 
  • @Tiva, To format a code, select it with the mouse and click on the {} button of the editor. - Nicolas Chabanovsky

1 answer 1

  1. Correct the vector<vector<int>>v; on vector<vector<int> >v; This is a common mistake of using templates (template magic is not just so named).
  2. When you use an increment over an iterator, you do not need to use the postfix form since an extra temporary copy is created, use the prefix one.
  3. I may not understand something, but in the function of loading from a file you do not understand what :-) the simplest solution is to read symbolically and if a space is found (or whatever you use to separate characters, you are not talking about the storage format they said) to do push_back in the nested vector, and if a line feed character is detected, go to the next element of the upper vector, so to speak.

PS: no one will write the code for you) and this is a bad tone)

  • Most importantly, the file format is unknown. - avp
  • By the way, the problem >> in C ++ 11 was solved. Now you can write in the templates. - skegg
  • Of course, thanks for the advice, especially about the increment, because of this, I always had an extra element. but, I’ve heard a lot of “algorithms in Russian” in different forums on the 1st course, and they give us crazy labs, and I can’t write this code myself, but don’t explain, because I don’t understand these iterators at all .. help me please - Tiva
  • @Tiva, this is of course a great pity that you are being taught this way (right away with C ++). But in any language, in order to read a rectangular matrix from a file, you need to know its size in the file (the number of rows and columns). In principle, knowing the file format , this information can be obtained by analyzing its contents (different delimiters, etc.) you write that you saved it to a file and everything worked out for you. So think about how to pull out information about the number of rows and columns from this output (we don’t see this file). On the order of the elements: I think that you first go all the columns of the first row, then the second, etc. - avp
  • one
    Think up the file format you yourself and save the matrix to files. Well, or write down the dimensions of the matrix at the beginning of the file. - Roman Goriachevskiy