3D array C++ using int [] operator

后端 未结 7 1855
长发绾君心
长发绾君心 2020-12-15 09:03

I\'m new to C/C++ and I\'ve been cracking my head but still got no idea how to make an \"structure\" like this

\

7条回答
  •  醉梦人生
    2020-12-15 09:16

    What you're trying to do is not idiomatic in C++. Of course, you can use a int***pointer for this, but this is strongly discouraged. In C++ we have better ways to get there.

    vector > > foo (5,vector >(4, vector(3)));
    

    This will result in something with the memory layout similar to what you asked for. It supports dynamic resizing and inner vectors to have different sizes just like in your picture. In addition, you don't have to worry about manual allocation / deletion of any of it. Also, the vectors know their size so you don't have to remember it somewhere.

    But if you just want a "rectangular" 3D array where all the elements are consecutivly stored in the same memory block, you could use a boost::multiarray.

提交回复
热议问题