Vector is a dynamic container type in OpenFrameworks (and C++) that is able to hold multiple elements of the same type. It functions like a special type of array that can be expanded and contracted while the application runs. Vector elements are stored in contiguous locations so they can be accessed easily using operators, iterators, and pointers. You can download a pdf version of this tutorial here. Here is a link to the C++ vector reference page.
Declaration
When you declare a vector you need to specify the type of data that it will contain. This enables the compiler to know how much memory to allocate to a vector instance.
syntax: vector < data_type > vector_name; example: vector < ofTouchEventArgs > touches;
Add Element
To add a new element to the end of a vector you use the push_back() method. This method accepts an object as an argument. This object is placed at the end of the array. The object must be of the appropriate data type, otherwise you will receive a “No matching function” error.
syntax: vector_name.push_back(new_object); example: touches.push_back(touches);
Get Element
To access objects from an Vector you can use several different approaches. The most common ones are the operator [index] and the at(index) method. The difference between these approaches is that at() throws an exception if the index is out of range).
syntax: vector_name[index] example: touches.at(index)
Iterators and pointer math can also be used to access objects from a Vector. To learn more about how to use these approaches check out the C++ reference website.
Remove Element
To remove elements from a Vector the best approach is to use the erase() method. This function takes an iterator as an argument. The vector methods begin() and end() both return iterators; the former returns an iterator that points to the first element in the Vector, the later returns an iterator that points to the last element in the Vector.
syntax: vector_name.erase(iterator); example: touches.erase(touches.begin() + i);
You can also use the pop_back() function to remove the last element from a vector.
syntax: vector_name.pop_back(); example: touches.pop_back();
Vector Iterators
An iterator is an object that has the ability to iterate through the elements of a range, such as a vector or array. Vectors have a special iterator that can go through its elements using operators such as increment (++) and decrement (–). Iterators are used as input arguments for several of the modification methods in the vector class and for looping through all the elements in an array.
To get an iterator from a vector you can use the begin() and end() methods. These methods return an iterator that resolves to the first and last positions of the vector respectively. Below is sample of code that demonstrates how to use an iterator to loop through the elements in a vector.
vector my_vector;
for (int i=1; i<=5; i++)
my_vector.push_back(i);
vector::iterator my_iterator;
for (my_iterator = my_vector.begin(); my_iterator < my_vector.end(); ++my_iterator)
cout << " " << * my_iterator;
Random Stuff
Here is a brief overview of a few other vector methods worth knowing about. Check out the full reference for more information about vectors.
- empty(): checks if a vector empty, and returns true if it is.
- size(): returns the number of elements in a vector.
- capacity(): returns the amount of space allocated to the vector.
Thank You
I hope this short tutorial has been helpful since the reference pages can sometimes be a bit cryptic. If you have any comments or questions email me at julioterra at gmail dot com.

![Stackable [photos] Stackable [photos]](http://julioterra.com/journal/wp-content/uploads/2011/07/stackables_photo_550.png)



