c++ - Advantages of 2D vector array -
previously, i've been creating 2d arrays of class object
using pointer, in following syntax:
object** myarray = new object*[row_num]; (int = 0; < row_num; i++) { myarray[i] = new object[col_num]; [skip] }
however, many people have been recommending me use vector<vector<object>>
rather using object**
.
as far i'm concerned, vector requires more memories trade-off easier change in array size. however, since 2d array need used backtracking algorithm of grid (which never change dimension once it's determined), not feel necessity of changing them.
are there other advantages of vector
i'm unaware of?
automatic deallocation
as @nathanoliver mentioned, never need call delete
(often customized recursive deletion) multi-dimensional vectors, objects inside released when fall out of scope automatically. can reduce amount of code need write , maintain.
obviously, if vectors
contain objects allocated new
or malloc
, you'll need delete them normally. shared_ptr
s come in that's topic.
regarding overhead
while there slight overhead each vector, far more in line c++ paradigm use them on traditional c-style arrays. if vector of fixed size, can use std::array<t, n>
, , avoid lot of overhead having dynamically sized container brings.
c++-ness
consistency in language important. if you're going writing c++ using standard library, should stick closely possible. i've worked @ many places use random hodge-podge of c , c++ mixed together, , makes reading , understanding code nightmare.
Comments
Post a Comment