Shadowing Practice: Stack Data Structure In STL | C++ Tutorial - Learn English Speaking with Video

Creando lección...
1
In this video, we're going to learn about the stack data structure class template that's built into the C++ standard template library.
2
So a data structure stores a collection of data and stacks are a type of data structure.
3
Stacks are very commonly used in practice, so they're included directly in the C++ standard template library.
4
Stacks are a last in first out data structure.
5
What
6
that means is the last element that's put into the data
7
structure is going to be the first element out of the data structure.
8
So a stack data structure works like a stack of plates or books.
9
For example, we could have a stack data structure to store integer values.
10
And when we insert elements onto the stack, we say that we push them onto the stack.
11
When we remove elements from the stack, we say that we pop them from the stack.
12
So for example, if we pushed the element 5 onto the stack, 5 would be here.
13
We could call this the bottom of the stack.
14
We could then push the element 7 onto the stack, and 7 would go here.
15
We would say that 7 is at the top of the stack.
16
Then we could push another element onto the stack.
17
So we could push 9 onto the stack.
18
9 would then be the new top of the stack.
19
Now if we popped an element off this stack, it's always going to be removed from the top of the stack.
20
So if we popped an element off this stack here it would be this element here that's popped
21
and then seven would become the new top of the stack
22
and we could keep popping elements from the stack
23
and eventually the stack would be empty where an empty stack
24
is a stack with no elements we would say this stack here has a size of two
25
because the stack has two elements and there's also typically an operation which allows us to view the top of the stack.
26
It's usually called peak.
27
In C++, it's called top.
28
So let's try out the stack data structure that's built into the C++ standard template library.
29
The first thing we'll do is include the stack library.
30
This will allow us to declare and use stack objects.
31
Then down here, we'll declare a stack object to store in values called numbers.
32
So this will declare a stack object to store in values called numbers.
33
Now initially this stack is going to be empty and it's going to have a size of zero.
34
Stack objects have size and empty methods
35
that we can use to check the size of a stack and to check whether it's empty or not.
36
So let's call those methods.
37
Here we'll output size colon
38
and then we'll call numbers .size where size is going to return the size of the stack
39
and we'll output that then we'll also call the empty method to check
40
if the stack is empty so
41
if numbers .empty is true then we'll output here stack is empty followed by an end line
42
so the empty method is going to return true if the stack is empty and false otherwise.
43
So right now, if we save compile and run the program, we'll get that the stack has a size of zero and that the stack is empty, both of which are correct.
44
So now let's push an element onto the stack.
45
We'll have here numbers .push and we'll push the value eight onto the stack.
46
Now, if we check the size of the stack, the size should now be one.
47
Let's try that we'll copy this and down here we'll paste it
48
and if we save compile and run the program now
49
we see the size is now one we could also check
50
if the stack is not empty so now we'll have if
51
not numbers dot empty is true so in other words if the empty function now returns false
52
then we'll output stack is not empty followed by an end line
53
and if we save compile
54
and run the program we'll now get stack is not empty
55
we can also retrieve the value at the top of the stack using the top method
56
so we could call here numbers dot top
57
and this is going to return the value that's at the top of the stack.
58
We'll output that.
59
We'll have here top colon and we'll output the value at the top of the stack followed by an end line.
60
And if we save compile and run the program, we'll now get the value at the top of the stack is eight.
61
And that makes sense because right now eight is the only thing on the stack.
62
We could try to push a few more values onto the stack
63
so right now we have eight at the bottom of the stack which is also the top of the stack.
64
We could push on, let's say, the value 9.
65
We'll have numbers .push and then 9.
66
So then at this point, 9 would become the new top of the stack and 8 would still be at the bottom of the stack.
67
We could also push on the value 5.
68
We could have numbers .push 5.
69
And then 5 would be at the top of the stack and the stack would now have a size of 3.
70
So let's output the size and top after performing these operations.
71
Down here, we'll output the top again with cout top and numbers .top.
72
We'll also output an endline for spacing and we'll also output the new size.
73
So we'll have cout and size colon and we'll have numbers .size to output the size followed by an end line.
74
And now if we save compile and run the program, we'll get that the new top of the stack is five and the new size of the stack is three,
75
both of which are correct.
76
So then we could also pop a value from the stack.
77
So for example, with this stack here, if we pop a value from the stack, it's going to be the element five that's removed from the stack.
78
so let's try that we'll use the pop method of the stack object to do that we'll have here numbers .pop
79
now this method is only going to remove the element at
80
the top of the stack it's not going to return it
81
so we'll output here the new top of the stack
82
and the new size of the stack we'll copy these and paste them after the pop.
83
So now if we save compile and run the program, we'll get that the new top of the stack is nine and the new size of the stack is two,
84
both of which are correct because here we have nine at the top of the stack
85
and the size of the stack is now two.
86
Now again, the pop method is not going to return the value that's removed from the stack.
87
So we're going to lose it if we don't save it.
88
So what we may want to do is save the value in a variable.
89
For example, we could have here int popped value is equal to,
90
and we'll call numbers .top to return the value that's currently at the top of the stack
91
and save it into this variable here.
92
Then we can pop the value, but we still have the value saved in this variable here.
93
So we could output that we could have cout
94
and let's say popped value colon
95
and we'll output the pop value followed by an inline and
96
if we save compile
97
and run the program we'll see the pop value is five
98
the previous top of the stack before we pop the value
99
off the stack now we can also use the swap method to swap the contents of two stacks.
100
So for example, let's declare another stack to store int values.
101
We'll have here stack int, and we'll call this stack other stack.
102
Then we'll push the value four onto the stack.
103
So we'll have other stack dot push four.
104
So this stack only has one value on the stack.
105
We could output the size of the stack here.
106
We could have cout
107
and then other stack size colon we'll output the size of the other stack
108
and we'll also output an end line as well.
109
So now if we save compile and run the program, we'll get here that this other stack has a size of one.
110
We can now swap this stack with our original stack numbers.
111
So we could have other stack and then dot swap to call the swap method and we'll pass it numbers.
112
And this will swap the contents of the two stacks.
113
So other stack should now have the two values nine and eight and numbers should now have the one value four.
114
Let's output the size of the stacks to confirm this.
115
So we'll output here an end line and then we'll output the size of other stack.
116
So we'll copy this and paste it here
117
and we'll also put the size of the number stack with numbers size colon
118
and we'll output numbers dot size
119
and then we'll output end lines as well one after outputting the other stack size
120
and another after outputting the number stack size so now if we save compile and run the program,
121
we'll see the other stack size is two and the number stack size is one.
122
So we can tell these stacks have been swapped.
123
Now the type int is a primitive type in C++.
124
We can also push objects onto a stack using the push method.
125
If we use the push method to push objects onto the stack, a copy of the object is going to be created.
126
There's also a method called in place.
127
The in place method will actually forward its arguments to the constructor of the object
128
and create an object for the purposes of the stack.
129
Let me show you the difference.
130
The first thing we'll do is define our own class called employee.
131
So up here we'll define a class called employee and this is going to be a pretty simple class.
132
We'll have one public member variable called days.
133
The constructor for employee objects
134
is going to accept an int as an argument called days
135
and we'll set the days member variable to the days value that's provided.
136
We'll also output in this constructor constructor called
137
and the number of days just
138
so we can verify the constructor has been called followed by
139
an inline we'll also create what's called a copy constructor the
140
copy constructor is called to create a copy of the object
141
its parameter is going to be a reference parameter of the same type of this object here employee
142
so we'll have const employee and then end employee and we'll output here that the copy constructor has been called.
143
So we'll have copy constructor called and we'll output the days in this object here followed by an end line.
144
And we'll also set this copy objects days to match the days of the object that it's copying.
145
So we're making a copy of this object here.
146
So we're going to take its days value and set the days member variable of this employee object to that value.
147
So then down here, we'll declare a stack to store employee objects.
148
We'll have stack employee, and we'll call this stack to store employee objects, employee underscore stack.
149
We'll create an employee object called Joe.
150
We'll have employee Joe, and we'll have 10, and maybe 10 is the number of days that Joe worked, then we'll push Joe onto the stack.
151
So we'll have employee stack dot push Joe.
152
So now if we save compile and run the program,
153
we'll get this constructor called 10 and then again, copy constructor called 10.
154
So what's happened here is two objects have been made.
155
Originally, we made the Joe object here.
156
then when we push Joe onto the stack, a copy of the Joe object is made.
157
So be aware of the fact that's how it works.
158
Now we could also use a method called emplace,
159
and the emplace method will actually forward its arguments to the constructor of this type of object that the stack is storing, in this case here, employee.
160
And it's going to create an object that's going to exist on the stack.
161
So if we have here employee stack and we call the emplace method
162
and we pass it let's say 20 what's going to happen
163
is 20 is going to be passed to an employee object constructor
164
and the employee object that's created is going to be pushed onto the stack.
165
So if we save compile and run the program notice this this time we only get constructor called 20
166
No copy constructor is called.
167
So in the case of Joe, the Joe object was created, and when we push Joe onto the stack, a copy of that object was created.
168
Here, that's not occurring.
169
Here, what's happening is the object is being created and placed directly on the stack.
170
Now, notably, if we call the top method to retrieve the element at the top of the stack, and we assign the result to an employee variable,
171
at that point the copy constructor is going to be called.
172
So if we had let's say employee and we'll have maybe top employee is equal to employee stack dot top.
173
At this point the copy constructor will be called to make a copy of that employee object.
174
So if we save compile
175
and run the program we'll find now a copy has been made of that object at the top of the stack.
176
Now when we pop an object from the stack, the destructor for that object is going to be called.
177
So we could have a destructor in our employee class.
178
We'll have here tilde employee.
179
And again, we'll make a very simple destructor.
180
What we'll do is just output that the destructor has been called.
181
So we'll have cout and then destructor.
182
and called and we'll put the days followed by an inline.
183
So now down here, if we pop the top element from the stack, we'll see that destructor is called.
184
So what we'll have is employee and then stack and we'll have dot pop to pop the top element from the stack.
185
I'm actually going to comment out this line here and this line here and this line here.
186
So we can just focus on this one element at the top of the stack.
187
So right now, if we save compile and run the program, we'll see here that we get destructor called 20.
188
So when we pop that object from the stack, the destructor was called.
189
So in C++, the implementations of different data structures like stacks and queues that come with the standard template library are called containers.
190
In particular, the stack container is called a container adapter.
191
It's called a container adapter because underlying a stack object is going to be a container object of a different type.
192
So there might be something like a vector
193
or a DQ container object that's really behind the scenes where
194
the stack object is kind of like a limited interface
195
that allows us to access that other object in a limited way that conforms to the rules of a stack, which is important.
196
The last in first out behavior of stack data structure elements is what makes stacks useful in certain scenarios like parsing.
197
So this is how we can use stacks in the C++ standard template library.
198
Check out portfoliocourses .com where we'll help you build a portfolio that will impress employers.
199
Thank you.

Who Is This Video For?

This video is perfect for English learners at an intermediate level who want to improve their technical vocabulary and listening skills. Whether you’re studying computer science or just curious about programming concepts, it offers clear, slow-paced explanations that make complex ideas easy to follow. The structured approach—breaking down "stack" mechanics step by step—also helps learners practice understanding sequential instructions, a key skill for both academic and professional English.

Words & Idioms Worth Stealing

  • Last in first out (LIFO): A common technical phrase describing a system where the most recent item added is the first to be removed. Example: "A stack of plates works on a last in first out basis."
  • Push (and pop): Verbs used specifically with stacks to mean "add" and "remove." Example: "I’ll push the book onto the stack, then pop it off later."
  • Top of the stack: The most recent item added. Example: "The red pen is at the top of the stack."

How to Get the Accent Right

The speaker uses a clear, neutral accent with consistent stress on key terms like "stack," "push," and "pop." To practice, use a shadowing app or shadowing site to mimic their rhythm. Focus on stressing the first syllable of "structure" (STRUK-ture) and "template" (TEM-plate). Notice how they pause after explaining concepts—this helps with natural pacing. For tricky phrases like "last in first out," repeat them using shadow speak techniques: listen, pause, then repeat immediately. A shadowspeak tool can help you match the speaker’s intonation, ensuring you sound confident when using these technical terms in conversation.

¿Qué es la Técnica de Shadowing?

Shadowing es una técnica de aprendizaje de idiomas respaldada por la ciencia, desarrollada originalmente para la formación de intérpretes profesionales y popularizada por el políglota Dr. Alexander Arguelles. El método es simple pero poderoso: escuchas audio en inglés nativo y lo repites en voz alta de inmediato, como una sombra que sigue al hablante con solo 1-2 segundos de retraso. A diferencia de la escucha pasiva o los ejercicios de gramática, el shadowing obliga a tu cerebro y músculos de la boca a procesar y reproducir simultáneamente patrones de habla reales. Las investigaciones muestran que mejora significativamente la precisión de la pronunciación, la entonación, el ritmo, el habla conectada, la comprensión auditiva y la fluidez al hablar, convirtiéndola en una de las metodologías más efectivas para la preparación del IELTS Speaking y la comunicación en inglés en el mundo real.

Técnica de shadowing: lee la guía completa paso a paso →