跟讀練習: Learn C++ With Me #19 - Sets - 通過影片學習英語口語

選擇練習模式

每種模式都會開啟全屏練習室。

217 句
如果句子過短或過長,請點選 Edit 進行調整。
1
Hello everybody.
2
And welcome to another C plus plus tutorial for beginners.
3
In today's video, I'll be covering sets.
4
Now we set is a data structure that tells us if an element is present or not.
5
That's really the best way to describe it.
6
And on this video, I will talk to you all about it. So the best way
7
that I can describe a set in the shortest number of words possible is an unordered collection of unique elements.
8
Keep that in mind.
9
As we go through this kind of example here, a set is an unordered collection of unique elements.
10
What that means is that when we use a set, we do not care about the order of elements.
11
We do not care about the frequency of elements.
12
We simply care does an element exist or does it not?
13
There's a lot of very good use cases for a set.
14
And the reason you would use a set is a similar reason to why you would use a map
15
because finding if something exists or if it doesn't exist can happen instantly.
16
So unlike an array where, as I described, you would need to look through the entire thing, or unlike a vector where you would need to look through the entire thing,
17
because it's an ordered collection of potentially non unique elements in a set, the element either exists or it does not.
18
And so to find it can happen instantly.
19
It doesn't matter if the set has a million elements or if it has one element.
20
Now I want to be very clear here, as I described this, this is kind of a generalization.
21
There is some instances in which you're using a set or a map, and it will take a little bit longer than instantly to discover if something exists.
22
Now that is again, well beyond the scope of this tutorial.
23
I'm not getting into lower level implementation of C plus plus built in features, but just understand that all of this is a very general way of looking at this.
24
As you learn more about computer science and C plus plus and programming in general, you will kind of realize how these things actually work beyond the level at which we're using them at.
25
Anyways, with that said, let's get started here.
26
I'm going to include set.
27
So include set like that.
28
So let's make a set.
29
When we make a set, what we do is we say set, we do our angle brackets.
30
We do the type in this case, I'll just make it, uh, actually let's make it a char set.
31
Uh, and then we do the name of the set.
32
So I'll say S one.
33
Now, if you want to initialize the set, uh, I'm just looking at my kind of cheat sheet here.
34
Actually, it doesn't even show what I can do.
35
Let's just try to initialize the set by adding some characters like C and D to it and see if this works.
36
All right.
37
So when I run this, no problem.
38
So, uh, this kind of confirms my assumption that if you would like to create a set.
39
What you can do is put all of the elements that are in the set, uh, just in curly braces, like you would in an array, and you can initialize it that way,
40
or you can declare the set by simply doing set the type and then the name of the set.
41
And well, that works as well.
42
Anyways, let's add a few duplicate elements into this set
43
and let's iterate through this set and let's see what elements are actually in it.
44
So I'm going to say four, and I'm going to use kind of that iterator pattern we've seen here.
45
So I'm going to say auto S one, uh, is equal to, sorry, not S one auto I T R is equal to S one dot begin.
46
And then I will say, I T R does not equal S one dot end.
47
And then I will say plus plus I T R.
48
Now again, remember a set unordered collection of unique elements.
49
And well, that means we are not sure what order we're going to get elements in when we loop through the set.
50
And so this kind of means nothing when I write like this specific ordering of elements, just, just keep it in mind.
51
Okay.
52
That we don't know the order of elements in a set.
53
Then what I'm going to do is say, see out asterisk, ITR and, and L and let's see what we get.
54
All right.
55
So we get C and then we get D notice.
56
I didn't get CD CD.
57
I didn't get CCDD.
58
I only got one C and one D and that is
59
because when you insert an element into a set that already exists, it doesn't do anything because we only care if something exists or if it does not exist in a set, that is really the purpose of using it.
60
And if I decide to add maybe an a here and we run this now, notice we get a CD, even though a was at the very end of the set.
61
So we don't really know what order we're going to get elements in.
62
Although this might make you assume that you're going to get it in alphabetical order.
63
Anyways, that is a set.
64
So hopefully that's clear on the basics of a set.
65
And I'll kind of leave this here so
66
that we can see what happens as we insert and remove elements from the set.
67
So to insert something into the set, you can say S one dot insert, and then you can just add whatever you want.
68
So maybe we'll add a B or something.
69
We can run this.
70
Now we see all is good.
71
A B has been added in and kind of confirming our assumption that it's probably printing out alphabetical order.
72
Anyways, we can also erase or remove an element from the set.
73
So I believe it's actually a race.
74
Uh, let me look here and see if that is what it is.
75
Where's all these methods.
76
I have a list of all of the methods.
77
I think that a race race.
78
Yep.
79
A race looks good.
80
So we can erase maybe the key C or the elements C.
81
So when I do that, notice that now C is no longer printing out.
82
And so that was kind of the most basic aspects of a set.
83
That's how you insert.
84
That's how you erase.
85
And well, this is how you iterate through it.
86
Now, a few other things we can do.
87
We can check if an element is present and say, if, and we'll say S one dot fines, and we will look for some element.
88
Let's say we're looking for elements.
89
See, we can say, if that is equal to S one dot end.
90
That means the element does not exist.
91
So I can see out,
92
uh, could not find C and we can do an end L and then otherwise we can say C out and
93
found C exclamation point, same thing.
94
And L okay.
95
So let's run this now and notice that we're getting, could not find C ABD because well, C is not in the set.
96
And so this same way as in the map is telling us it's not there.
97
if we look for a B, uh, well actually we did see, so let's just not erase C now.
98
So if we don't erase C and we run this, it says found C it's telling us C is in the set.
99
Okay.
100
So that is really like, that's pretty much it for a set.
101
Now let's just run through a quick example here.
102
And just like I did with the map, I will create a string.
103
So let's say string test is equal to this is a test.
104
I J whatever, a bunch of random letters.
105
Okay.
106
Now we're going to do is say set char occurrence is equal to, and actually we can just do it like that.
107
We don't need to define or initialize it.
108
And instead of occurrence, let's just make it easier.
109
Let's just say exists.
110
Okay.
111
Now what I'm going to do is loop through the strings.
112
I'm going to say four, and we could use a string iterator here too, or we can just loop through it in the way we normally do.
113
So I'd say int I is equal to zero.
114
I is less than test dot dot length.
115
And then I plus plus, or again, just cause I think this is the proper way.
116
We'll go plus plus I, and then what I will do now is say exists dots insert, and we will first define the letter.
117
So we'll say char letter is equal to test at I now notice we don't have to do this.
118
I'm just doing this cause it's kind of good programming practice to define what it is you're actually inserting, uh, and kind of make it easier to read, but you could just put test I in here.
119
Like you could do test I without defining the variable first, but for me, I just like to define the variable.
120
So I'm going to say a letter and we'll insert that into the set.
121
So now what we've done is we've looped through the string.
122
We've inserted every single element in the string into the set.
123
Now I want to look at all of the unique elements in the set.
124
And so what I'll do is say four, and then I will loop through the set.
125
So I will say, uh, what is this going to be?
126
Auto I T R is equal to exists dot begin.
127
I'll then say, I TR does not equal exists dot end.
128
And then I will say plus plus I T R.
129
And then I will just, oops, see out an asterisk, I T R D reference and L.
130
Okay.
131
Let's run this.
132
And what we're getting now is all of the unique letters, including the space that is in this string.
133
Now it doesn't matter if I add like a ton more a's, we're not going to get another a here because well, it's a set, right?
134
And so if we add a, and again, it doesn't do anything because it's already in the set.
135
So there you go.
136
Now we can do actually a quick example here.
137
Again, this is kind of another thing you might see in a coding interview.
138
Speaking of coding interviews, if you guys want to prepare for them, you know what to use algo expert, which is a sponsor of this video.
139
And this series that have over 150 coding interview questions, many of which use sets, dictionaries, and a lot of things we're talking about here.
140
Check out algo expert from the link in the description
141
and use the code tech with Tim for a discount on the platform.
142
Anyways, good segue there.
143
What we're going to do now is I'm going to create another string.
144
I'm going to say string find is equal to, and then I'm going to say, let's say the word.
145
Hello.
146
Okay.
147
So let's say we were given a problem and we were said, okay, you're given this string called find, and you want to determine if all of the letters in this string are also present in this string.
148
So you want to see if all of the unique letters here are also present in this string right here.
149
There's a few different ways to go about doing this, but using a set, we can do this kind of elegantly.
150
We can create a set that stores all of the letters in this string that we're trying to look for.
151
Then what we can do is loop through this string.
152
We can remove all of the, uh, letters here from the set that contains the letters in this string.
153
And if at the end of going through this entire string, this set is empty.
154
That means that this, uh, first string contains all of the unique letters that are in this string.
155
Hopefully that kind of makes sense, but I'll just run you through it and we can see how that works as a good example.
156
So I'm going to say set, uh, we'll say find letters like that.
157
So now we need to go through and essentially add all of these elements into the set.
158
There's a few different ways to do this, just some shortcuts as well, but we're just going to do the long way because it's good practice.
159
I'm going to say four and I equals zero.
160
I is less than find dot length plus plus I will then say char letter is equal to,
161
and then this will be find at I, and then we will say find letters, oops, find letters dot insert.
162
And we will insert, uh, what is the letter letter like that?
163
Okay.
164
So now if we were to print out all of the letters in this set, we would get H E L and Oh, So now we want to go through here and see if this contains all of the same letters that are in here.
165
So what I'm going to do now is say four, and we'll say, um, I guess in I equals zero,
166
I is less than test dot length.
167
And I plus plus we'll say char letter is equal to test at index I,
168
and then what we will do is erase this from the sets.
169
We will say set dot erase and we'll erase letter.
170
Okay, great.
171
So then finally, what I'm going to do here is write an if statement, I'm going to say if, and don't worry, I'll go through all this code in a second.
172
I just want to type it out first.
173
I'll say, if the find letters dot size is greater than zero, then that means see out.
174
No, it does not have all letters.
175
I know it's not really great English, but you get the idea else we can see out.
176
Yes, it does.
177
Okay, great.
178
So anyways, let's just quickly run this actually.
179
And let me see if this works a warning character concept.
180
Oh, I forgot about this.
181
This needs to be double quotation marks.
182
My bad and missing template argument before dot.
183
Oh, I don't know why I called this set.
184
Uh, look at how many airs that I need a semi -colon there.
185
This isn't set.
186
This should be find letters.
187
Let's run this now.
188
And there we go.
189
No, it does not have all of the letters.
190
And so that is working as intended.
191
Okay.
192
Let me run through all this code.
193
I know I went through this pretty quickly.
194
So the idea is that we were asked, all right, we have this fine string.
195
We have this test string.
196
We want to see if all of the letters that are contained in fine unique letters, by the way, are also contained in the test string.
197
And so what we do is we first create a set, we call it find letters.
198
We then add all of the letters that are in side of this fine string to the set.
199
That's going to automatically handle not having like duplicates and anything for us, because if we add two L's, it's just going to be one L that remains in the set.
200
Then we go through the second string for all of its letters.
201
We remove them from this set, because then that means if we remove every single element from the set, this string had all of the elements that were in this string.
202
If it doesn't though, if this set does have at least one element, that means that we did not write, like we did not have all of the same letters.
203
And so in that case, we would print out, no, it does not have all the letters.
204
However, here, the letter that we're missing, I believe is an L, um, might be missing an O two.
205
Yeah, we are also missing an O but we have an E and we have an eight.
206
So now if I add an L and an O in here and we run this, we should see that it says, yes, it does.
207
It does have all of these same letters.
208
We can even make this a bit simpler.
209
We can make this string say like this, like hello with only one L and we'll see, we still get, yes, it does.
210
Because even though it doesn't have two L's that doesn't matter.
211
Although if we remove the O it's going to say, no, it does not have all the letters.
212
So this is an example of where you might use a set.
213
Of course, there's a lot of other examples, but hopefully that kind of brought everything together for you.
214
And again, I'm trying to do some longer examples here
215
so we can see how we actually solve problems with all of these tools that we now have.
216
Anyways, if you guys enjoyed, make sure leave a like, subscribe to the channel.
217
I will see you in another YouTube video.

下載應用

流利口語所需的一切

AI 發音評分逐句打分
音標練習掌握每個音
詞彙學習建立你的詞庫
詞彙遊戲邊玩邊學

同頻道

FOR YOU

推薦影片

本課的詞彙與口語要點

這段影片共有 217 個句子、2904 個單詞可供跟讀。 講話部分時長為 13:25。 說話人語速較快,每分鐘約 217 個詞,會出現較多連讀和弱讀。 84% 的單詞屬於英語最常用的 3,000 詞,其餘的詞建議在練習前先學一下。

影片中的重點詞彙

影片中 15 個值得學習的單詞,附發音和釋義:

單詞發音釋義
element 名詞/ˈɛləmənt/要素
string 名詞/stɹɪŋ/線 /線
insert 動詞/ɪnˈsɝt/插入, 插上
litter 名詞/ˈlɪt.ɚ/墊料
erase 動詞/ɪˈɹeɪs/拭除, 抹去
loop 名詞/luːp/圈, 環 /環
define 動詞/dɪˈfaɪn/界定
instantly 副詞/ˈɪnstəntli/立即
automobile 名詞/ˈɔː.tə.məˌbil/汽車 /汽車, 車 /車
confirm 動詞/kənˈfɝm/證實 /證實, 確認 /確認
assumption 名詞/əˈsʌm(p).ʃ(ə)n/假設 /假設
algorithm 名詞/ˈælɡəɹɪðm̩/演算法, 演算法
tutorial 名詞/ˌtjuːˈtɔːɹɪəl/輔導 /輔導, 教程
occurrence 名詞/əˈkɜɹ.əns/事件
oops/ups/哎呀, 啊哈

影片中出現的短語動詞

單詞釋義
go through 動詞通過 /通過
check out 動詞退房

影片中的語法

說話人最常用的結構,並附上影片中的原話:

結構影片中的用法
被動語態 be + 過去分詞 — 強調發生了什麼,而不是誰做的is put · has been added · were given
現在完成時 have/has + 過去分詞 — 過去發生但與現在仍有關聯的事we've seen · has been added · we've done

需要注意的發音

說話人用了 80 次縮略和弱讀形式,例如 I'm, we're, we'll。請按聽到的簡短形式來說。

  • “sh” 和 “zh” 音: assumption /əˈsʌm(p).ʃ(ə)n/, essentially /ɪˈsɛnʃəli/, sheet /ʃit/, potentially /pəˈtɛnʃ(ə)li/, implementation /ˌɪmplɪmənˈteɪʃən/
  • 長單詞——注意重音位置: automobile /ˈɔː.tə.məˌbil/, essentially /ɪˈsɛnʃəli/, potentially /pəˈtɛnʃ(ə)li/, implementation /ˌɪmplɪmənˈteɪʃən/, dictionary /ˈdɪk.ʃəˌnɛ.ɹi/

如何用這段影片練習

  1. 先完整聽一遍影片,不要開口,記下不認識的單詞。
  2. 先用 0.75 倍速逐句跟讀,熟練之後再回到正常速度。
  3. 錄下自己的聲音並與原聲對比,特別注意 element, string, insert 這類單詞。

什麼是跟讀法?

跟讀法 (Shadowing) 是一種有科學依據的語言學習技巧,最初開發用於專業口譯員的培訓,並由多語言者Alexander Arguelles博士普及。這個方法簡單而強大:您在聽英語母語原聲的同時立即大聲重複——就像是一個延遲1-2秒緊跟說話者的影子。與被動聽力或語法練習不同,跟讀法強迫您的大腦和口腔肌肉同時處理並模模擬實的講話模式。研究表明它能顯著提高發音準確性,語調,節奏,連讀,聽力理解和口語流利度——使其成為雅思口語備考和真實英語交流最有效的方法之一。

影子跟讀法: 閱讀完整分步指南 →