Arrays and vectors in C++
Arrays
A vector in C++ is a class in STL that represents an array.
Advantages of array
- Code optimization: data can be retrieved or sorted efficiently.
- Random access: Any data located at an index position can be retrieved.
Disadvantages of array
- Size limit: fixed size of elements in the array. It doesn’t grow its size at runtime.
int arr[6] = {10, 20, 30, 40};
// same as int arr[] = {10, 20, 30, 40, 0, 0}
Vectors
- They have the ability to resize itself automatically when an element is inserted or deleted.
- Their storage is handled automatically by the container.
- Vector elements are placed in contiguous storage so that they can be accessed and traversed using iterators.
- In vectors, data is inserted at the end. This takes
differential time
, as sometimes the array may need to be extended. - Removing the last element takes only
constant time
since no resizing happens. - Inserting and erasing at the beginning or in the middle takes
linear time
.
begin()
: returns an iterator pointing to the first element in the vector.end()
: returns an iterator pointing to 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 first element in the vector (considered as reverse end).cbegin()
: returns a constant iterator pointing to the first element in the vector.cend()
: returns a constant iterator pointing to the last element in the vector.crbegin() and crend()
: constant reverse iterators.
Capacity
size()
: returns number of elements.max_size()
: returns maximum number of elements that the vector can hold.capacity()
: returns the size of the storage space currently allocated to the vector.resize(n)
: resizes the container so that it contains n elements.empty()
: returns whether the container is emtpy.shrink_to_fit()
: reduces the capacity of the container to fit its size and destroys all elements beyond the capacity.reverse(n)
: requests that the vector capacity be at least enough to contain n elements.
Element access
- reference operator
[g]
: returns a reference to element at position g. at(g)
: returns a reference to element at position g.front()
: returns a reference to first element.back()
: returns a reference to last element.data()
: returns a direct pointer to the first element.
Modifiers
assign()
: assigns new value to the vector elements by replacing old ones.push_back()
andpop_back()
insert()
: inserts new elements before the element at the specified position.erase()
: remove elements from the speficied position or range.swap()
: swap contents between two vectors of same type. Sizes may differ.clear()
: remove all the elements.embrace()
: extends the container by inserting new element at position.embrace_back()
: extends the container by inserting new element at the end of the vector.
Time complexity for operations on vectors
- random access: constant O(1)
- insertion/removal of elements at the end: constant O(1)
- insertion/removal of elements - linear O(N)
- knowing the size: constant O(1)
- resizing the vector: linear O(N)
Reference:
https://www.geeksforgeeks.org/arrays-in-c-cpp/
https://www.geeksforgeeks.org/vector-in-cpp-stl/
Leave a comment