Why am I getting the correct capacity for the 2nd example but no the first. This presupposes that 128 is wrong, which itr may not be. Could someone please clarify this?
The value returned by std::vector::capacity() is unspecified. The only restriction is that std::vector::capacity() >= std::vector::size(). Various vector operations may or may not change the capacity. The language designers left that up to the library implementers on purpose, so they could decide how to prioritize allocation and memory efficiency for the particular platform.
Also I noted after I posted that you can manually set the size with .reserve(number)
BUt why would I do that given what you said about the container design.
Why is the (int) here? I don't think I saw that in the reference.
That's a good question, where you found this code is another question. That (int) (a C style cast) is not needed. And IMO using a C style cast in C++ programs is a bad practice. If you must cast you should be using C++ style casts (static_cast<int>(value), etc.). Also casting a size_t (the type of variable returned by size or capacity) is also a bad practice, because a size_t can hold values that are much larger than can be held by an int. A size_t is an implementation defined unsigned type (unsigned int, unsigned long, or unsigned long long).
Also I noted after I posted that you can manually set the size with .reserve(number)
The vector.reserve() doesn't alter the size() of the vector, it alters the capacity of the vector.
std::vector::reserve() is used to increase the vector's capacity, not to set it. This is useful if you know ahead of time how large the vector can get, to avoid unnecessary reallocations. http://www.cplusplus.com/reference/vector/vector/reserve/
AFAIK, no vector function is required to ever shrink a vector's capacity, other than moving the vector:
1 2 3 4 5 6
std::vector<int> a;
// populate a
{
std::vector<int> b = std::move(a);
}
// a should now be a zero capacity vector.
Ok, I understand. I read up on vectors and see size, capacity and reserve now. It makes sense that there is a container to track all of this. Here is something more mundane with vector I don't understand:
When I initialize a list of values into a vector (e.g. array = {1,3,2,5,6,7,4,9} am I putting in array values or stack values?Also how do you identify a stack instead of a list looking at that values?
Second, I know that array.pop_back() will start with the right most number (9) and move leftward with each pop. Am I to assume that LIFO applies, and if so how did 9 become the last in ready for first out?
When I initialize a list of values into a vector (e.g. array = {1,3,2,5,6,7,4,9} am I putting in array values or stack values?Also how do you identify a stack instead of a list looking at that values?
Your questions make no sense. When you use the { ... } to designate an initializer list, a std::initializer_list is used which is generally a wrapper around an array with automatic storage duration, but that shouldn't matter to you.
Second, I know that array.pop_back() will start with the right most number (9) and move leftward with each pop. Am I to assume that LIFO applies, and if so how did 9 become the last in
ready for first out?
A vector is not a LIFO data structure, although you may treat it as such if you only insert and remove from the end. As for the positioning, you gave vector an ordered sequence. It respects the order.
I don't think that's the case at all. How do I use vector for a stack versus vector for an array? How would there respective data structures be like?If I have these confused please enlighten me, I want to learn.
As for the positioning, you gave vector an ordered sequence. It respects the order.
Can you please expand on this above?
Finally a vector is not a LIFO data structure, although you may treat it as such if you only ins0ert and remove from the end. As for the positioning, you gave vector an ordered sequence. It respects the order.
Ok, so I take it that pop_back() has no inherent LIFO. At least with vector array.
What about the vector stack?
When you place an item in a vector you place it at a certain location within the vector, the value of the item doesn't matter.
Ok, so I take it that pop_back() has no inherent LIFO.
Correct, because you can insert elements into a vector at any location.
If you want a LIFO structure you must always place elements at the end of the vector, and pop elements from the end of the vector.
What about the vector stack?
What is a vector stack?
If you're talking about std::stack the difference is that std::stack only allows you to put items into the container at the top of the stack, and you can only pop items from the top of the stack.
How do I use vector for a stack versus vector for an array? How would there respective data structures be like?
A single vector can behave as either a stack or an array at any given time, depending on which specific functions you are using. When you use push_back()/pop_back(), it behaves like a stack. When you using operator[](), it behaves like an array.
The internal representation is designed to accommodate both of these kinds of operations without internal rearrangements (except when the vector runs out of capacity).
Ok, so I take it that pop_back() has no inherent LIFO. At least with vector array.
What about the vector stack?
What cire means is that a vector is not a LIFO structure because a LIFO structure has a narrowly defined interface. A LIFO structure should only give you empty(), push(), pop(), and maybe peek() and size(). It shouldn't let you access arbitrary elements like a vector does.
A vector can be used as if it was a LIFO structure if you only use push_back() and pop_back().