Vector Flashcards
Element access
at( )
access specified element with bounds checking
Returns a reference to the element at specified location pos, with bounds checking.
If pos is not within the range of the container, an exception of type std::out_of_range is thrown.
Parameters pos - position of the element to return Return value Reference to the requested element. Exceptions std::out_of_range if !(pos < size()).
Complexity
Constant.
Element access
operator[]
access specified element
Returns a reference to the element at specified location pos. No bounds checking is performed.
Parameters
pos - position of the element to return
Return value
Reference to the requested element.
Complexity
Constant.
Element access
front( )
access the first element
Returns a reference to the first element in the container.
Calling front on an empty container is undefined.
Parameters
(none)
Return value
reference to the first element
Complexity
Constant
Element access
back( )
access the last element
Returns a reference to the last element in the container.
Calling back on an empty container causes undefined behavior.
Parameters
(none)
Return value
Reference to the last element.
Complexity
Constant.
Element access
data( )
direct access to the underlying array
Returns pointer to the underlying array serving as element storage. The pointer is such that range [data(); data() + size()) is always a valid range, even if the container is empty (data() is not dereferenceable in that case).
Parameters
(none)
Return value
Pointer to the underlying element storage. For non-empty containers, the returned pointer compares equal to the address of the first element.
Complexity
Constant.
Iterators
begin
cbegin( )
returns an iterator to the beginning
Returns an iterator to the first element of the vector.
If the vector is empty, the returned iterator will be equal to end().
range-begin-end.svg
Parameters
(none)
Return value
Iterator to the first element.
Complexity
Constant.
Iterators
end
cend( )
returns an iterator to the end
Returns an iterator to the element following the last element of the vector.
This element acts as a placeholder; attempting to access it results in undefined behavior.
range-begin-end.svg
Parameters
(none)
Return value
Iterator to the element following the last element.
Complexity
Constant.
Iterators
rbegin
crbegin( )
returns a reverse iterator to the beginning
Returns a reverse iterator to the first element of the reversed vector. It corresponds to the last element of the non-reversed vector. If the vector is empty, the returned iterator is equal to rend().
range-rbegin-rend.svg
Parameters
(none)
Return value
Reverse iterator to the first element.
Complexity
Constant.
Iterators
rend
crend( )
returns a reverse iterator to the end
Returns a reverse iterator to the element following the last element of the reversed vector. It corresponds to the element preceding the first element of the non-reversed vector. This element acts as a placeholder, attempting to access it results in undefined behavior.
range-rbegin-rend.svg
Parameters
(none)
Return value
Reverse iterator to the element following the last element.
Complexity
Constant.
Capacity
empty( )
checks whether the container is empty
hecks if the container has no elements, i.e. whether begin() == end().
Parameters
(none)
Return value
true if the container is empty, false otherwise
Complexity
Constant.
Capacity
size( )
returns the number of elements
Returns the number of elements in the container, i.e. std::distance(begin(), end()).
Parameters
(none)
Return value
The number of elements in the container.
Complexity
Constant.
Capacity
max_size( )
returns the maximum possible number of elements
Returns the maximum number of elements the container is able to hold due to system or library implementation limitations, i.e. std::distance(begin(), end()) for the largest container.
Parameters
(none)
Return value
Maximum number of elements.
Complexity
Constant.
Capacity
reserve( )
reserves storage
Increase the capacity of the vector to a value that’s greater or equal to new_cap. If new_cap is greater than the current capacity(), new storage is allocated, otherwise the method does nothing.
reserve() does not change the size of the vector.
If new_cap is greater than capacity(), all iterators, including the past-the-end iterator, and all references to the elements are invalidated. Otherwise, no iterators or references are invalidated.
Parameters
new_cap - new capacity of the vector
Type requirements
-T must meet the requirements of MoveInsertable.
Return value
(none)
Exceptions
std::length_error if new_cap > max_size().
any exception thrown by Allocator::allocate() (typically std::bad_alloc)
If an exception is thrown, this function has no effect (strong exception guarantee).
If T’s move constructor is not noexcept and T is not CopyInsertable into *this, vector will use the throwing move constructor. If it throws, the guarantee is waived and the effects are unspecified.
(since C++11)
Complexity
At most linear in the size() of the container.
Capacity
capacity( )
returns the number of elements that can be held in currently allocated storage
Returns the number of elements that the container has currently allocated space for.
Parameters
(none)
Return value
Capacity of the currently allocated storage.
Complexity
Constant.
Capacity
shrink_to_fit( )
reduces memory usage by freeing unused memory
Requests the removal of unused capacity.
It is a non-binding request to reduce capacity() to size(). It depends on the implementation whether the request is fulfilled.
If reallocation occurs, all iterators, including the past the end iterator, and all references to the elements are invalidated. If no reallocation takes place, no iterators or references are invalidated.
Parameters
(none)
Type requirements
-T must meet the requirements of MoveInsertable.
Return value
(none)
Complexity
At most linear in the size of the container.