# Vector problem

• 10-22-2002
correlcj
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?

1. 0,0
2. 7,7
3. 4,4
4.
5. 0,39
:confused:
• 10-22-2002
SilentStrike
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)?
• 10-23-2002
correlcj
Quote:

How are we supposed to know the semantics of Vector?
Does this help: Arrays and Vector<T>, thats what the chapter is all bout!:D
• 10-23-2002
FillYourBrain
is it capitalized? I think that's what SS was getting at. "vector" is something standard, "Vector" is not.
• 10-23-2002
correlcj
Quote:

is it capitalized?
No its not capitalized. just vector.
does this help?
• 10-23-2002
correlcj
Quote:

is it capitalized?
No its not capitalized. just vector.
does this help? Sorry for the confusion.
;)
• 10-23-2002
FillYourBrain
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
• 10-23-2002
FillYourBrain
• 10-27-2002
correlcj
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;

1. 0,0
2. 5,0
3. 5,1
4. 5,0
5. 77 and 88

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()?
• 10-27-2002
Prelude
>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