Game Development Reference
In-Depth Information
Chapter 17
STL's Stack and Queue
The STL containers you have seen so far have all provided options for storing and retrieving values
and are suited to different circumstances. The stack and queue containers are designed for different
reasons. Although they do store values, their behavior is much more important. I'll show you exactly
how these containers behave in this chapter.
Note The game examples in this topic don't use these containers, but I didn't want to leave them out. You
might find a stack useful in a game where you are building a UI system and want to track the path to the
current screen. I've also found a queue to be useful in event systems where you wish to process events in
the exact order in which they were received.
The STL stack Container
The stack container is used when you would like a container that can only be accessed in a last in,
first out behavior. You can think of this like a tower of blocks. When you place a block onto a tower,
that is the only block you can then remove. Hopefully you can see how this container is useful more
for its behavior than for its ability to contain values. Listing 17-1 shows how you can specialize the
stack container.
Listing 17-1. Specializing the stack Container
using namespace std;
using MyStack = stack<int>;
You should be growing comfortable with the syntax used when specializing templates. Listing 17-2
shows how we can create an instance of a stack and add values to it.
177
 
 
Search WWH ::




Custom Search