Welcome back. I hope you enjoyed working with vectors
and learn more about how to use them.
You can find my solution in the section 7 workspace
in the challenge solution project.
Let's go through my solution to this challenge one line at a time.
I've inserted the challenge directives in my solution as comments,
so it's easier to follow along. So where do we start.
First thing we need to do is we need to include the vector library.
If you didn't do that you probably got some syntax errors about vector not found and
probably got a lot of errors.
Also we can use the namespace standard.
We don't have to do this but it makes the code a lot less cluttered,
and it's easier to display for teaching purposes that way.
If you didn't include namespace standard,
you might have gotten errors and you probably did something like standard
scope resolution operator vector.
So let's do the first directive. It says declare two empty vectors
of integers named vector1 and vector2, respectively.
On lines 42 and 43, that's exactly what I'm doing.
You can see vector1 is a vector of integers,
and vector2 is a vector of integers.
There is no constructor here with the size.
There's no initialization list. So these are going to be empty vectors.
The second directive on line 45 says add 10 and 20 to vector1
dynamically using pushback.
That's what I'm doing on lines 47 and 48, vector1.pushback10,
vector1.pushback20.
These two elements will be added to the back of the vector and the storage will be allocated
automatically for them.
This is where we can see that the vectors can grow.
On line 50, we want to display the elements in vector 1 using the at method,
and we also want to display the size of the vector using the size method.
So you can see that between lines 52 and 55, that's what I'm doing here.
On line 53, I'm simply saying give me the element at vectorn1's 0
and then give me the element at 1.
So that will display 20 and 20 because that's what's in there.
And then I'm gonna say vector1 contains
vector.size will return 2 because there's currently 2 elements in the vector1.
So it should display vector1 contains two elements.
Okay. So, so far pretty easy. Let me scroll up just a little bit.
. And on line 57,
we're doing the same thing with a 100 and 200 but to vector2.
So i'm adding 100 and 200
dynamically using pushback on vector2.
Vector2 was empty.
So after this, vector2 should have 100 and 200
and the size of 2.
What we're doing on line 64 through 67 is the same thing we did with vector1,
except we're doing it with vector2.
We're displaying the results. This should display 100,
200, and vector2 contains two elements.
Okay. Let me scroll up a little bit more now on line 69,
the directive says declare an empty two-dimensional vector called vector2d.
And there's a little hint there that says remember that a 2d vector is a vector of vectors.
So this is how we would declare this. Vector2d,
that's my object,
is a vector of integer vectors
or a vector of vector of integers, however, you want to pronounce it.
So you get the idea there. So it's a vector that can hold other vectors.
Therefore, you have the two dimensionality.
On line 74 and 75, what we're doing is we're adding
an entire vector to the vector2d with pushback.
We can do that, right, because it's a vector of vector. So I could certainly add a vector to it.
And that's what's happening on line 77 and 78.
On line 77, I'm adding vector1
to vector2d using pushback,
which means that remember vector2d at this point was empty.
So storage will be allocated, vector1 will be put in there
doing the same with vector2. So at this point, we'll have
a two dimensional vector that contains two vectors.
So on line 80, we're displaying the elements in vector2d d using the at method.
So you can see here I'm displaying the element at 0, 0., and then the element at 0, 1.
And then the element at 1, 0 and the element at 1, 1.
So this should display 10, 20 and 100, 200.
So this is the interesting part now. I've asked you to change
vector1's first element, right. The element at index 0 to a 1000,
and then display the vector again.
What did you expect? Some of you might have thought that that first element
would have changed, right, because after all we did put vector1 into the 2d vector.
And you might be surprised that it didn't.And we'll talk about that
in a second. But here, we're displaying vector1 just to see if it actually did change.
So let's run this, and we'll walk through the output. So I'm going to build and run.
And you can see at the beginning, we're saying okay vector1,
right, we pushed to 10 and the 20. So that was fine.
And then vector1 contains the two elements. That's what we expect. Vector2,
we inserted the 100 and the 200.
And vector2 now contains two elements.
Vector2d contains vector1 and vector2 within it, right.
Again, it's a 2d vector. So the first one was 10, 20.
The second one was 100, 200. Pretty cool.
But now at this point what we did was this is where we're at right now.
We just changed vector1's first element to a 1000.
And then we printed out that 2d vector again. But you can see the 2d vector,
there's no 1000 here where the 10 is. That's where you would expect it, right.
There isn't. The reason for that is that when you add elements to vectors,
you actually make a copy of what you're inserting.
So that tells you a little bit about what's going on.
A little bit later on in the course what we'll do is
we'll have vectors of references or vectors of pointers.
And those are a little bit different because here we can actually
change what it's pointing to and it'll be reflected in the vector.
But when you're using integers and floats and doubles, remember,
we're making copies of what we're putting into the vector.
All right. So that completes this solution. I hope you enjoyed the challenge.
And I'll see you in the next video.