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.
Author
Released
12/20/2018- 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
Duration
Views
Related Courses
-
Learning C++ Pointers
with Peggy Fisher53m 55s Intermediate -
Learning C++
with Erin Colvin2h 26m Beginner
-
Introduction
-
1. Templates
-
What is a template?4m 43s
-
Template syntax1m 48s
-
Template specialization1m 49s
-
Template variables4m 14s
-
Impact on header files3m 13s
-
Type inference6m 45s
-
Argument deduction3m 15s
-
-
2. STL Containers
-
3. STL Iterators
-
Accessing iterators2m 28s
-
Input iterators2m 2s
-
Output iterators1m 27s
-
Forward iterators1m 58s
-
Bidirectional iterators1m 59s
-
Random access iterators2m 2s
-
-
4. Transformations
-
The transform function5m 28s
-
Lambda transformations4m 23s
-
Transforming strings2m 49s
-
Binary transformations3m 7s
-
Transforming types3m 3s
-
-
5. STL Functions
-
About functors1m 46s
-
Arithmetic functors2m 10s
-
Relational functors3m 13s
-
Logical functors1m 31s
-
-
6. STL Algorithm
-
About STL algorithms1m 42s
-
Testing conditions2m 36s
-
Searching and counting4m 38s
-
Replacing and removing4m 14s
-
Modifying algorithms5m 46s
-
Partitions2m 52s
-
Sorting3m 42s
-
Merging sequences2m 4s
-
Binary searches3m 57s
-
-
Conclusion
-
Goodbye1m 29s
-
- 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.
CancelTake 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.
Share this video
Embed this video
Video: Stack