It is necessary with the help of the constructor with the parameter to copy the vector. Throws a segmentation fault . Help me to understand.

class matrix{ private: int size; vector<vector<double> > mat; public: ..... matrix(vector<vector <double> > matr, int s){ size = s; mat.resize(size); for(int n=0;n<size;n++){ mat[n].resize(size); } for(int i=0;i<size;i++){ for(int j=0;j<size;j++){ mat[i].push_back(matr[i][j]); } } } ..... 

    3 answers 3

    First of all, if you have already changed the size, then why add elements, and not overwrite?

    Secondly, it is better to transfer large objects by reference, and not by value.

    Thirdly, I do not understand - do you need to transfer only a part of the matr matrix to a matr ? Simply if completely - then the easiest

     matrix(const vector<vector<double>>& matr) :mat(matr){} 

    Update

    But how to display -

     for(auto row: mat) { for(auto x: row) cout << x << " "; cout << endl; } 

    Well, or if you really want with indexes -

     for(size_t i = 0; i < mat.size(); ++i) { for(size_t j = 0; j < mat[i].size(); ++j) cout << mat[i][j] << " "; cout << endl; } 

    And no s - the size of the vector is stored in the vector itself.

    • matrix(const vector<vector <double> > &matr, int s):mat(matr){ size = s; for(int i=0;i<size;i++){ for(int j=0;j<size;j++){ cout<<mat[i][j]<<" "; } cout<<"\n"; } } matrix(const vector<vector <double> > &matr, int s):mat(matr){ size = s; for(int i=0;i<size;i++){ for(int j=0;j<size;j++){ cout<<mat[i][j]<<" "; } cout<<"\n"; } } - Vlad Lesnoy
    • I reissued the code but the problem arises when it is displayed on the screen - Vlad Lesnoy
    • What is your parameter s - what does it contain? The feeling that you are beyond the bounds of the matrix. - Harry
    • s is the size of the matrix - Vlad Lesnoy
    • You do not need him! See amended answer. - Harry
    1. Check that s <= matr.size() and s <= mart[i].size()
    2. Doing first mat[n].resize(size); , and then below the mat[i].push_back(...) вы получаете матруци назмером s*2s. Замените последний code mat[i].push_back(...) вы получаете матруци назмером s*2s. Замените последний mat[i].push_back(...) вы получаете матруци назмером s*2s. Замените последний push_back на mat [i] [j] = matr [i] [j] `
    • Replaced, the exact same situation occurred - Vlad Lesnoy
    • @ Vladlesnoy Regular magic ball passed into polishing, so I can not guess on which line and what message the debager shows when it falls, but I suspect that the problem is not in this section of the code. It is better to post a more complete version here. - ffk

    Solved a problem. The fact is that the reserve () function does not create the size of the array. And for this, the vector.size () function returns 0. Out of the situation by creating with resize ()

    • one
      Stop stop stop! Where is your initial reserve question? If there was a reserve , I would immediately tell you that you can not do this. But you used resize ! I also wrote in the answer - " if you have already changed the size " - i.e. used resize . - Harry
    • The problem was not quite in function. And in the matrix of the short, I passed to the function. There it was a reserve - Vlad Lesnoy