Today we talked about vectors. Key points were:
I used strings as an analogy to explain the general idea:
a string is essentially a list of characters,
and a vector<VAR> is essentially a list of variables of type VAR.
For example, in this class we will frequently use vector<int>, vector<double>, and vector<string>.
To use vectors efficiently, it's fine to modify any value in the list
(using myVector[ 3 ] to access the element in slot 3 and so on).
It's also fine to add or remove elements from the END of the list.
But inserting or deleting elements from the beginning or middle ends up costing a lot of runtime.
The most important functions for working with vectors are:
Constructors: These are the various ways of initializing a vector.
We can start our vectors out empty,
or with a specified size,
or with a specified size AND all elements initially set to a certain value.
size: Returns the number of elements currently in the list.
: We can access elements of the list using e.g. myVector[k],
where the index k runs from 0 through myVector.size()-1.
push_back: Use this to add one element to the end of the list.
pop_back: Use this to remove the last element from the list.
We wrote the code example vectors.cpp,
which shows examples of all of the functionality above.
We built several basic functions to practice iterating through vectors.
Here are some Google searches you could try if you're having trouble with today's material.
I'm deliberately trying to choose search terms that you could have thought of yourself!
c++ std::vector This would help you find
which is a pretty complete resource on vector functions.
In the "Member Functions" section there're links to examples using each of the functions I listed above,
so this would be a good site to check if you're having trouble getting the syntax right.
Of course, there are lots of functions on the list that you won't be responsible for this quarter!