문제

Consider the allocation of objects pointers for a vector:

std::vector<mapSeg*> mapLayer[3];

Is it possible to make this implmentation a contiguous array of objects of a fixed size?

Is it possible to allocate a block of memory for 3 * 64 objects and have them addressed in a vector properly like the setup described above? Each of the arrays represents a layer of objects on a 2D map (back, mid, fore), so access to these layers must be consistant

올바른 솔루션이 없습니다

다른 팁

That depends how much of the semantics you want to preserve. The first step would be

std::vector<object> array[3];

Now we have a (effectively) a 3 element array of variable size storage.

We could also wrap a std::vector<object> maintain three sizes and provide access to a slice of the vector. Insertions into one of the subvectors now become rather expensive because possibly a lot of data has to be copied around, something the std::vector<object> array[3] version avoids. Iterator invalidation properties would also be stricter.

It depends on what you want to do. If you are going to repeatedly emplace_back on either of the three vector use the first version, if you fill them in order, don't erase elements, the last version provides marginal benefits.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top