C++ Stack Usage

Started by
14 comments, last by load_bitmap_file 19 years, 9 months ago
I think I've got it figured out now. Thanks to you both gryfang and snk_kid!

I still want to know why you would want someone would want a "vector stack" or a "list stack" instead of a "deque stack" though.
Advertisement
Quote:Original post by load_bitmap_file
I think I've got it figured out now. Thanks to you both gryfang and snk_kid!

I still want to know why you would want someone would want a "vector stack" or a "list stack" instead of a "deque stack" though.


I'm not sure if i understood you correctly so i'll give you two answers.

If you mean't a list of stack of something then thats only if it comes up when conceptualizing the solutions.

If you mean't using the various containers to represent a stack, well if you remember what we where saying how it's abstract and can be represented by almost any structure, depending on which structure you decide to use has implications on how efficently it preforms a paritcular operation. Like a list is better for insertion & deletion in arbitrary places, vector is better for such and such.

When you use a stack you may decide that your going to be doing a particular operation more often than the rest, you check STL's documentation and see that say a list is more faster/efficent at that particular operation.

There is a notation used to measure against. It's called big-O notation you would learn about that if you took a course or read a book on data structures & algorithms.
One problem with using vector as a stack is that you give the users of the stack too much freedom. If you are trying to limit the usage of your container to a FILO paradigm, you don't want users calling push_back, pop_front whenever they want.

A good usage of stacks is in implementing your own scripting language. As users call various functions in their scripts, you need to keep track of the running script's "program stack" so you know where to return in the higher-level script when a function is complete.

Regards,
Jeff
Whoops, I got mixed up about what functions were available to stack. Thanks all!

EDIT: Ack, I've confused myself again. When making a stack of vectors, the vector functions don't become available to that stack object right? The intellisense thingy in VS didn't say so and I got a compiler error when I tried accessing pop_back() from a stack of type vector. Doesn't that mean that no matter what type of stack it is, it'll still only perform the same operations defined in stack and nothing else (EDIT: meaning that deques will always be the best type for stacks)?

EDIT (again): I think I may have understood you snk_kid. I meant declaring a stack like this:

std::stack<int, vector<int> > aStack;
Quote:Original post by load_bitmap_file
Whoops, I got mixed up about what functions were available to stack. Thanks all!

EDIT: Ack, I've confused myself again. When making a stack of vectors, the vector functions don't become available to that stack object right? The intellisense thingy in VS didn't say so and I got a compiler error when I tried accessing pop_back() from a stack of type vector. Doesn't that mean that no matter what type of stack it is, it'll still only perform the same operations defined in stack and nothing else (EDIT: meaning that deques will always be the best type for stacks)?

EDIT (again): I think I may have understood you snk_kid. I meant declaring a stack like this:

std::stack<int, vector<int> > aStack;


Becareful on the terminology it's not a stack of vector, it's a stack of ints implementated with a vector. Yes the interface is different and you can find out what operations are in the interface from here and yes deque is the best all round structure to use and it's the default so if do:

std::stack<int> my_stack_of_ints;


defaults to a deque implementation.
That about wraps everything up for me. Thank you very much snk_kid!

This topic is closed to new replies.

Advertisement