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.
Constructs a new vector. This is the default constructor.
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.
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.
Returns the number of elements stored in the container.
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.
Removes all elements from the vector.
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.
Returns an iterator to the first element in the vector.
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.
Appends the specified element to the back of the vector, increasing the size by one.
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.
Returns a reference to the last element of the vector. There is no bounds-checking, so make sure the vector is nonempty before calling.
Returns a reference to the first element of the vector. There is no bounds-checking, so make sure the vector is nonempty before calling.
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.
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.
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.
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.
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.
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.
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.
vectorsExchanges the contents of this vector and another vector.
vector content.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.