Explore the STL stack, a container adapter that implements a last-in, first-out stack based on another container.
- [Instructor] The STL stack is a container adapter which is like a wrapper that uses an underlying container to hold its contents. The stack implements a last in first out stack where elements are pushed and popped from the top of the stack which is the back of the container. Here I have a working copy of stack dot CPP from chapter two of the exercise files, and you'll notice that I have the stack header included which includes the definition of the stack type.
The stack destructor uses two arguments, the type of the elements and the type of the underlying container which in this case is a vector of integers, and it's being initialized from the vector of integers that I have declared beforehand and initialized with an initializer list. Report simply calls this function up here which reports the size and the top element which is the back of the underlying container, and I can push elements, I can pop elements and in order to display everything in the stack, of course I need to pop them all out, and they'll come out in reverse order because it's a last in first out queue.
And so if I build and run this, you'll see that here's my size. The top element is five which is the end of the vector. If I push a 42, the size is now six and the top is 42, and if I pop, then the size is five and the top is five again, and if I pop all which is in this loop here, you notice they come out in reverse order.
Now, just like the queue, the stack has a default underlying container which is the deque container which is optimized for this sort of use. So I can declare a stack without an underlying type and it will simply build a deque for me, and then I can push elements on to it, in this case, their strings, and you'll notice that this works just like our integer version with the vector. The top is five, I can push 42, the top is 42.
You can pop it, the top is five again, and I can pop all of them and they come out in reverse order. So a stack is a container adapter that adapts an underlying container into a last in first out queue.
- Template fundamentals
- STL containers
- Accessing iterators
- Output, forward, and bidirectional iterators
- The transform function
- Transforming strings and types
- Arithmetic, relational, and logical functions
- STL algorithms
Skill Level Intermediate
2. STL Containers
3. STL Iterators
5. STL Functions
6. STL Algorithm
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.