In this video, we'll look at the syntax for accessing individual
vector elements.
The first way to access vector elements is to use the array syntax we've already learned,
using the subscript operator. That's the square brackets.
We use the name of the vector, followed by the square brackets with the index of the vector
element we want inside the square brackets.
For example, test score sub-1 refers to the element at index 1
in the test scores vector.
In the code example at the bottom,
you can see that the syntax is exactly as it was for arrays.
Remember that vector subscripting or indexing is the same as in arrays.
It starts at 0, so the first element of an array is at index 0.
It's important to understand when we use the subscript operator with a vector,
no bounds checking will be done.
You have to do it yourself as with arrays.
Now let's look at another way of accessing vector elements.
We can also access vector elements using the at method.
This is the vector syntax.
Since when we create a vector, we're really creating an object
in object-oriented jargon.
Then we can ask objects to perform operations.
These operations are called methods.
The syntax is simple. The object name, in this case, the name of the vector
followed by a . called the .operator,
followed by the name of the operation we want performed.
This is the method name.
Don't worry about the jargon for now, it will come before you know it,
and we'll cover it in depth when we talk about object-oriented programming.
The c++ plus vector has a method or operation called "at", a-t.
So you can ask the vector to give you an element at a specific position,
which you provide inside the parentheses.
This style is actually much more intuitive since the name "at" provides context.
So in the example, we've declared and initialized a vector named test scores
to the 5 integers 100, 95, 99, 87 and 88.
If we want to access a specific element,
we use the at method and provide the element index.
You can see that the syntax is very easy to read and understand.
Test scores at 0
will refer to the first element in the vector. That's element at position 0.
We can also use the same syntax when we want to modify vector elements.
In this example, we're reading 5 integers from the console
and storing these elements into the test scores vector.
The last statement is an assignment statement as we saw with arrays.
We're storing 90 into the first element of the test score's vector.
You're probably thinking okay, that's pretty easy. Vectors are just as easy to use as arrays but
when do they change in size dynamically.
The vector has a method called pushback
that adds a new element to the end of the vector.
Remember, all vector elements must be of the same type,
so the element you add must be of the same type as all the other vector elements.
In this example, we've created a vector called test scores that contains 3 integers:
100, 95 and 99.
If this were an array and we wanted to add another test score,
we'd be stuck since arrays are fixed in size.
However, with vectors, we can use the pushback method
and provide the integer you want to add within the parentheses.
You can see that I'm asking the test scores vector
to push back 80 and then 90.
This will add 80 and then 90 to the end of the vector.
Now the vector will contain five elements: 100, 95, 99,
80 and 90.
How does this work? You don't have to worry about it. That's the beauty of vectors
and of the other classes in the standard template library.
The vector will take care of allocating or de-allocating space,
adding new values to that space and so forth.
You can concentrate on solving your problem.
Okay. But what happens if you're using at
or some other vector method and you go out of bounds? Let's see.
If you go out of bounds, and you're using a method that does bounce checking,
c++ plus will throw an exception.
That's the jargon used to say that an exceptional condition has been encountered
and the program can't continue.
An error message is displayed and the program will terminate.
This is a good thing.
Now you have an error message that tells you what the problem was and you can fix it.
The error message in this case is pretty long,
but it's telling you exactly what happened.
The error message may look different depending on the compiler you have, but the information provided
will be the same.
In this case, I bolded the important parts of the error message.
You can see out of range,
vector range check and the numbers 5 and 2.
When you look back at your code,
you'll see the vector has two elements and you wanted element at index 5.
You want your program to terminate or crash during testing, so you can fix errors,
but you don't want it to crash in production.
This is where you can do exception handling.
This involves catching these exceptions as the program runs
and dealing with them so that the program doesn't crash.
Exception handling is a more advanced topic, but i want to cover the basics,
so there will be a section at the end of the course just on exception handling.
I hope you can see how much better vectors are than arrays,
and why you should use vectors in modern c++.
Now let's head over to CodeLite and use what we learned
and write some code with vectors.
So now I'm in CodeLite. And let's do some examples with vectors.
I'm in the seven section workspace under the vectors project.
And here's my main file. Notice that I'm including the vector include file.
You must do this in order to use vectors. Also,
vectors belong to the standard namespace,
so either you use the namespace standard or you'll have to do std
scope resolution operator everywhere you use vectors.
Let's talk about a couple of different ways to create vectors. In this first example here,
I can create a vector called vowels.
And it's a vector of characters. And that's the syntax.
We put the type inside the angle brackets.
Okay. So in this case, I've got vowels is a vector of characters
and this is an empty vector.
Okay it's of size 0. There's nothing in there right now.
The only way to add things to it is to do something like pushback,
which we'll do in a little bit. The second example creates
a vector called vowels again.
In this case, it's 5 characters long
and each one is initialized to 0.
This third example here which is the one I'm going to use in this example.
I'm using an initializer list here so I'm initializing the vector
vowels, which is again is a vector of characters to
the characters a, e, i, o, u., which are my vowels.
Again, remember that the character literals have single quotes around them, not double quotes.
So at this point, I can simply output vowel sub-0
and vowel sub-4, which i expect to print out a
and u,respectively.
Notice that even though I'm using a vector,
I'm using the array syntax. So it's very, very familiar.
Also remember that using the subscript notation does not
do bounce checking.
Okay. So let me run this.
So there's my sample run. There's my lowercase a, my lowercase u, just what I expect.
All right. So now let's take a look at another declaration of vector. In this first example here,
I'm creating a vector called test scores, which is a vector of integers.
And I'm creating a vector of 3 integers in this case,
all of them will be initialized to 0. In the second example,
I'm creating the test scores vector again.
Obviously, I have to comment them out you can't use the same name twice that's why I'm doing this.
It's a vector of integers, 3 of them, each one is initialized to a 100.
And so those are two variants for constructing these vector objects.
The one I'm going to use is this one right here,
where I'm saying test scores again it's a vector of integers.
And I'm initializing them to 100, 98 and 89.
So in this case, I'm going to get exactly 3 elements
in my vector. My vector will be of size 3.
Okay. So let's comment this out here.
And in this case, I expect it to display
100, 98, 89.
Again, here I'm using the array syntax. So let me run this.
And there you go, 100, 98, 89 using array syntax.
Now we can also use vector syntax. So let me uncomment this.
And now here, you can see I'm saying test_scores.at(0),
test_scores.at(1), test_scores.at(2).
So in this case, I'm using the at method to give me the value at that specific index.
Same idea except that this is a method
and this will be bounce checked, which is very important to understand.
Also after I print out those three numbers, I'm saying there are
so many scores in the vector. Okay. So what does that mean?
Well, notice the size method here. This is a method in the vector class.
So I can have my vector name and I can just say something like .size. It's that simple.
And this will return, however, many items are in the vector right now.
Right now I have 3 of them. So I expect this to say 3 are three
scores in the vector.
Remember, the vector can grow and shrink dynamically.
So it's important to be able to ask it, hey, how big are you?
That's what the size method does. So I'll run this one more time.
And you can see here, my test score is using array syntax 100, 98, 89.
Now my test score is using the vector syntax 100, 98, 89.
We expect that because the the vector hasn't changed.
But here it says, there are three scores in the vector.
So here we're asking the vector, hey, how big are you? What's your size? And it's returning a 3.
In this example here, what we're going to do is we're going to
actually put some information into the vector.
We're going to use the vector syntax from this point forward.
So here, I'm saying enter 3 test scores
and then I'm grabbing three integers from the console
and I'm putting the first one at 0, the second one at 1 and the third one at 2.
So I'm changing those vector elements.
And then I just want to display them out again. So I'll uncomment this.
Okay. So let's do that. I'll build and run again.
Right now we've got 100, 98 and 89 in there.
So let's just say I want 10, 20 and 30.
And I want to display them out. And there you go. Updated test scores is 10, 20
and 30. So you can see that we've actually changed the contents of those vector elements.
Now let's talk a little bit about the dynamic nature of vectors.
This is the part that's really really cool.
I'm going to uncomment this bit out here.
And notice the code here, it says enter a test score to add to the vector.
Remember, the vector at this point has 3 elements in it.
I need to read this into some variable.
So I've created a simple variable right here called score to add.
It's just an integer. This is not a vector. This is not an array.
This is just a plain old integer variable.
I'm initializing it to 0. That's the best practice.
And I'm reading an integer from the keyboard,
from the user and putting it into score to add then. What I'm going to do is
I'm going to tell the test scores vector
to push that integer at the back of the vector.
So it's going to add it to the vector.
If the vector needs to increase in size, it will.
That's nothing we have to do, it handles that automatically.
Then I'm going to say enter one more test score.
So I'll read another test score from the console,
and I'll push that score on.
So at this point when we display, we should have
5 integers now in the vector.
I'm going to display them all at 0, 1, 2, 3 and 4.
And then I'm going to say, hey, test scores, what's your size?
And I expect a 5 here. Okay. So let's try that.
All right. So I'm going to enter my test scores again. I'll say 10, 20 and 30.
Now remember, our vector contains 3
integers right now: 10, 20 and 30.
So let's enter a test score to add. Let's add a 100.
Now it's saying give me one more. Let's put in
105, maybe I got some extra credit.
Now when I press enter, I should expect to see 5 integers displayed
as well as the size of the vector now being 5. So I'm going to press enter
and that's exactly what I see.
The test scores are 10, 20, 30, 100, 105.
Then it says there are now 5 scores in the vector. That's pretty cool.
Let me show you an example of an exception.
Right here, I'm saying this should cause an exception because I'm trying to access
test scores at 10. If I tried to do something like this
and just used array syntax,
like that, no bounds checking will happen.
I might just get a crash with no error message or anything. Instead, I'm actually saying at
10.
And we could try to put a new line. It's not going to matter much because it's not going to do
that because there is no 10th element. Let me run this again, and I'll enter those numbers again.
Okay. So again, we'll say 10, 20, 30.
Let's push in 100 and 105. Now we should get an exception,
and there's the exception. You can see the program is still stopping,
but there's my exception.
Terminate called after throwing an instance of out of range.
There's a range checking problem, which is at 10,
and this guy is 5 big.
So that gives you a really, really good clue as to what's going on.
Right, I'm going to comment that out so it won't affect us again.
And then the last thing we'll do is I'll show you an example of using
a two-dimensional vector.
I'll uncomment all of this out. All right. So here, how do we declare
a two-dimensional vector? Well, a two-dimensional vector
is a vector of vectors. So it's a vector of vectors.
And this is how I would declare a vector of vectors. I want to declare a vector
that contains within it not int, not floats, not doubles, but a vector.
In this case, a vector of integers, and that's my movie ratings.
I can initialize them right here.
So going horizontally that would be the first reviewer,
the second reviewer, the third reviewer.
And going vertically, this would be the first movie, the second movie, the third movie and the fourth movie.
Now just like I showed you in the slides with arrays, I can use array syntax.
So I could say here the movie ratings for reviewer number one using array syntax.
And I would expect this to print out 1, 2, 3, 4, that's the first reviewer,
right up here. And in order to use vector syntax, we just use at,but we use it twice.
Give me the rating at 0 that gives me this guy.
And then once I have that guy, I can take at 0 for him
or at 1 and the next line and so forth.
So if I run this, I would expect to see 1, 2, 3, 4
both times. So let's run it,
and we'll get by this part one more time.
And there you go.Here are the movie ratings for reviewer number one;
array syntax 1, 2, 3, 4.
Here are the movie ratings for reviewer number 2,
that's -- sorry number one again, that's right, using vector syntax 1, 2 3, 4.
Okay. So that basically covers the fundamentals of vectors.
How to use them. There's so much more effect. And we'll talk about all of that
when we talk about the standard template library.
But congratulations, you've just now created objects,
used object methods. And we're on the way to object-oriented programming.