1 00:00:05,480 --> 00:00:08,620 Welcome back, I hope you had fun doing that challenge. 2 00:00:09,010 --> 00:00:12,640 I'm in the Section 20 workspace and the challenge 3 - Solution. 3 00:00:13,299 --> 00:00:16,289 This is an example of explaining something is going to be much 4 00:00:16,289 --> 00:00:18,669 easier than explaining the actual problem to begin with. 5 00:00:18,850 --> 00:00:20,170 This code is so simple. 6 00:00:20,230 --> 00:00:23,110 Hopefully, did it similar to the way I did it. 7 00:00:23,630 --> 00:00:25,440 Let the STL do its thing. 8 00:00:25,440 --> 00:00:26,869 The STL is super powerful. 9 00:00:27,679 --> 00:00:29,120 Let's take a look at part one. 10 00:00:30,610 --> 00:00:32,610 Here's the solution for part one. 11 00:00:33,179 --> 00:00:35,309 And let's walk through this one line at a time. 12 00:00:35,600 --> 00:00:39,790 We've got our map here called words, and it's a map of 13 00:00:39,800 --> 00:00:42,150 string, integer key value pairs. 14 00:00:42,700 --> 00:00:46,230 I've got a line that's what I'm going to read the line from a file into. 15 00:00:47,050 --> 00:00:49,599 I've got a word and this is how I'm going to process my words. 16 00:00:49,799 --> 00:00:50,939 Here's my file. 17 00:00:51,309 --> 00:00:53,059 My file is words.txt. 18 00:00:53,510 --> 00:00:55,480 If I can't open it, I'm down here. 19 00:00:55,780 --> 00:00:56,510 And I'm out. 20 00:00:57,490 --> 00:01:00,650 If I can open it then what I want to do is I want to process the 21 00:01:00,650 --> 00:01:02,349 words from the line I just read. 22 00:01:02,559 --> 00:01:05,180 And that's not so easy to do there, it's much easier to do 23 00:01:05,180 --> 00:01:06,470 if you use a string stream. 24 00:01:06,750 --> 00:01:09,479 And if you look at the I O videos I talked about string 25 00:01:09,480 --> 00:01:10,560 streams in that video. 26 00:01:10,910 --> 00:01:14,920 So, in this case, I'm creating a string stream called ss over line. 27 00:01:15,450 --> 00:01:19,650 So, remember we just read a line, let's say we read, and this is a test. 28 00:01:22,380 --> 00:01:23,920 And we don't want to deal with new lines. 29 00:01:23,920 --> 00:01:24,969 We don't want to deal with any of that. 30 00:01:24,969 --> 00:01:26,150 So, we just read that line. 31 00:01:26,309 --> 00:01:27,830 And then what we're going to do is we're going to create the 32 00:01:27,830 --> 00:01:29,299 string stream from that line. 33 00:01:29,299 --> 00:01:30,610 So, that's going to put in that buffer. 34 00:01:30,770 --> 00:01:34,810 So, now I can read words from the string-stream and it 35 00:01:34,810 --> 00:01:36,230 makes it really, really, easy. 36 00:01:36,340 --> 00:01:40,480 So, you can see right there while I'm going to extract a 37 00:01:40,480 --> 00:01:42,230 word from the string-stream. 38 00:01:42,230 --> 00:01:44,590 So, I'm going to extract this word, and then this word, and then 39 00:01:44,590 --> 00:01:45,699 this word, and then this word. 40 00:01:46,969 --> 00:01:51,100 For each one of those words, I'm going to pass it into clean-string. 41 00:01:51,100 --> 00:01:52,880 So, I can get rid of the periods at the end. 42 00:01:52,880 --> 00:01:55,309 And suppose I have something like commas and so forth. 43 00:01:55,620 --> 00:01:56,630 It'll get rid of that. 44 00:01:57,059 --> 00:02:01,250 So, in this case, I will end up with a clean word and all I have to do 45 00:02:01,250 --> 00:02:03,360 is just say words, that's my map. 46 00:02:04,150 --> 00:02:07,379 I'm using the subscript operator right, so in this case it's 47 00:02:07,390 --> 00:02:11,799 equivalent to saying something like this, words, this. 48 00:02:12,850 --> 00:02:15,110 Let's assume that we're working with that guy right there. 49 00:02:16,030 --> 00:02:19,020 That is my key if it doesn't exist, the value is going to 50 00:02:19,020 --> 00:02:20,300 be added with the key, right. 51 00:02:20,309 --> 00:02:21,810 So, in this case, that's my key. 52 00:02:22,039 --> 00:02:22,969 What do I want to do? 53 00:02:23,299 --> 00:02:27,220 I want to increment the value at that key, simple as that. 54 00:02:27,220 --> 00:02:29,130 So, now I just put that word in once. 55 00:02:29,550 --> 00:02:33,350 The next time I see that same word, it will already be there. 56 00:02:33,350 --> 00:02:36,020 So, it's going to increment this to 2 and so forth. 57 00:02:36,059 --> 00:02:37,370 That's it, that's the code. 58 00:02:37,570 --> 00:02:41,830 It's really one line right there that does all the good work. 59 00:02:42,549 --> 00:02:44,429 I commented this line out right here. 60 00:02:44,530 --> 00:02:47,209 And you can use that for debugging purposes if you'd like to make 61 00:02:47,210 --> 00:02:49,500 sure that you're reading these lines correctly from the file. 62 00:02:50,380 --> 00:02:51,569 That's it, that's part one. 63 00:02:51,570 --> 00:02:52,760 I told you it's pretty simple. 64 00:02:52,830 --> 00:02:56,790 I'm closing the file and then I'm calling display words and display 65 00:02:56,790 --> 00:02:59,729 words will come up here for part one. 66 00:02:59,730 --> 00:03:02,990 And it's going to call this function right there. 67 00:03:03,810 --> 00:03:07,590 And all it's doing is it's just doing a lot of formatting 68 00:03:07,600 --> 00:03:08,870 which is just to look nice. 69 00:03:08,940 --> 00:03:16,670 But for each pair from the words map, I'm dealing with 70 00:03:16,740 --> 00:03:17,949 the first and the second. 71 00:03:17,959 --> 00:03:19,179 The first is the key. 72 00:03:20,700 --> 00:03:22,060 The second is the value. 73 00:03:23,020 --> 00:03:26,930 So, this is just displaying Aunt 8, Frank 3 and so forth. 74 00:03:27,980 --> 00:03:29,920 Okay, let's look at part two. 75 00:03:31,880 --> 00:03:32,560 Here's part two. 76 00:03:32,910 --> 00:03:34,899 Very, very, similar the idea. 77 00:03:35,109 --> 00:03:38,200 We've got our map is words, and this is a little bit different 78 00:03:38,320 --> 00:03:39,840 right because I've got a key here. 79 00:03:40,990 --> 00:03:43,410 And my value is not an Int. 80 00:03:43,450 --> 00:03:45,100 In this case, it's a set OF Ints. 81 00:03:45,530 --> 00:03:48,660 The whole point of this exercise was to show you how 82 00:03:48,660 --> 00:03:50,040 you can build these things up. 83 00:03:50,049 --> 00:03:51,629 I mean it's really, really, powerful. 84 00:03:51,920 --> 00:03:54,439 Many times, we need to solve a problem like this. 85 00:03:55,080 --> 00:03:58,959 And you'll see some programmers creating classes for words, and 86 00:03:58,960 --> 00:04:01,630 for lines, and you create a new class that's going to model the 87 00:04:01,630 --> 00:04:04,480 occurrences of a line you really don't need to do any of that 88 00:04:04,480 --> 00:04:06,220 because the STL does this for you. 89 00:04:06,460 --> 00:04:08,229 This is a real powerful concept. 90 00:04:08,240 --> 00:04:12,489 I mean I'm already modeling key value pairs right here, why do I need to 91 00:04:12,500 --> 00:04:15,859 create another class to do this when I just need to solve this problem. 92 00:04:16,220 --> 00:04:18,959 Okay, so in this case, I've got the same variables. 93 00:04:18,959 --> 00:04:20,149 I've got the same file. 94 00:04:20,149 --> 00:04:22,489 I'm opening my file right here on line 88. 95 00:04:23,520 --> 00:04:27,229 I need to have a running count of line numbers because I need my line number. 96 00:04:27,240 --> 00:04:29,120 So, I'm just starting line numbers at zero. 97 00:04:29,150 --> 00:04:30,740 I mean I haven't really read anything yet. 98 00:04:31,680 --> 00:04:35,030 Now what I'm going to do is I'm going to read, let me clear this a little bit, 99 00:04:35,030 --> 00:04:38,150 I'm going to read the line into this variable from the file. 100 00:04:39,099 --> 00:04:42,020 And now I want to increment line number because I just read a line. 101 00:04:42,020 --> 00:04:43,540 So, my number now would be 1. 102 00:04:44,130 --> 00:04:49,740 Again, I'm using the string-stream just to process words, cleaning the string, 103 00:04:50,100 --> 00:04:51,640 and this is the only difference here. 104 00:04:51,640 --> 00:04:55,770 So, now I'm doing something like words, again let's assume we're using that 105 00:04:55,770 --> 00:04:58,929 same that word we just read this. 106 00:05:00,349 --> 00:05:03,880 This piece right here, that's the key. 107 00:05:04,119 --> 00:05:09,100 This will return the pair and I can access the value right there. 108 00:05:09,280 --> 00:05:10,180 So, what am I doing? 109 00:05:10,550 --> 00:05:16,690 I'm inserting line number into the value which is that guy. 110 00:05:17,290 --> 00:05:21,220 Right! So, I'm using the sets insert method which is really, really, easy. 111 00:05:21,969 --> 00:05:24,260 The first time through the line number is the line number, I'm 112 00:05:24,260 --> 00:05:26,000 just inserting that key value pair. 113 00:05:26,000 --> 00:05:29,190 The next time it's just updating that value. 114 00:05:30,310 --> 00:05:33,190 And that's it, let's run this one last time. 115 00:05:36,590 --> 00:05:37,800 Here's my output. 116 00:05:39,049 --> 00:05:41,240 And you can double check it in the code light editor but it's 117 00:05:41,240 --> 00:05:42,629 really, really, straightforward. 118 00:05:42,950 --> 00:05:46,320 Here's part one where you've got your word and the number of times 119 00:05:46,320 --> 00:05:51,450 it occurs, and down here is part two where we've got the word and the 120 00:05:51,450 --> 00:05:52,680 line numbers that it occurred on. 121 00:05:54,270 --> 00:05:56,180 Okay, so, hopefully, you had fun with this one. 122 00:05:56,190 --> 00:05:57,930 And hopefully, you can see how easy it is now. 123 00:05:58,420 --> 00:06:01,240 So, in the next video we'll talk about the container adapters, 124 00:06:01,560 --> 00:06:04,150 stack and queue, and we'll have another challenge for those.