Shadowing Practice: Learn C++ With Me #19 - Sets - Learn English Speaking with Video

Les maken...
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.

Woordenschat en spreektips bij deze les

Deze video bevat 217 zinnen en 2904 woorden om na te spreken. Het gesproken deel duurt 13:25. De spreker praat snel, ongeveer 217 woorden per minuut, dus reken op verbonden en ingeslikte klanken. 84% van de woorden hoort bij de 3.000 meest gebruikte Engelse woorden; de rest kun je beter vooraf bekijken.

Belangrijke woorden in deze video

15 woorden uit de video die het leren waard zijn, met uitspraak en betekenis:

WoordUitspraakBetekenis
element zelfstandig naamwoord/ˈɛləmənt/element
string zelfstandig naamwoord/stɹɪŋ/draad, koord
insert werkwoord/ɪnˈsɝt/invoegen
litter zelfstandig naamwoord/ˈlɪt.ɚ/strooisel
erase werkwoord/ɪˈɹeɪs/uitwissen, uitgummen
loop zelfstandig naamwoord/luːp/lus, strop
define werkwoord/dɪˈfaɪn/bepalen, determineren
automobile zelfstandig naamwoord/ˈɔː.tə.məˌbil/automobiel, auto
confirm werkwoord/kənˈfɝm/bevestigen
variable bijvoeglijk naamwoord[ˈvæɹ.i.ə.bl̩]veranderlijk, variabel
assumption zelfstandig naamwoord/əˈsʌm(p).ʃ(ə)n/op zich nemen
algorithm zelfstandig naamwoord/ˈælɡəɹɪðm̩/algoritme
tutorial zelfstandig naamwoord/ˌtjuːˈtɔːɹɪəl/snelcursus, tutorial
occurrence zelfstandig naamwoord/əˈkɜɹ.əns/voorval
oops/ups/oeps, oei

Phrasal verbs die je zult horen

WoordBetekenis
check out werkwoordonderzoeken

Grammatica in deze video

De structuren die de spreker het meest gebruikt, met de exacte woorden uit de video:

StructuurIn de video
Lijdende vorm be + voltooid deelwoord — het gaat om wat er gebeurt, niet om wie het doetis put · has been added · were given
Present perfect have/has + voltooid deelwoord — iets uit het verleden dat nu nog teltwe've seen · has been added · we've done

Uitspraak om op te letten

De spreker gebruikt 80 samentrekkingen en verkorte vormen, zoals I'm, we're, we'll. Spreek ze kort uit, zoals je ze hoort.

  • De klanken “sh” en “zh”: assumption /əˈsʌm(p).ʃ(ə)n/, essentially /ɪˈsɛnʃəli/, sheet /ʃit/, potentially /pəˈtɛnʃ(ə)li/, implementation /ˌɪmplɪmənˈteɪʃən/
  • Lange woorden — let op de klemtoon: automobile /ˈɔː.tə.məˌbil/, essentially /ɪˈsɛnʃəli/, potentially /pəˈtɛnʃ(ə)li/, implementation /ˌɪmplɪmənˈteɪʃən/, dictionary /ˈdɪk.ʃəˌnɛ.ɹi/

Zo oefen je met deze video

  1. Luister de hele video één keer zonder te spreken en noteer de woorden die je niet kent.
  2. Begin op 0,75× snelheid, spreek zin voor zin na en ga terug naar normale snelheid zodra het makkelijk gaat.
  3. Neem jezelf op en vergelijk met het origineel; let daarbij op woorden als element, string, insert.

Wat is de Shadowing-techniek?

Shadowing is een wetenschappelijk onderbouwde taalleermethode die oorspronkelijk is ontwikkeld voor professionele tolkentraining en gepopulariseerd door polyglot Dr. Alexander Arguelles. De methode is eenvoudig maar krachtig: je luistert naar native Engelse audio en herhaalt het onmiddellijk hardop — als een schaduw die de spreker volgt met slechts 1–2 seconden vertraging. In tegenstelling tot passief luisteren of grammaticadrills, dwingt shadowing je hersenen en mondspieren om echte spraakpatronen tegelijkertijd te verwerken en te reproduceren. Onderzoek toont aan dat het de uitspraaknauwkeurigheid, intonatie, ritme, verbonden spraak, luisterbegrip en spreekvaardigheid aanzienlijk verbetert — waardoor het een van de meest effectieve methoden is voor IELTS Speaking-voorbereiding en echte Engelse communicatie.

Shadowing-techniek: lees de volledige stap-voor-stap-gids →