|
-
November 30th, 2003, 03:53 AM
#11
Originally posted by souldog
This is just a question stated as an answer.
int some_stuff[] = {1,2,3,4,5}
std::vector<int> vect_of_stuff(some_stiff, some_stuff + sizeof(some_stuff)/sizeof(some_stuff[0]));
the capacity of a vector initialized in this way is equal to its size.
Is there a reason why this does not refute the above claim?
I do not think this refutes my claim.
The standard guarantees that the invariant of your construct (calling a vector's range constructor) is that the size is equal to the number of elements within the range (provided the range is valid).
so size = 5 is guaranteed but capacity = 5 is not.
This holds for the key-value constructor, copy constructor, resize member function, erase member functions and clear member function. None have a guarantee for capacity.
std::vector<int> Example(0);
Example.empty() == true; Example.size() == 0; Example.capacity() == ?
The only vector member function that I think might have a hard guarantee on reducing capacity (reserve obviously makes one on increasing it) is swap. But swap cannot be performed by iterator - and hence not with a normal array (where you control the size).
I thankfully concede that this is a 'standards' question. I know of no STL implementation so brain-dead that the constructors discussed can't be used to control size (but I bet they exist out there somewhere right now - and they're probably still 'standard' conforming)!
The views expressed are those of the author and do not reflect any position taken by the Goverment of the United States of America, National Aeronautics and Space Administration (NASA), Jet Propulsion Laboratory (JPL), or California Institute of Technology (CalTech)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|