- [Bill Weinman] A vector is a sequence container
that supports random access of its elements.
You can think of it like an object oriented array
with a bunch of cool extra features.
Vectors are extremely flexible.
Let's take a look.
This is vector.cpp from chapter 9
of the exercise files.
You'll notice I include the vector header.
Also the string header.
We'll be using STL strings a little later
but the vector header is where the
vector template class is defined.
A vector is declared
using template syntax
with angle brackets for the underlying type.
And so you can see here, this is a vector of integers.
And you'll notice also that we use the
std:: syntax
to specify the vector in the standard name space.
All of the elements of the
STL, the standard template library,
are in the STD, or standard, name space.
That includes "cout" and "endl" and "string"
and all of these identifiers are in the standard name space.
You initialize a vector with an initializer list
just like you would with an array.
And it has methods for the size of the vector,
the front element and the back element.
And if we build and run this,
we'll just look at the top output here.
There's a bunch of other stuff yet to come.
You notice that the size is 10
because we gave it 10 elements.
And the front element is one and the back element is 10.
The begin and end methods provide an iterator,
and the iterator is of this rather
intimidating looking type.
It's basically the vector
and the vector's underlying type
with this scope operator just like in namespace,
because the word iterator is actually a
member type def in the vector class.
And so this is the type of the iterator
that's returned from begin and end.
And you'll notice here in my for loop,
I use the auto keyword because the compiler
knows the type of itbegin and itend.
In fact,
I can use the auto keyword here
at it'll work just fine.
And this is actually far more common.
I just wanted you to see what the iterator type looks like.
But this works great.
And in fact, we don't even need this itbegin and itend.
We can just say vi1 dot begin,
and vi1 dot end.
And we don't even need those at all,
and this for loop will work just the same.
I did want you to see what those iterators look like.
You can access a particular element from an iterator
by using it just like a pointer with the
pointer dereference operator.
And in fact, this iterator will act
essentially like a pointer.
You can look at an element using the
same bracket syntax that you would with a native array,
or you can use the at method to
access a particular element in the vector.
And we can see those here.
Element at 5, element at 5.
And you can also use the range-based for loop,
just like this.
Again, just like you would with a native array
and this works just great.
And this is actually very very common in
code written since C++11.
And you see the result there under range-based for loop.
We can insert an element using the insert method.
And you give it an iterator and an offset.
Basically so a begin iterator plus five, or you can use
an end iterator minus whatever.
Again, we're using that iterator in this case
just like we would a pointer, with an offset.
And so after we insert, you see now our size is
11 and our element at number five
is now 42.
And we can erase using the erase method.
And again here we erase.
And our size is now 10 and our element at number five
is again six, like expected.
We can push an element on the back with push_back,
and that will add an element to the end of the array.
We can also initialize a vector from a C-array.
And here we have an array and size is 10
and it has those 10 elements.
And here we initialize that vector using
a constructor with the array.
And we can see that there's our 10 elements from the array
using this range-based for loop.
And of course we can use any type we want to
with the vector.
Again, this is why it's a template class.
So it really doesn't care what the type is.
It can be one of your own classes, or anything.
And in this case we have here a vector of STL strings.
And there's five of them.
And you can see that output down there.
So the vector is a fundamental container type
that's very powerful and easy to use.
Use it wherever you would otherwise use a C array
but could benefit from some of the capabilities
that the vector provides.
While vector is the most common, the STL provides
a number of container classes.
For more in-depth training, see the
companion course C++ templates and the STL.