1 00:00:05,500 --> 00:00:09,100 Welcome back to the challenge 1 solution for section 17. 2 00:00:09,900 --> 00:00:12,700 What I'll do now is I'll write these three functions, 3 00:00:12,700 --> 00:00:15,360 and we'll do it together and we'll explain it as we go. 4 00:00:15,360 --> 00:00:20,350 The first one is the make function. That's the one that creates that unique pointer. 5 00:00:20,350 --> 00:00:24,250 And the way we do that is we use make unique. Now obviously, we need to return it. 6 00:00:24,250 --> 00:00:27,750 So we'll say return std make unique. 7 00:00:28,850 --> 00:00:32,450 And what are we making. Well, we're making a 8 00:00:32,950 --> 00:00:33,750 vector. 9 00:00:35,750 --> 00:00:38,350 And the syntax is going to look kind of weird here, but we'll -- 10 00:00:38,650 --> 00:00:41,250 we just have to deal with it because that's just the way it is. 11 00:00:41,250 --> 00:00:45,130 And it's not that hard once you break things up together, as I said. So we're returning 12 00:00:45,130 --> 00:00:49,490 a unique pointer to a vector, right. And that vector is of 13 00:00:49,490 --> 00:00:51,090 std shared pointers 14 00:00:53,590 --> 00:00:55,490 to test objects. 15 00:00:55,990 --> 00:00:59,190 And we'll have three greater than signs here to close that up. 16 00:00:59,190 --> 00:01:02,550 And then what we need to do is we need to call the constructor for that vector. 17 00:01:02,950 --> 00:01:05,150 That's it. That's all we really need to do here. 18 00:01:05,150 --> 00:01:08,750 Now we'll clean this up in a second. We'll make this even better than it is here, 19 00:01:08,750 --> 00:01:12,250 but that's one liner. That's all it's doing. We have to make that unique pointer. 20 00:01:12,250 --> 00:01:14,550 Remember, down here on line 89, 21 00:01:14,950 --> 00:01:19,650 we've got a constructed unique pointer, right. But it's not pointing anywhere yet. 22 00:01:19,650 --> 00:01:22,850 That's what we're doing here now we're actually creating that vector. 23 00:01:23,400 --> 00:01:26,000 All right. So now there's the fill. How do we do the fill. 24 00:01:26,000 --> 00:01:28,360 Well, the fill expects that vector. 25 00:01:28,360 --> 00:01:32,860 So it's pretty straightforward. I just need to read that integer from the user for each iteration, 26 00:01:32,860 --> 00:01:37,260 right. So we need a temporary variable here. Let's just call that temp. That's what we're going to read into. 27 00:01:37,760 --> 00:01:40,060 And now we're going to loop. How many times are we going to loop. 28 00:01:40,060 --> 00:01:44,060 We're going to loop num times. So however many numbers the user wants to enter. 29 00:01:44,060 --> 00:01:48,560 So we'll say for int I equals 1, we'll start at 1. 30 00:01:48,560 --> 00:01:51,220 And we'll go I less than or equal to num. 31 00:01:51,620 --> 00:01:53,620 And we'll increment I each time. 32 00:01:54,420 --> 00:01:55,520 So that's my loop. 33 00:01:55,770 --> 00:02:00,470 We're going to use a for loop here because we know exactly how many times we need to loop, we need to loop num times. 34 00:02:01,470 --> 00:02:04,770 So what do we do here. Let's prompt the user. So we'll say std cout. 35 00:02:06,970 --> 00:02:08,850 And we'll ask the user to enter 36 00:02:10,350 --> 00:02:11,250 data point, 37 00:02:13,350 --> 00:02:17,500 and we'll just give them a little number here so that's easier for them to understand 38 00:02:17,500 --> 00:02:21,600 which one they're putting in. And we'll just surround that in brackets like that. 39 00:02:23,590 --> 00:02:26,090 That's it. And then what we'll do is we'll read 40 00:02:27,090 --> 00:02:30,340 from cin into temp. So we're going to do that 41 00:02:30,340 --> 00:02:33,340 three times, five times, ten times, however many times they want. 42 00:02:33,590 --> 00:02:37,090 Now what we can do is we can create our shared pointer. 43 00:02:37,090 --> 00:02:39,590 We know the data that's going to be in the test object, 44 00:02:39,590 --> 00:02:42,190 right. That's this guy right here, temp, we just read it. 45 00:02:42,190 --> 00:02:47,180 So we can do this in a couple of different ways. Let's do it two ways. Let's do it in two steps, and then we'll do it in one step. 46 00:02:47,180 --> 00:02:51,680 So what we need to do is we need to have a variable here. And it's going to be a shared pointer 47 00:02:52,780 --> 00:02:56,080 to a temp object. So my template parameter is temp. 48 00:02:57,980 --> 00:03:00,340 And what do we call this, let's just call it ptr. 49 00:03:00,340 --> 00:03:04,000 This variable name could be anything, and we're going to use std make shared, 50 00:03:05,500 --> 00:03:09,400 again, test. And what are we going to construct it with. 51 00:03:09,400 --> 00:03:13,900 Well, we're going to construct it with data or temp whatever the user entered. 52 00:03:14,700 --> 00:03:18,300 Now that we've got that pointer, remember, we have a vector 53 00:03:18,660 --> 00:03:20,020 of shared pointers. 54 00:03:20,020 --> 00:03:25,520 We just created the shared pointer. So all we need to do now is just say vec.pushback, 55 00:03:26,520 --> 00:03:28,520 and we're going to push back the pointer. 56 00:03:29,120 --> 00:03:30,320 That's it. We're done. 57 00:03:30,320 --> 00:03:34,320 So that's exactly what's going on here. And we're going to loop that many times 58 00:03:34,320 --> 00:03:37,310 for each iteration, create that shared pointer, 59 00:03:37,310 --> 00:03:40,510 instantiate that test object, and that's it. 60 00:03:40,510 --> 00:03:43,210 Now rather than do it in two steps like this, 61 00:03:43,210 --> 00:03:47,460 let's just do it in one step because it's much more efficient to use, move semantics. 62 00:03:47,460 --> 00:03:49,660 So let's just say vec.pushback. 63 00:03:50,160 --> 00:03:52,960 And right in here, we'll just say std make shared, 64 00:03:54,460 --> 00:03:58,660 right. So that's going to call -- that's going to create that r-value, and the r-value ref will be moved over. 65 00:03:58,660 --> 00:03:59,660 So test 66 00:04:00,860 --> 00:04:02,860 and temp. 67 00:04:03,660 --> 00:04:06,620 All right. So again, one simple statement. 68 00:04:07,220 --> 00:04:10,450 And now the last thing we need to do is display this vector. 69 00:04:10,450 --> 00:04:13,770 So I've already got the header information here with the two couts. 70 00:04:13,770 --> 00:04:17,570 And what do we do here. Well, let's just use a real simple range-based for loop. 71 00:04:17,570 --> 00:04:21,570 So we'll say for const auto, 72 00:04:22,170 --> 00:04:23,570 we'll use a ref 73 00:04:24,370 --> 00:04:27,370 and we'll call it pointer, and we're going to iterate through that vector. 74 00:04:27,870 --> 00:04:31,670 And for each iteration, all I want to do is just say std cout. 75 00:04:32,170 --> 00:04:36,270 And we want to print the pointer's data, right. So we need to de-reference 76 00:04:36,270 --> 00:04:38,770 that pointer and get its data, 77 00:04:41,100 --> 00:04:44,100 something like that and then an endline, 78 00:04:46,300 --> 00:04:49,300 and that should do it. Let's compile this. 79 00:04:51,000 --> 00:04:53,600 And there's our run. How many points do you want to enter. 80 00:04:53,600 --> 00:04:55,600 Let's say I want to enter 10 data points. 81 00:04:56,000 --> 00:04:57,200 And they'll be, 82 00:04:57,200 --> 00:05:02,100 let's say, 2 4 6 8 10 12 14 16 83 00:05:02,760 --> 00:05:04,760 18 and the last one is 20. 84 00:05:05,460 --> 00:05:07,460 There we go. We're displaying our vector data. 85 00:05:07,460 --> 00:05:12,060 And then you can see that our data is being cleaned up automatically. Our test objects are being destroyed. 86 00:05:12,260 --> 00:05:13,060 Pretty cool. 87 00:05:13,060 --> 00:05:16,320 And you can see that there wasn't a lot of code to write it all. 88 00:05:16,320 --> 00:05:18,980 Now let's clean this up just a little bit here. 89 00:05:19,680 --> 00:05:23,340 Remember, we can allow the compiler to deduce our types for us. 90 00:05:23,340 --> 00:05:25,540 So we can really clean this up a little bit. 91 00:05:25,540 --> 00:05:29,440 Since the compiler knows exactly what we're returning right here, 92 00:05:29,640 --> 00:05:32,940 we can simply replace this whole piece right here to auto. 93 00:05:33,940 --> 00:05:36,090 We'll just say auto right here. Let the compiler 94 00:05:36,090 --> 00:05:39,690 figure out what the type of that return value is and it knows it based on this. 95 00:05:40,190 --> 00:05:42,490 And we can also change it up here in the function prototype, 96 00:05:43,040 --> 00:05:47,040 get rid of all that and just replace that with auto. 97 00:05:47,040 --> 00:05:50,640 So we can replace this entire return type right here with auto. 98 00:05:51,300 --> 00:05:55,660 And now if we build again, we get the same compile, we get the same run. 99 00:05:55,860 --> 00:06:00,060 So let's put three data items in here, and we'll say 5 6 and 7. 100 00:06:00,260 --> 00:06:04,660 There we go. We're displaying our data, our vector 5 6 7, and we're destroying those objects. 101 00:06:04,660 --> 00:06:08,020 Pretty simple, no news, no deletes, not worrying about 102 00:06:08,020 --> 00:06:09,380 who needs to delete what. 103 00:06:09,780 --> 00:06:12,480 And that's it. That's the solution to this challenge. 104 00:06:12,480 --> 00:06:16,640 I hope yours was close. If you had issues with the pointers and figuring out how they work, 105 00:06:16,640 --> 00:06:20,340 don't worry, just keep at it. It's just a matter of practice and understanding 106 00:06:20,340 --> 00:06:23,340 conceptually what's going on and what's pointing to what, 107 00:06:23,340 --> 00:06:27,540 de-referencing what to get what, and it's just a little process that takes time to learn.