# STL vector <T> problem

• 11-05-2002
correlcj
STL vector <T> problem
Greetings,
I am having difficulties understanding this problem, my concept of capacity and size properties of a vector are crystal clear but...
Can anyone solve this problem and explain why you got the answers you did? Below I have added my answers into the questions can anyone explain why I got these wrong or right?

See below:

vector<int> x,y(7),z(4,2),w(6);
w.push_back(14);
w.push_back(39);

1. capacity of x is 0 and size is 0
2. capacity of y is 7 and size is 0
3. " " z is 4 and size is 2
4. " " w is 6 and size is 0

what is the output of:

5. cout <<w.front() << ' ' << w.back()<<endl;

ans. 0, 39

:confused:
• 11-05-2002
JoeSixpack
w already had a capacity. push_back will add to that capacity. You're not 'pushing_back' to the front of the vector (or it would be called something different). Unless you assign to the already allocated front -

w[0] = 14;

then it'll always return zero.
• 11-06-2002
correlcj
1. capacity of x is 0 and size is 0
2. capacity of y is 7 and size is 7
3. " " z is 4 and size is 4
4. " " w is 6 and size is 6

what is the output of:

5. cout <<w.front() << ' ' << w.back()<<endl;

ans. 0, 39

I have found the correct answers and have revised it as shown above. Thanks anyways for everyone help! If i am still incorrect feel free to correct me but i believe these answers are correct now!
Thnx!

:D
• 11-06-2002
Prelude
>push_back will add to that capacity.
Not necessarily. The capacity is the number of items that the vector can hold, if you push_back one item when the vector capacity is six then the capacity will not change, only the size.

>1. capacity of x is 0 and size is 0
Yes, x is an empty vector.

>2. capacity of y is 7 and size is 0
No, The constructor creates a vector of size 7 and initializes it with the default value of the data type the vector was declared as, so capacity would be at least 7 and size would be 7.

3. " " z is 4 and size is 2
No, this constructor is the same as above except you specify the value to initialize with, so capacity would be at least 4 and size would be 4.

4. " " w is 6 and size is 0
If this question refers to w before the calls to push_back then it would be at least 6, and 6 as I described above, but if this questions refers to w after the calls to push_back then capacity will most likely be 12 and size will be 8 because the two added items may require the vector to resize itself to hold them.

>ans. 0, 39
Yes.

On a side note, didn't I answer a question very similar to this not too long ago?

-Prelude
• 11-06-2002
skipper
Quote:

Originally posted by Prelude
On a side note, didn't I answer a question very similar to this not too long ago?
10/27, actually, but I assume it was a slow day at the office today given the time that you posted your response. Nothing wrong with a good rerun, though. :D

P.S. No, I didn't memorize the date and thread. One search of "vector capacity" turned it up rather quickly. ;)

-Skipper
• 11-06-2002
Prelude
>but I assume it was a slow day at the office today given the time that you posted your response
Every day is a slow day where I work. How do you think I got over 3000 posts here. ;)

-Prelude
• 11-06-2002
Hammer
>>Every day is a slow day where I work.
That's 'cos you code too damn fast Prelude ;)
• 11-06-2002
skipper
Apparently Wednesday evenings aren't real hot either since you both managed to respond so quickly. (As opposed to my Wednesday evening which is filled with gleeful joy and gay abandon...wait...let me re-phrase the latter portion of that statement. :))

-Skipper
• 11-06-2002
Prelude
>let me re-phrase the latter portion of that statement
Don't you hate how perfectly benign words have been mangled by modern times to mean something bad?

-Prelude
• 11-06-2002
correlcj
Thanks!
Quote:

but if this questions refers to w after the calls to push_back then capacity will most likely be 12 and size will be 8 because the two added items may require the vector to resize itself to hold them.
Can you explain further how you concluded the answers 12 and 8, please prelude? What two items are you referring to?

I apologize for a similar question but i was still unsure of the whole problem. You have gone further in detail as to explain why and i appreciate that.

Thanks everybody and THANKS Prelude for the indepth lesson.
;)
• 11-06-2002
Prelude
>Can you explain further how you concluded the answers 12 and 8, please prelude.
Gladly, let's take a look at the definition for w:

vector<int> w(6);
w.push_back(14);
w.push_back(39);

The constructor creates 6 elements in w and fills them with the default value for an int, 0. The capacity at this point is at least 6 and the size is exactly 6. When 14 and 39 are pushed onto the end of the vector, the size increases to 8. A capacity of 12 is an educated guess because most implementations choose to double the current size of a vector when it gets full and more items need to be added, something like this:
Code:

```if ( size == capacity ) {   capacity *= 2;   // Allocate more raw memory }```
Since you can't be sure how much the implementation will resize the memory block, I had to assume that it was double, and 6 * 2 is 12. ;)

-Prelude
• 11-06-2002
correlcj
AWESOME!
Thanks again!
That makes perfect sense to me now.
:D