tags:

views:

127

answers:

5

The resize() function makes vector contain the required number of elements. If we require less elements than vector already contain, the last ones will be deleted. If we ask vector to grow, it will enlarge its size and fill the newly created elements with zeroes.

 vector<int> v(20); 
 for(int i = 0; i < 20; i++) { 
     v[i] = i+1; 
    } 
  v.resize(25); 

 for(int i = 20; i < 25; i++) { 
     v[i] = i*2; 
   } 

But if we use push_back() after resize(), it will add elements AFTER the newly allocated size, but not INTO it. In the example above the size of the resulting vector is 25, while if we use push_back() in a second loop, it would be 30.

 vector<int> v(20); 
 for(int i = 0; i < 20; i++) { 
     v[i] = i+1; 
  } 
 v.resize(25); 
  for(int i = 20; i < 25; i++) { 
  v.push_back(i*2);   // Writes to elements with indices [25..30), not [20..25) ! <
 } 

Then where is the advantage of resize() function ? Doesn't it creates a confusion for indexing and accessing elements from the vector ?

+3  A: 

Remember the alternative - reserve. resize is used when you want to act on the vector using the [] operator -- hence you need a "empty" table of elements. resize is not intended to be used with push_back. Use reserve if you want to prepare the array for push_back.

Resize is mainly usefull if the array has meaningful "empty" constructor, when you can create an array of empty elements, and only change the ones that are meaningful.

Kornel Kisielewicz
`[]` never throws.
GMan
@GMan, duh, I (as usually) confused it with `at()` -_-. No reason to downvote though.
Kornel Kisielewicz
Never, @GMan? I think it's unspecified. It could throw if the implementer chose to make it throw. The standard doesn't say one way or the other. To guarantee an exception, use `at` instead.
Rob Kennedy
I didn't downvote. Ironically, I was your up-vote.
GMan
@GMan, sorry for the accusation then. Wonder why it did get downvoted then... Ironically, sorry even more :)
Kornel Kisielewicz
@Rob: You're semi-right. It's undefined behavior to access an out-of-bounds element with `[]`; so I'm wrong saying it can't ever throw. But it's not unspecified per se, just anything could happen. @Kornel: No problems.
GMan
@Rob and @GMan, this poses an interesting question though -- is there an implementation that throws on out-of-bounds `[]` query? A debug mode with such a feature would be extremely useful.
Kornel Kisielewicz
@Kornel: Well, MSVC++ asserts in debug mode (and in release if you don't specify otherwise.) I don't know about other compilers; I assume they'd do the same. But I doubt any throw. Doing so requires a check, and the point of `[]` over `at` is to skip the check. And if it's gonna be debug only, might as well make it assert instead.
GMan
@GMan, fair enough. Just checked it (hate browsing through MSVC STL implementation).
Kornel Kisielewicz
+6  A: 

It sounds as though you should be using vector::reserve.

vector::resize is used to initialize the newly created space with a given value (or just the default.) The second parameter to the function is the initialization value to use.

Ben S
A: 

I don't really understand the confusion. The advantage of resize is that it resizes your vector. Having to do a loop of push_backs is both tedious and may require more than one "actual" resize.

If you want to "resize" your vector without changing its accessible indexes then use std::vector<T>::reserve. That will change the size of the internal allocated array without actually "adding" anything.

Peter Alexander
+1  A: 

resize() function changes the actual content of the vector by inserting or erasing elements from the vector. It does not only change its storage capacity. To direct a change only in storage capacity, use vector::reserve instead. Have a look at the vector visualization in the link, notice where v.back is pointing to.

sand
+3  A: 

The resize() method changes the vector's size, which is not the same as the vector's capacity.

It is very important to understand the distinction between these two values:

  • The size is the number of actual elements that the vector contains.
  • The capacity is the maximum number of elements that the vector could contain without reallocating a larger chunk of memory.

A vector's capacity is always larger or equal to its size. A vector's capacity never shrinks, even when you reduce its size, with one exception: when you use swap() to exchange the contents with another vector. And as others have mentioned, you can increase a vector's capacity by calling reserve().

I think that using the correct terminology for size and capacity makes it easier to understand the C++ vector class and to speak clearly about its behavior.

bk1e