vector<T>

The vector is one of the most ubiquitous and most useful STL container classes. Like the CS106 Vector, the vector is a linear, sequential container that acts as a dynamically-managed array. You can access individual elements, insert and remove elements at arbitrary points, and resize as needed.

The vector is similar in function to the deque container in that both act as variable-length arrays supporting random access. deque can be faster than vector in some cases, so you may want to read into the deque a bit.

Another linear container class is the list, which represents a list of elements that does not support random access.

Common vector<T> Member Functions
Constructor: vector<T> ()
Constructs a new empty vector.
vector<int> myVector; // Constructs a new, empty vector.

Constructs a new vector. This is the default constructor.

Constructor: vector<T> (size_type numElems)
Constructor: vector<T> (size_type numElems, const T &elem)
Constructs a pre-initialized vector
vector<int> myVector(10, 137); // Constructs a vector with 10 elements, all 137
vector<string> myVector(10); // Constructs a vector with 10 elements, all the empty string

Constructs a new vector with the specified number of copies of the specified element. With a single parameter, this constructor creates a vector containing the specified number of elements, all initialized to the default. With two parameters, this constructor creates a vector of the specified number of copies of the specified element.

T& operator [] (size_type index)
const T& operator [] (size_type index) const
T& at (size_type index)
const T& at (size_type index) const
Access vector elements.
myVector[10] = 137; // Element at position 10 is now 137.

Returns a reference to the stored element at the specified position. The operator[] function will not bounds-check and has undefined behavior when accessing beyond the bounds of the vector. The at() function will throw an out_of_range exception when reading beyond the ends of the array.

size_type size() const
Get container size
for(int i = 0; i < myVector.size(); ++i) { ... }

Returns the number of elements stored in the container.

bool empty() const
Is container empty?
if(myVector.empty()) { ... }

Returns whether the vector is empty. It's considered good practice to call empty to determine whether the vector is empty rather than checking whether size() returns zero.

void clear()
Empty container
myVector.clear();

Removes all elements from the vector.

void resize(size_type size);
void resize(size_type size, const T& fill);
Resize vector
myVector.resize(137); // Resizes the vector to 137 elements.
myVector.resize(137, 42); // Resizes the vector to 137 elements, filling new entries with 137

Resizes the vector by adding or removing elements from the end of the vector. The first version resizes the vector to the specified size, trimming elements from the end if the new size is smaller than the old size and padding the end with new objects if the new size is greater than the old size. The newly-added elements are constructed using the default constructor.

The second version of the function works identically to the first, except that any newly-added elements are initialized to have the same value as the parameter.

iterator begin()
const_iterator begin() const
Get start iterator.
for(vector<int>::iterator itr = myVector.begin(); /* ... */)

Returns an iterator to the first element in the vector.

iterator end()
const_iterator end() const
Get stop iterator.
for(/* ... */ itr != myVector.end(); /* ... */)

Returns an iterator to the element one past the end of the vector. This iterator does not point to a valid element, so do not dereference it.

void push_back(const T& elem);
Append element
myVector.push_back(137); // Appends 137 to the end of the vector.

Appends the specified element to the back of the vector, increasing the size by one.

void pop_back()
Delete last element
myVector.pop_back();

Removes the last element in the vector. pop_back does not return a value, so if you want to get the value, call back first. pop_back does not do any bounds-checking, so make sure the vector is nonempty before calling.

T& back()
const T& back() const
Get last element
int lastElem = myVector.back();

Returns a reference to the last element of the vector. There is no bounds-checking, so make sure the vector is nonempty before calling.

T& front()
const T& front() const
Get first element
int firstElem = myVector.front();

Returns a reference to the first element of the vector. There is no bounds-checking, so make sure the vector is nonempty before calling.

iterator insert(iterator position, const T& elem)
iterator insert(iterator position, size_type numCopies, const T&elem)
iterator insert(iterator position, InputIterator begin, InputIterator end)
Insert elements
myVector.insert(myVector.begin() + 3, 137); // Insert 137 after the third element
myVector.insert(myVector.begin() + 3, 42, 137); // Insert 42 copies of 137 after the third element
myVector.insert(myVector.begin(), otherContainer.begin(), otherContainer.end()); // Insert elements from another container

The first version inserts the specified element at the position indicated by the specified iterator. There is no bounds-checking. The size of the container will grow by one and elements will be shifted down.

The second version inserts the specified number of copies of the specified element at the specified position.

The third version inserts the elements in the specified iterator range into the vector at the specified position. The source iterators can be of any type.

iterator erase(iterator position)
iterator erase(iterator start, iterator stop)
Remove elements
myVector.erase(myVector.begin() + 7); // Erase seventh element.
myVector.erase(myVector.begin(), myVector.end() - 4); // Erase all but last four elements

Removes elements from the vector without bounds-checking. The first version removes the element pointed at by the iterator, and the second removes all elements in the specified range.

Other vector<T> Member Functions
void reserve(size_type size)
Request overallocated space
myVector.reserve(137); // There is now space for at least 137 elements without internal resizing

Because the vector is backed by a dynamically-allocated array, when inserting many elements the vector periodically must reallocate its space behind the scenes, which over time can lead to performance degradation. To increase efficiency, you can use the reserve function to force the vector to allocate space, guaranteeing that the array will have at least the specified number of elements.

While reserve can be used to increase the amount of space used by the vector, most implementations will ignore requests to reserve that tell the vector to shrink its buffer.

Use this function if you have advance knowledge about the number of elements you'll be holding in the vector. Used correctly, the vector's speed will greatly increase.

size_type capacity() const
Returns allocated array size
int allocatedSize = myVector.capacity(); // Returns the size of the vector behind the scenes

capacity returns the size of the dynamically-allocated buffer used by the vector. capacity does not indicate the number of elements in the vector; rather, it's the number of elements the vector can hold without resizing.

reverse_iterator begin()
const_reverse_iterator begin() const
Get start reverse iterator
for(vector<int>::reverse_iterator itr = myVector.rbegin(); /* ... */)

Returns a reverse_iterator to the last element of the vector. reverse_iterators are like regular iterators except that they traverse in the opposite direction. Check for the end-of-container condition of reverse_iterators with rend.

reverse_iterator end()
const_reverse_iterator end() const
Get stop reverse iterator.
for(/* ... */ itr != myVector.rend(); /* ... */)

Returns a reverse_iterator to the element one element before the start of the array. Like end, the iterator returned by rend does not actually reference a real element of the vector and you should take care not to actually dereference it.

size_type max_size() const
Get maximum container size
vector<int>::size_type maxElems = myVector.max_size()

Returns the maximum number of elements the vector can hold, which is usually the number of unique memory addresses divided by the size of the elements stored in the container.

void swap(vector<T> &other)
Exchange two vectors
myVector.swap(myOtherVector); // Exchanges the two vectors

Exchanges the contents of this vector and another vector.

void assign(size_type numCopies, const T& elem)
void assign(InputIterator start, InputIterator end)
Reset vector content.
myVector.assign(5, 137); // Reset content to five copies of the number 137
myVector.assign(otherContainer.begin(), otherContainer.end()); // Copy other container into this vector.

Erases any stored content and replaces it with either the specified number of repeats of the specified elements or the elements contained in the range [start, end). In either case, the original vector contents are deleted.