vector stack behavior

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
	#include <vector>

	int main()
	{
	  std::cout<<"Example 1"<<std::endl;
	  std::vector<int>nStack;
	  // set some content in the vector:
	  for (int i=0; i<100; i++) {nStack.push_back(i+2);}
	  std::cout<<"test value :"<<nStack.at(40)<<" Ok"<<std::endl; //test value
	  std::cout << "size: " <<nStack.size() << "  capacity: " << nStack.capacity() << '\n';
	  nStack.pop_back();
	  nStack.pop_back();
	  std::cout<<"Print top of stack:";
	  std::cout<<nStack.back(); //print LIFO  -- test value
	  std::cout<<std::endl<<std::endl<<"2nd Example"<<std::endl;
	  std::cout<<"---------------------------"<<std::endl;


	  std::vector<int> array;
	  array = { 0, 1, 2, 3, 4 }; // okay, array length = 5
	  std::cout << "size: " << array.size() << "  capacity: " << array.capacity() << '\n';

	  array = { 9, 8, 7 }; // okay, array length is now 3!
	  std::cout << "size: " << array.size() << "  capacity: " << array.capacity() << '\n';

	  array.clear();
	  //std::cout<<array.at(3); //generates expected error


	  return 0;
	}
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.
These are the results I get - what do you expect ?

Example 1
test value :42 Ok
size: 100 capacity: 141
Print top of stack:99

2nd Example
---------------------------
size: 5 capacity: 5
size: 3 capacity: 5

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.

One other question:

std::cout << "size: " << nStack.size() << " capacity: " << nStack.capacity() << '\n';
std::cout << "size: " << (int) nStack.size() << '\n';
std::cout << "capacity: "<<(int)nStack.capacity() << '\n';
std::cout << "max_size: " << (int)nStack.max_size() << '\n';

Why is the (int) here? I don't think I saw that in the reference.
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. 
Last edited on
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.
Your questions make no sense.


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?

Thank you for your time.


Last edited on
How do I use vector for a stack versus vector for an array?

If you want a stack use std::stack.
http://www.cplusplus.com/reference/stack/stack/
By the way a vector is not an array, don't confuse the two.

Can you please expand on this above?

What don't you understand?

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().
Last edited on
That hit the nail on the head, especially the previous post. Thx to all.
Last edited on
Topic archived. No new replies allowed.