c++ - Vector of Vector Initialization -
i having tough time getting head wrapped around how initialize vector of vectors.
typedef vector< vector < vector < vector< float > > > > datacontainer;
i want conform to
level_1 (2 elements/vectors) level_2 (7 elements/vectors) level_3 (480 elements/vectors) level_4 (31 elements of float)
addressing elements isn't issue. should simple like
dc[0][1][2][3];
the problem need fill data coming in out of order file such successive items need placed like
dc[0][3][230][22]; dc[1][3][110][6]; //...etc
so need initialize v of v beforehand.
am psyching myself out or simple as
for 0..1 0..6 0..479 0..30 dc[i][j][k][l] = 0.0;
it doesn't seem should work. somehow top level vectors must initialized first.
any appreciated. sure must simpler imagining.
please do not use nested vectors if size of storage known ahead of time, i.e. there specific reason why e.g. first index must of size 6, , never change. use plain array. better yet, use
boost::array
. way, benefits of having plain array (save huge amounts of space when go multi-dimensional), , benefits of having real object instantiation.please do not use nested vectors if storage must rectangular, i.e. might resize 1 or more of dimensions, every "row" must same length @ point. use
boost::multi_array
. way, document "this storage rectangular", save huge amounts of space , still ability resize, benefits of having real object, etc.
the thing std::vector
(a) meant resizable , (b) doesn't care contents in slightest, long they're of correct type. means if have vector<vector<int> >
, of "row vectors" must maintain own separate book-keeping information how long - if want enforce they're same length. means manage separate memory allocations, hurts performance (cache behaviour), , wastes more space because of how std::vector
reallocates. boost::multi_array
designed expectation may want resize it, won't resizing appending elements (rows, 2-dimensional array / faces, 3-dimensional array / etc.) end. std::vector
designed (potentially) waste space make sure operation not slow. boost::multi_array
designed save space , keep neatly organized in memory.
that said:
yes, need before can index vector. std::vector
not magically cause indexes pop existence because want store there. however, easy deal with:
you can default-initialize vector appropriate amount of zeros first, , replace them, using (size_t n, const t& value = t())
constructor. is,
std::vector<int> foo(10); // makes vector of 10 ints, each of 0
because "default-constructed" int has value 0.
in case, need specify size of each dimension, making creating sub-vectors of appropriate size , letting constructor copy them. looks like:
typedef vector<float> d1; typedef vector<d1> d2; typedef vector<d2> d3; typedef vector<d3> d4; d4 result(2, d3(7, d2(480, d1(31))));
that is, unnamed d1
constructed of size 31, used initialize default d2
, used initialize default d3
, used initialize result
.
there other approaches, they're clumsier if want bunch of zeroes start. if you're going read entire data set file, though:
you can use
.push_back()
append vector. make emptyd1
before inner-most loop, in repeatedly.push_back()
fill it. after loop,.push_back()
result ontod2
created before next-innermost loop, , on.you can resize vector beforehand
.resize()
, , index (up amount resized to).
Comments
Post a Comment