跟读练习: Stack Data Structure In STL | C++ Tutorial - 通过视频学习英语口语
正在创建课程...
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.
✨ 推荐视频
誰適合這支影片?
這支影片適合具備基礎C++知識、想學習STL容器的初級程式設計者,也適合需要鞏固堆疊資料結構概念的自學者。無論你是在校學生還是轉行學員,只要想瞭解「後進先出」(LIFO)機制的實際應用,或是想熟練使用STL中的stack類別,這部教程都能幫你快速上手。
值得積累的詞彙與表達
- stack data structure:堆疊資料結構。一種遵循「後進先出」原則的資料存儲方式,類似現實中疊放的盤子或書本。
- push:入棧。將元素添加到堆疊頂部的操作。
- pop:出棧。從堆疊頂部移除元素的操作。
- top:查看堆疊頂部元素(C++中稱為top,不同語言可能稱為peak)。
- template library:模板庫。C++標準庫的重要組成部分,提供可重用的資料結構和演算法。
如何模仿正確口音?
這段講解的語速中等,發音清晰,適合用於shadow speech(影子跟讀)練習。以下是幾個發音重點:
- stack:注意/k/的清輔音發音,不要帶濁化,類似「斯塔克」(輕讀尾音)。
- push和pop:短元音/ʊ/要發得飽滿,類似「鋪室」和「泡普」(尾音輕快)。
- template:重音在第一個音節/ˈtɛmplət/,類似「坦普雷特」(避免讀成「湯普雷特」)。
- 連讀現象:如「data structure」會連讀成/ˈdeɪtə ˈstrʌktʃər/,注意兩個單詞間的輕聲過渡。
建議反覆跟讀「So a stack data structure works like a stack of plates or books」這類句子,感受語調的起伏和停頓,提升英语口语练习的流暢度。對於準備雅思口语练习的學員,這種邏輯清晰的講解風格也值得借鑒,可鍛煉說理時的條理性。
看視頻學英語的小技巧
除了學習程式設計知識,還能通過以下方式提升英語能力:
- 邊看邊記錄專業術語,並整理成詞彙表,加深記憶。
- 嘗試用英語復述「堆疊的工作原理」,鍛煉口語表達和邏輯思維。
- 模仿講者的語氣和停頓,錄下自己的跟讀音頻並對比,找出發音差距。
這種「技能+語言」雙重學習的方式,能讓你在提升程式設計能力的同時,紮實練好英語,可謂一舉兩得。
视频中的语法
说话人最常用的结构,并附上视频中的原话:
| 结构 | 视频中的用法 |
|---|---|
| 被动语态 be + 过去分词 — 强调发生了什么,而不是谁做的 | be removed · have been swapped · be created |
| 现在完成时 have/has + 过去分词 — 过去发生但与现在仍有关联的事 | have been swapped · has been called · have been made |
| 定语从句 who / which + 从句 — 补充说明人或事物 | stack which is · stack, which is |
什么是跟读法?
跟读法 (Shadowing) 是一种有科学依据的语言学习技巧,最初开发用于专业口译员的培训,并由多语言者Alexander Arguelles博士普及。这个方法简单而强大:您在听英语母语原声的同时立即大声重复——就像是一个延迟1-2秒紧跟说话者的影子。与被动听力或语法练习不同,跟读法强迫您的大脑和口腔肌肉同时处理并模仿真实的讲话模式。研究表明它能显着提高发音准确性,语调,节奏,连读,听力理解和口语流利度——使其成为雅思口语备考和真实英语交流最有效的方法之一。











