Vectors are just like dynamic arrays. Vectors can automatically resize themselves when an element is removed/added, and the container handles their storage. Iterators can access and traverse vector items since they are stored in contiguous storage. Data is put at the end of vectors, and inserting at the end takes more time because the array may need to be extended at times. Because there is no scaling, removing the last element takes a constant amount of time. In terms of time, inserting and erasing at the start or in the middle is linear.
The following are some of the functions connected with the vector:
Iterators
- begin() - returns an iterator referring to the vector's initial element; end() returns an iterator pointing to the theoretical element that follows the vector's last element.
- rbegin() – Returns a reverse iterator that points to the vector's final entry. It progresses from the last to the first piece.
- rend() – Returns a reverse iterator that points to the theoretical element before the vector's first element.
- cbegin() – Returns a constant iterator that points to the vector's first element.
- cend() – Returns a constant iterator pointing to the theoretical element following the last element of the vector.
- crbegin() – Returns a constant reverse iterator pointing to the last element of the vector. From the last to the first item, it progresses.
- crend() – Returns a constant reverse iterator pointing to the theoretical element before the vector's first element.
Capacity
- size() – Returns the vector's size in elements.
- max size() — This function returns the vector's maximum size in elements.
- capacity() – Returns the number of elements in the presently allocated storage space for the vector.
- resize(n) — Resizes the container to fit 'n' elements in it.
- empty() – This function determines whether or not the container is empty.
- shrink to fit() – Reduces the container's capacity to fit its size and destroys any elements that exceed the container's capacity.
- reserve() – Demands that the vector capacity be sufficient to hold at least n elements.
Element access:
- reference operator [g] - Returns a reference to the element at point 'g' in the vector with the reference operator [g].
- at(g) – Returns a pointer to the element in the vector at position 'g'.
- front() – Gets the first element in the vector as a reference.
- back() – Returns the final element in the vector as a reference.
- data() –Returns a pointer to the vector's internal memory array for storing its own elements.
Modifiers:
- assign() — It replaces old values with new ones in the vector elements.
- pop back() - It is used to pop or remove elements from a vector from the back. push back() is used to push elements into a vector from the back.
- insert() — This function adds additional elements to the front of an existing element at the provided place.
- erase() - It is used to remove elements from a container from a defined location or range using the erase() method.
- swap() - It is a function that swaps the contents of two vectors of the same type. Sizes may vary.
- clear() – It is a function that removes all of the elements from the vector container
- emplace () – It expands the container by adding a new element to the emplaced position.
- back() — This function is used to add a new element to the end of a vector container.