A vector represents a dynamic sized array in the Standard Template Library(STL) that automatically grows when elements are added beyond current capacity.
- A programmer does not have to worry about maintaining the capacity and allocating extra space initially.
- Certain insertions and removals can become slightly costly in terms of time as it internally resizes itself beyond certain predefined limits. If time taken for these operations is super critical, then use of plain arrays in C++ should be preferred.
- Vectors support bound checking by using v.at(i) for accessing an element at index i in a vector v.
Declaration and Initialization of a Vector
A vector is defined as the std::vector class template in the <vector> header file.
vector<T> v;
where T is the data type of elements and v is the name assigned to the vector.
Operations in Vector
Vectors in C++ support various useful operations that allow you to add, remove, access, and modify elements dynamically.
Insert Elements
- push_back(value): Inserts element at the end of the vector efficiently. Takes constant time O(1) on average, so it's very fast.
- insert(position, value): Inserts element at any position (beginning, middle, or end). Takes linear time O(n), as it shifts elements to make space.
Access elements
- We can access elements in a vector using v[i] for direct access or v.at(i) for bounds-checked access.
- Both return the element at index i, but at(i) throws an exception if i is out of range.
- It takes constant time O(1) on average, so it's very fast.
OutputElement at index 2 using []: 30
Element at index 3 using at(): 40
Update elements
- To update an element, you simply use its index with the assignment operator, for example v[i]=newValue;
- This replaces the old element at index i with the new value you want.
OutputOriginal value at index 1: 20
Updated value at index 1: 50
Find Vector Size
We can find the size(number of elements) of a vector using the size() function, like v.size().
Traverse Vector
- Traversing a vector means going through each element one by one, usually using a loop.
- We can use a for loop with an index (v[i]), a range-based for loop (for(int x: v)), or iterators.
- It takes linear time O(n) .
Recommendation:
- Use "for (char &x : v)" when you want to modify the elements of the vector.
- Use "for (const char &x : v)" for read-only access, as it avoids copying and improves efficiency.
Delete Elements
- Elements can be deleted from a vector using erase(), which requires an iterator pointing to the element. It takes linear time O(n) because all elements after the erased one need to be shifted.
- To remove the last element efficiently, use pop_back() . It takes constant time O(1) on average, so it's very fast.
Vector is Empty
- To check if the vector is empty we use empty() function. It returns true if the vector has no elements ,and false otherwise.
- This is useful before accessing or modifying the vector to avoid errors.
OutputVector is empty.
Vector is not empty. First element 100
Multidimensional Vectors
Multidimensional vectors are dynamic arrays that can store data in more than one dimension, like tables or grids. They are implemented using vector inside another vector, allowing flexible row-column (2D), or even higher-dimensional structures.
Syntax to declare a 2D vector:
vector<vector<int>> matrix;
Below is an example of creating a 2D vector and traversing over it: