Container Classes: Vector
The vector provides a dynamically resizing, random access container for like type objects. Vectors are suitable replacements for C++ arrays in that they provide all the capability of and array, plus the added safety of resizing and bounds checking.
Usage: #include <vector> and use the std namespace.
Declaration examples:
vector<int> vectorOfIntegers;
vector<char> vectorOfCharacters;
vector<myClass> vectorOfMyClass; // a vector of myClass objects where myClass is any user-defined class
Common member functions:
Operators: [], =, ==, and !=
push_back(element): appends an element onto the end of the vector
size(): reports the number of elements in the vector
capacity(): reports the number of elements the vector can hold before it needs to resize itself
resize(): allows the user enlarge (decrease) the size of the vector, loading multiple initial elements into the vector when enlarged and eliminating elements in positions greater than the new capacity when decreased.
reserve(): enlarges the capacity without inserting elements.
Example programs:
This program creates several vectors of integers to demonstrate them: int_vectors.cpp
Using a vector to implement a new string class: vector.cpp