Trying to create a 3 dimensional vector in c++

后端 未结 2 1237
暖寄归人
暖寄归人 2020-12-30 06:00

So, im trying to create a 3 dimensional 5x3x2 vector, using the vector lib and saving the number 4 in every node.

Thats what im trying:

vector

        
相关标签:
2条回答
  • 2020-12-30 06:18

    You almost got it right -- the second nested vector should be vector<vector<int> >, not just a vector<int>:

    vector<vector<vector<int> > > vec (5,vector<vector<int> >(3,vector <int>(2,4)));
    
    0 讨论(0)
  • 2020-12-30 06:26

    Also you can declare of this forms:

    // first form
    typedef vector<int> v1d;
    typedef vector<v1d> v2d;
    typedef vector<v2d> v3d;
    v3d v(5, v2d(3, v1d(2, 4)));
    
    // second form
    vector<vector<vector<int> > > v = vector<vector<vector<int> > >( 5, vector<vector<int> >(3, vector<int>(2, 4)))
    
    0 讨论(0)
提交回复
热议问题