Multi-dimensional vector

后端 未结 8 2077
一个人的身影
一个人的身影 2020-11-28 09:32

How can I create a 2D vector? I know that in 2D array, I can express it like:

a[0][1]=98;
a[0][2]=95;
a[0][3]=99;
a[0][4]=910;

a[1][0]=98;
a[1][1]=989;
a[1]         


        
相关标签:
8条回答
  • 2020-11-28 09:43
    std::vector< std::vector< int > > a; // as Ari pointed
    

    Using this for a growing matrix can become complex, as the system will not guarantee that all internal vectors are of the same size. Whenever you grow on the second dimension you will have to explicitly grow all vectors.

    // grow twice in the first dimension
    a.push_back( vector<int>() );
    a.push_back( vector<int>() );
    
    a[0].push_back( 5 ); // a[0].size() == 1, a[1].size()==0
    

    If that is fine with you (it is not really a matrix but a vector of vectors), you should be fine. Else you will need to put extra care to keep the second dimension stable across all the vectors.

    If you are planing on a fixed size matrix, then you should consider encapsulating in a class and overriding operator() instead of providing the double array syntax. Read the C++ FAQ regarding this here

    0 讨论(0)
  • 2020-11-28 09:43

    Declaration of a matrix, for example, with 5 rows and 3 columns:

    vector<vector<int> > new_matrix(5,vector<int>(3));
    

    Another way of declaration to get the same result as above:

    vector<int> Row;    
    

    One row of the matrix:

    vector<Row> My_matrix;
    

    My_matrix is a vector of rows:

    My_matrix new_matrix(5,Row(3)); 
    
    0 讨论(0)
  • 2020-11-28 09:50

    dribeas' suggestion is really the way to go.

    Just to give a reason why you might want to go the operator() route, consider that for instance if your data is sparse you can lay it out differently to save space internally and operator() hides that internal implementation issue from your end user giving you better encapsulation and allowing you to make space or speed improving changes to the internal layout later on without breaking your interface.

    0 讨论(0)
  • 2020-11-28 09:50

    As Ari pointed, vector< vector< int>> is the right way to do it.

    In addition to that, in such cases I always consider wrapping the inner vector (actually, whatever it represents) in a class, because complex STL structures tend to become clumsy and confusing.

    0 讨论(0)
  • 2020-11-28 09:54

    Just use the following method to use 2-D vector.

    int rows, columns;        
    
    // . . .
    
    vector < vector < int > > Matrix(rows, vector< int >(columns,0));
    
                                      Or
    
    vector < vector < int > > Matrix;
    Matrix.assign(rows, vector < int >(columns, 0));
    
    // Do your stuff here...
    

    This will create a Matrix of size rows * columns and initializes it with zeros because we are passing a zero(0) as a second argument in the constructor i.e vector < int > (columns, 0).

    0 讨论(0)
  • 2020-11-28 09:55

    vector<vector<int> > a;

    If you want to define the rows and columns,

    vector<vector<int> > a{{11, 2, 4}, {4, 5, 6}, {10, 8, -12}};

    0 讨论(0)
提交回复
热议问题