Multi-dimensional vector

后端 未结 8 2078
一个人的身影
一个人的身影 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:58
    std::vector< std::vector<int> > a;
    
        //m * n is the size of the matrix
    
        int m = 2, n = 4;
        //Grow rows by m
        a.resize(m);
        for(int i = 0 ; i < m ; ++i)
        {
            //Grow Columns by n
            a[i].resize(n);
        }
        //Now you have matrix m*n with default values
    
        //you can use the Matrix, now
        a[1][0]=98;
        a[1][1]=989;
        a[1][2]=981;
        a[1][3]=987;
    
    //OR
    for(i = 0 ; i < m ; ++i)
    {
        for(int j = 0 ; j < n ; ++j)
        {      //modify matrix
            int x = a[i][j];
        }
    
    }
    
    0 讨论(0)
  • 2020-11-28 09:59

    If you don't have to use vectors, you may want to try Boost.Multi_array. Here is a link to a short example.

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