STL vector <T> problem

This is a discussion on STL vector <T> problem within the C++ Programming forums, part of the General Programming Boards category; Greetings, I am having difficulties understanding this problem, my concept of capacity and size properties of a vector are crystal ...

  1. #1
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    Question 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


    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    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.
    Joe

  3. #3
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378
    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!

    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    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.

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

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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
    My best code is written with the delete key.

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Every day is a slow day where I work.
    That's 'cos you code too damn fast Prelude
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    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
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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
    My best code is written with the delete key.

  10. #10
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    Thanks!

    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.
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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
    My best code is written with the delete key.

  12. #12
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    AWESOME!

    Thanks again!
    That makes perfect sense to me now.
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  2. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 02:04 AM
  3. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 09:37 AM
  4. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 06:18 AM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21