Vectors are dynamic arrays that can resize themselves automatically whenever an element is added or deleted. Vectors require more memory than arrays in exchange for the flexibility to manage storage and develop dynamically in an efficient manner. They can be accessed and traversed using iterators.
Some properties of vectors as a container:
Elements in sequence containers are ordered in a strictly linear sequence. Like an array, elements are accessed by their position in this sequence.
Allows direct access to any element in the sequence, and allows for relatively quick addition/removal of elements at the end of the sequence.
The container uses an allocator object to dynamically handle its storage needs.
Member functions
Iterators
begin(): Returns an iterator pointing to the first element in the vector.
end(): Returns an iterator pointing to the end at an imaginary element that follows the last element in the vector.
rbegin(): Returns a reverse iterator pointing to the last element in the vector (reverse beginning).
rend(): Returns a reverse iterator pointing to the imaginary element preceding the first element in the vector (reverse end).
cbegin(): Returns a constant iterator pointing to the first element in the vector.
cend(): Returns a constant iterator pointing to the imaginary element that follows the last element in the vector.
crbegin(): Returns a constant reverse iterator pointing to the last element in the vector.
crend(): Returns a constant reverse iterator pointing to the imaginary element preceding the first element in the vector.
Capacity
size(): Returns the total number of elements in the vector.
max_size(): Returns the maximum number of elements the vector can hold.
resize(n): Change the size of the vector to contain ‘n’ elements.
capacity(): Returns the size of the allocated storage capacity of the vector.
empty(): Returns true if the vector is empty.
reserve(): Requests a change in the vector capacity to be enough to contain n elements.
shrink_to_fit(): Reduces the capacity of the vector to fit its size and destroys all elements beyond the capacity.
Element access
operator[p]: Returns a reference to the element at position ‘p’ in the vector.
at(g): Returns a reference to the element at position ‘p’ in the vector. (Exceptions thrown are different.)
front(): Returns a reference to the first element in the vector
back(): Returns a reference to the last element in the vector
data(): Returns a direct pointer to the memory array used internally by the vector to store its owned elements.
Modifiers
assign(): It assigns a new value to the vector elements,
replacing old ones
push_back(): It pushes(adds) the elements into a vector from the back or end.
pop_back(): It pops(removes) elements from a vector from the back or last element.
insert() – It inserts new elements before the element at the specified position.
erase() – Removes elements from a vector from the specified position or range.
swap() – It swaps the contents of one vector with another vector of the same type. Sizes of vectors may differ.
clear() – It is used to remove all the elements of the vector.
emplace() – It extends the container by inserting a new element at position
emplace_back() – It is used to extend and insert a new element into the vector, the new element is added to the end of the vector.
Please check code for the above functions here.
Thank you :)