There is a code:

int g[500][500], n; cin >> n; for(int i=0; i<n; i++) for(int j=0; j<n; j++) cin >> g[i][j]; 

How do I replace a static array with a vector?

Those. vector <vector <int>> g(n);

How to carry out data input from the keyboard into a vector (two-dimensional)?

PS This is for graphs.

    3 answers 3

    Instead of int g[500][500] -

     vector<vector<int>> g(500,vector<int>(500)); 

    But better this way:

     int main() { int n; cin >> n; vector<vector<int>> g(n,vector<int>(n)); ... } 
       cin >> n; vector <vector <int>> g(n, vector<int>(n)); 

      And no further.

        // And just like that, for example:

        vector <int> g;

        int i, j, n, x;

        cin >> n;

         for(int i=0; i<n; i++) { for(int j=0; j<n; j++) { cin >> x; g.push_back(x); } }