# C++ Notes: STL: vector<T> template class

The `vector` template class provides a form of dynamic array that expands at the end as necessary to accommodate additional elements.

Assume that `T` is some type (eg, int) and the following declarations:

       T e;
       vector<T> v, v1;
       vector<T>::iterator iter, iter2, beg, end;
       vector<T>::reverse_iterator riter;  /* beg, end could also be here */
       int i, n;
       bool b;

### Common vector constructors, functions, operators, etc

Result | Method |  | Description
Constructors and destructors
`` | `vector<T> v;` | L | Creates an empty vector of T's.
`` | `vector<T> v(n);` |  | Creates vector of n default values.
`` | `vector<T> v(n, e);` | L | Creates vector of n copies of e.
`` | `vector<T> v(beg, end);` | L | Creates vector with elements copied from range beg..end.
`` | `v.~vector<T>();` | L | Destroys all elems and frees memory.
Size
`i =` | `v.size();` | L | Number of elements.
`i =` | `v.capacity();` |  | Max number of elements before reallocation.
`i =` | `v.max_size();` |  | Implementation max number of elements.
`b =` | `v.empty();` | L | True if empty. Same as `v.size()==0`
`` | `v.reserve(n);` |  | Sets capacity to `n` before reallocation
Altering
`v =` | `v1;` | L | Assigns v1 to v.
`v[i] =` | `e;` |  | Sets ith element. Subscripts from zero.
`v.at(i) =` | `e;` |  | As subscription, but may throw `out_of_range`.
`v.front() =` | `e;` | L | Same as `v[0] = e`.
`v.back() =` | `e;` | L | Same as `v[v.size()-1] = e`.
`` | `v.push_back(e);` | L | Adds e to end of v. Expands v if necessary.
`` | `v.pop_back();` | L | Removes last element of v.
`` | `v.clear();` | L | Removes all elements.
`iter =` | `v.assign(n, e);` | L | Replaces existing elements with n copies of e.
`iter =` | `v.assign(beg, end);` | L | Replaces existing elements with copies from range beg..end.
`iter2 =` | `v.insert(iter, e);` | L | Inserts a copy of e at iter position and returns its position.
`` | `v.insert(iter, n, e);` | L | Inserts n copies of e starting at iter position.
`` | `v.insert(iter, beg, end);` | L | Inserts all elements in range beg..end, starting at iter position.
`iter2 =` | `v.erase(iter);` | L | Removes element at iter position and returns position of next element.
`iter =` | `v.erase(beg, end);` | L | Removes range beg..end and returns position of next element.
Access
`e =` | `v[i];` |  | ith element. No range checking.
`e =` | `v.at(i);` |  | As subscription, but may throw `out_of_range`.
`e =` | `v.front();` | L | First element. No range checking.
`e =` | `v.back();` | L | Last element. No range checking.
Iterators (operators apply to both forward and reverse iterators)
`iter =` | `v.begin();` | L | Returns iterator to first element.
`iter =` | `v.end();` | L | Returns iterator to after last element.
`riter =` | `v.rbegin();` | L | Returns iterator to first (in reverse order) element.
`riter =` | `v.rend();` | L | Returns iterator to after last (in reverse order) element.
`` | `++iter;` | L | Preincrement iter to next element. Use in both forward and reverse iterators normally. Can also postincrement. Returns value.
`` | `--iter;` | L | Predecrement iter. Can also postincrement. Returns value.
`iter2 =` | `iter + i;` |  | Iterator i elements after iter. `+=` assigment also defined.
`iter2 =` | `iter - i;` |  | Iterator i elements before iter `-=` assignment also defined.
`e =` | `*iter;` | L | Dereference the iterator to get the value.