Welcome back to the challenge 1 solution for section 17.
What I'll do now is I'll write these three functions,
and we'll do it together and we'll explain it as we go.
The first one is the make function. That's the one that creates that unique pointer.
And the way we do that is we use make unique. Now obviously, we need to return it.
So we'll say return std make unique.
And what are we making. Well, we're making a
vector.
And the syntax is going to look kind of weird here, but we'll --
we just have to deal with it because that's just the way it is.
And it's not that hard once you break things up together, as I said. So we're returning
a unique pointer to a vector, right. And that vector is of
std shared pointers
to test objects.
And we'll have three greater than signs here to close that up.
And then what we need to do is we need to call the constructor for that vector.
That's it. That's all we really need to do here.
Now we'll clean this up in a second. We'll make this even better than it is here,
but that's one liner. That's all it's doing. We have to make that unique pointer.
Remember, down here on line 89,
we've got a constructed unique pointer, right. But it's not pointing anywhere yet.
That's what we're doing here now we're actually creating that vector.
All right. So now there's the fill. How do we do the fill.
Well, the fill expects that vector.
So it's pretty straightforward. I just need to read that integer from the user for each iteration,
right. So we need a temporary variable here. Let's just call that temp. That's what we're going to read into.
And now we're going to loop. How many times are we going to loop.
We're going to loop num times. So however many numbers the user wants to enter.
So we'll say for int I equals 1, we'll start at 1.
And we'll go I less than or equal to num.
And we'll increment I each time.
So that's my loop.
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.
So what do we do here. Let's prompt the user. So we'll say std cout.
And we'll ask the user to enter
data point,
and we'll just give them a little number here so that's easier for them to understand
which one they're putting in. And we'll just surround that in brackets like that.
That's it. And then what we'll do is we'll read
from cin into temp. So we're going to do that
three times, five times, ten times, however many times they want.
Now what we can do is we can create our shared pointer.
We know the data that's going to be in the test object,
right. That's this guy right here, temp, we just read it.
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.
So what we need to do is we need to have a variable here. And it's going to be a shared pointer
to a temp object. So my template parameter is temp.
And what do we call this, let's just call it ptr.
This variable name could be anything, and we're going to use std make shared,
again, test. And what are we going to construct it with.
Well, we're going to construct it with data or temp whatever the user entered.
Now that we've got that pointer, remember, we have a vector
of shared pointers.
We just created the shared pointer. So all we need to do now is just say vec.pushback,
and we're going to push back the pointer.
That's it. We're done.
So that's exactly what's going on here. And we're going to loop that many times
for each iteration, create that shared pointer,
instantiate that test object, and that's it.
Now rather than do it in two steps like this,
let's just do it in one step because it's much more efficient to use, move semantics.
So let's just say vec.pushback.
And right in here, we'll just say std make shared,
right. So that's going to call -- that's going to create that r-value, and the r-value ref will be moved over.
So test
and temp.
All right. So again, one simple statement.
And now the last thing we need to do is display this vector.
So I've already got the header information here with the two couts.
And what do we do here. Well, let's just use a real simple range-based for loop.
So we'll say for const auto,
we'll use a ref
and we'll call it pointer, and we're going to iterate through that vector.
And for each iteration, all I want to do is just say std cout.
And we want to print the pointer's data, right. So we need to de-reference
that pointer and get its data,
something like that and then an endline,
and that should do it. Let's compile this.
And there's our run. How many points do you want to enter.
Let's say I want to enter 10 data points.
And they'll be,
let's say, 2 4 6 8 10 12 14 16
18 and the last one is 20.
There we go. We're displaying our vector data.
And then you can see that our data is being cleaned up automatically. Our test objects are being destroyed.
Pretty cool.
And you can see that there wasn't a lot of code to write it all.
Now let's clean this up just a little bit here.
Remember, we can allow the compiler to deduce our types for us.
So we can really clean this up a little bit.
Since the compiler knows exactly what we're returning right here,
we can simply replace this whole piece right here to auto.
We'll just say auto right here. Let the compiler
figure out what the type of that return value is and it knows it based on this.
And we can also change it up here in the function prototype,
get rid of all that and just replace that with auto.
So we can replace this entire return type right here with auto.
And now if we build again, we get the same compile, we get the same run.
So let's put three data items in here, and we'll say 5 6 and 7.
There we go. We're displaying our data, our vector 5 6 7, and we're destroying those objects.
Pretty simple, no news, no deletes, not worrying about
who needs to delete what.
And that's it. That's the solution to this challenge.
I hope yours was close. If you had issues with the pointers and figuring out how they work,
don't worry, just keep at it. It's just a matter of practice and understanding
conceptually what's going on and what's pointing to what,
de-referencing what to get what, and it's just a little process that takes time to learn.