开发者

C++: Reserve capacity for a std::vector, which is inside another container

I have following requirement,

std::vector< std::vector < std::string > > segments;

There are unknown numb开发者_JAVA技巧er of segments, but each segment is of fixed size say 1000. But, I am not able to figure out how do I declare variable segments with my requirements.

I tried

std::vector< std::vector < std::string >(1000) > segments; // not working

Thanks in advance..


std::vector is a dynamic container and doesn't allow you to specify a fixed-size. As an alternative you could look into Boost.Array (or the versions in TR1 etc.):

typedef boost::array<std::string, 1000> Segment;
typedef std::vector<Segment> SegmentVec;
SegmentVec segments;


You can't. You have to loop through and call reserve manually. (And for that matter there are no vectors inside segments yet.. how could reserve space in a nonexistent vector? :) )


Just allocate a single vector with 1000*segment count elements, and use segment * 1000 as the offset into it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜