Thread: Vector problem

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

    Vector problem

    Greetings;
    I was doing a problem in this book and this doesn't seem right to me. Can anyone pls give me their advice?

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

    1. capacity of x is __ and size is __
    2. capacity of y is __ and size is __
    3. " " z is __ and size is __
    4. " " w is __ and size is __

    what is the output of:

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

    Now for my answers pls see below:
    1. 0,0
    2. 7,0
    3. 4,2
    4. 6,0
    5. 14 and 39

    Now the part that confuses me and I wonder if someone can explain to me why?

    The Books answers:
    1. 0,0
    2. 7,7
    3. 4,4
    4.
    5. 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
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    How are we supposed to know the semantics of Vector? Is it the same as std::vector (at least, the answers are, but that's still no guarentee)?
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

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

    Thumbs up

    How are we supposed to know the semantics of Vector?
    Does this help: Arrays and Vector<T>, thats what the chapter is all bout!
    "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
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    is it capitalized? I think that's what SS was getting at. "vector" is something standard, "Vector" is not.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  5. #5
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378
    is it capitalized?
    No its not capitalized. just vector.
    does this help?
    "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

  6. #6
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378
    is it capitalized?
    No its not capitalized. just vector.
    does this help? Sorry for the confusion.
    "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

  7. #7
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    the books answers appear correct. I don't know much about the two parameter constructor on vector though (z). It seems though that w.size() should be 8 after the two push_backs
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  8. #8
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    which answer is confusing exactly?
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

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

    Here is another example problem?

    See comments down below to the books answers and i have explained in better detail my confusion.

    I thought that vectors were something like so...eg; vector<double> real vector(3 //this being its capacity, 4.0 //being its size). Correct??


    Vector<int> a, b(5), c(5, 1), d(5);
    w.push_back(77)
    w.push_back(88);

    1. capacity of a is __ and size is __
    2. capacity of b is __ and size is __
    3. " ... c is __ and size is __
    4. " ... d is __ and size is __

    What output is produced by

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

    These are my answers
    1. 0,0
    2. 5,0
    3. 5,1
    4. 5,0
    5. 77 and 88

    These are the books answers
    1. 0,0 //i understand this
    2. 5,5 // i understand the first 5 but not the 2nd, why?
    3. 5,5 // i understand the first 5 but not the 2nd, why?
    4. 10, 77 // I havent got a clue???
    5. 0,88 //I understand the front(0 is the first and back() is the last element +1 in an array, right? But why is the 0 in c.front()?
    Last edited by correlcj; 10-28-2002 at 04:34 PM.
    "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

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >See comments down below to the books answers and i have explained in better detail my confusion.
    Either your book is full of it or it isn't using the standard vector. The correct answers are:

    1) 0, 0
    2) 5, 5
    3) 5, 5
    4) 5, 5
    5) 77 88

    >1. 0,0
    This is correct, the default constructor creates an empty vector.

    >2. 7,7
    This is wacky, but not impossible. The implementation may choose to add extra elements, so capacity could be 7. Size on the other hand should be 5 since that is how many items were explicitly stated by the constructor.

    >3. 4,4
    This is the same as 2 except the value given to each element is 1 instead of the default value of 0 for int. There is no way the size and capacity could be less than the constructors arguments, then it wouldn't be able to hold them all.

    >4. 10, 77
    This is the same as the constructor for 2, having such large capacity is plausible, but once again size should be exactly what the constructor asked for.

    >5. 0,88
    77 is pushed onto the rear of the vector, then 88 was pushed behind it. The vector would have two elements, 77 and 88 in that order. front() returns the first valid element in the vector and back() returns the last valid element.

    My only conclusion is that the book either performs other operations or doesn't use the std::vector. Dare I ask what book this is?

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed