.Open Introducing C++ Vectors and Arrays
This is a short introduction to the standard vectors available in C++.
Vectors are a powerful yet simple data structure.
There is a PDF version
.See ./vectors.pdf
suitable for an ereader.
. Quick Reference
Vectors are good when we have an unknown sequence of similar
items to store
and we want to access them by their sequence numbers.
Vectors are held in a special library and can be used in a file that has
.As_is #include
at its beginning. It uses the "std" namespace so you will need "using ...;" statements.
.Table
.Row Declaration vector v(initial_size);
.Row Accessors v.empty(), v.size(), v.front(), v.back()
.Row Mutators v.push_back(T), v.pop_back()
.Row Operators v[int], v.at(int), v1=v2;, v1==v2
.Close.Table
. Key Facts
You need to remember the following facts about vectors:
.Box
A vector is an object that contains a sequence of other objects inside it.
The objects inside must all take up the same amount of storage.
They are numbered starting with 0.
If the whole vector is called `v` then the items in it are written `v[0]`, `v[1]`, `v[2]`, ...
The last item is `v[v.size()-1]` NOT `v[v.size()]`.
New items can be "pushed" onto the end of the vector.
The last item can be "popped" off of a vector.
Vectors can therefore change size.
We can find out the current size of a vector: v.size()
Vectors can be empty. If so v.empty() is true.
If a vector is empty then v[i] and v.pop.... crash.
Vectors are empty. by default, when created.
Vectors shouls be passed by reference whenever possible.
.Close.Box
. Details
Suppose that `T` is any type or class - say int, float, double, or
the name of a class, then
.As_is vector v;
declares a new and empty $vector called `v`.
Given object `v` declare like the above you can do the following things with it:
.Set
test to see if `v` is empty:
.As_is v.empty()
find how many items are in `v`:
.As_is v.size()
push `t` in `T` onto the end of `v`:
.As_is v.push_back(t)
pop the back of `v` off `v`:
.As_is v.pop_back()
Access the `i`'th item (0<=`i`
.As_is #include
.As_is using namespace std;
.As_is void print( const vector& ) ;//utility function outputs a vector of ints
.As_is void print_backwards( const vector &);
Then we describe the main program:
.As_is int main()
.As_is {
.As_is vector v;
.As_is int number;
.As_is
.As_is cout <<"Input some numbers and then end the input\n";
.As_is while(cin>>number){
.As_is v.push_back(number);
.As_is }//while(more)
.As_is
.As_is print(v);
.As_is print_backwards(v);
.As_is
.As_is }//main
Finally the two procedures that print out the data:
.As_is void print_backwards( const vector &a)
.As_is {
.As_is for(int i=a.size()-1; i>=0; --i)
.As_is cout << a[i] << " ";
.As_is
.As_is cout << endl;
.As_is cout << "----------------"<& a)
.As_is {
.As_is for(int i=0; i
library shares a lot of common features with and other sequential
containers. The main differences are (1) s can only hold
characters, and (2)s have some useful extra operations available.
. Hint
It helps to draw diagrams of vectors and work out by hand, on these diagrams
what an algorithm, or your program is doing. Pretending to be a computer
can teach you a lot about a complex computation.
. Glossary
container::=A data structure which contains a number of data items that can gain and lose items as the program runs. Examples include vectors, lists, stacks, ...
increment::$operation=moving an iterator/pointer/index on to the next iterator/pointer/index value commonly symbolized by ++ in C-like languages.
operation::=something that can be applied to an object to extract information from it or to change its state.
random_access::=being able to do things to items in a container in an unpredictable order typically by using an integer as an index or subscript.
vector::$container=a vector is a sequential container that is optimized to allow efficient $random_access of items by using an index.
Vectors are are given in a $contiguous block of storage space.
Vectors are useful whenever data must be stored in
one order accessed in a different one. A vector is
a dynamic array - an array that can grow when needed.
.See Vectors
.Open Errors
You must always #include if you use the word
vector.
You must be sure that all indices or subscripts stay between 0 and size()-1 inclusive.
Trying to change the size of an array does `not` work.
If a program compiles, loads, crashes, and there is a subscript operator ([...])
then check to see if the subscripted item has been put into the container
`before` the subscripted element is accessed. A very common error is to
declare a vector with no length and
to use [...] as if it created new elements. It doesn't. Use 'push_back(..)'
to add new elements to a vector.
If you are unable to
show that a subscript is in range then use a condition like the following
to guard `thing[i]` from errors:
.As_is 0<= i && i < thing.size()
If a program compiles, loads and crashes and it has a $pop_back,
function than make sure that there is an item to
be "popped"! In code you can use the empty function to guard statements
that contain "pop" from blowing up.
On some older compilers and libraries
when you need a as well as
you need to
.As_is #include
before including the list or vector rather in the
reverse order! Older string library appear to
define some special versions of vector and list operators
and the older compilers can not make up its mind which to use.
If the standard is not found then you are using an older C++
compiler.
You can have vectors of vectors:
.As_is vector < vector > matrix;
but you must put at least one space between the two ">" symbols.
The following is `wrong`:
.As_is vector < vector > matrix;
.Close Errors
.Close Introducing C++ Vectors and Arrays