C++: Reserve capacity for a std::vector, which is inside another container
I have following requirement,
std::vector< std::vector < std::string > > segments;
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 vector
s 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.
精彩评论