Welcome back, I hope you had fun doing that challenge.
I'm in the Section 20 workspace and the challenge 3 - Solution.
This is an example of explaining something is going to be much
easier than explaining the actual problem to begin with.
This code is so simple.
Hopefully, did it similar to the way I did it.
Let the STL do its thing.
The STL is super powerful.
Let's take a look at part one.
Here's the solution for part one.
And let's walk through this one line at a time.
We've got our map here called words, and it's a map of
string, integer key value pairs.
I've got a line that's what I'm going to read the line from a file into.
I've got a word and this is how I'm going to process my words.
Here's my file.
My file is words.txt.
If I can't open it, I'm down here.
And I'm out.
If I can open it then what I want to do is I want to process the
words from the line I just read.
And that's not so easy to do there, it's much easier to do
if you use a string stream.
And if you look at the I O videos I talked about string
streams in that video.
So, in this case, I'm creating a string stream called ss over line.
So, remember we just read a line, let's say we read, and this is a test.
And we don't want to deal with new lines.
We don't want to deal with any of that.
So, we just read that line.
And then what we're going to do is we're going to create the
string stream from that line.
So, that's going to put in that buffer.
So, now I can read words from the string-stream and it
makes it really, really, easy.
So, you can see right there while I'm going to extract a
word from the string-stream.
So, I'm going to extract this word, and then this word, and then
this word, and then this word.
For each one of those words, I'm going to pass it into clean-string.
So, I can get rid of the periods at the end.
And suppose I have something like commas and so forth.
It'll get rid of that.
So, in this case, I will end up with a clean word and all I have to do
is just say words, that's my map.
I'm using the subscript operator right, so in this case it's
equivalent to saying something like this, words, this.
Let's assume that we're working with that guy right there.
That is my key if it doesn't exist, the value is going to
be added with the key, right.
So, in this case, that's my key.
What do I want to do?
I want to increment the value at that key, simple as that.
So, now I just put that word in once.
The next time I see that same word, it will already be there.
So, it's going to increment this to 2 and so forth.
That's it, that's the code.
It's really one line right there that does all the good work.
I commented this line out right here.
And you can use that for debugging purposes if you'd like to make
sure that you're reading these lines correctly from the file.
That's it, that's part one.
I told you it's pretty simple.
I'm closing the file and then I'm calling display words and display
words will come up here for part one.
And it's going to call this function right there.
And all it's doing is it's just doing a lot of formatting
which is just to look nice.
But for each pair from the words map, I'm dealing with
the first and the second.
The first is the key.
The second is the value.
So, this is just displaying Aunt 8, Frank 3 and so forth.
Okay, let's look at part two.
Here's part two.
Very, very, similar the idea.
We've got our map is words, and this is a little bit different
right because I've got a key here.
And my value is not an Int.
In this case, it's a set OF Ints.
The whole point of this exercise was to show you how
you can build these things up.
I mean it's really, really, powerful.
Many times, we need to solve a problem like this.
And you'll see some programmers creating classes for words, and
for lines, and you create a new class that's going to model the
occurrences of a line you really don't need to do any of that
because the STL does this for you.
This is a real powerful concept.
I mean I'm already modeling key value pairs right here, why do I need to
create another class to do this when I just need to solve this problem.
Okay, so in this case, I've got the same variables.
I've got the same file.
I'm opening my file right here on line 88.
I need to have a running count of line numbers because I need my line number.
So, I'm just starting line numbers at zero.
I mean I haven't really read anything yet.
Now what I'm going to do is I'm going to read, let me clear this a little bit,
I'm going to read the line into this variable from the file.
And now I want to increment line number because I just read a line.
So, my number now would be 1.
Again, I'm using the string-stream just to process words, cleaning the string,
and this is the only difference here.
So, now I'm doing something like words, again let's assume we're using that
same that word we just read this.
This piece right here, that's the key.
This will return the pair and I can access the value right there.
So, what am I doing?
I'm inserting line number into the value which is that guy.
Right! So, I'm using the sets insert method which is really, really, easy.
The first time through the line number is the line number, I'm
just inserting that key value pair.
The next time it's just updating that value.
And that's it, let's run this one last time.
Here's my output.
And you can double check it in the code light editor but it's
really, really, straightforward.
Here's part one where you've got your word and the number of times
it occurs, and down here is part two where we've got the word and the
line numbers that it occurred on.
Okay, so, hopefully, you had fun with this one.
And hopefully, you can see how easy it is now.
So, in the next video we'll talk about the container adapters,
stack and queue, and we'll have another challenge for those.