Vector in C++
Vector in C++
In today’s article, we will be learning all the things about vector in C++ and how vector is different form an array in C++.
So basically, vector is very similar to an array in nature but the difference in vector over array is that the size of the array is dynamic and if we talk about an array then the size of an array size is static. Here is an example of an array when we creating an array then we need to put the size inside the square boxes and that would be like:-
int items[19]
Or we can assign something that would help in determining the size and that would be a sequence of values and example for this would be like:-
int items[ ]= {9,10}
but in vector it does not work like this, we can assign vector like this
std:vector<int> item;
We can also assign a sequence of values in vector also like we do in array but it would be like
std:vector<int>item={9,10}
Vector definition
In C++, the most common purpose of the containers is Vector. The vector in C++ supports the dynamic array. That means is an array which can grow when needed. As we know that in C++ the size of the array is fixed at the compiler time.
Although this the best and efficient way for implementing the array, but on the other side of this it also restrictive because we are not allowed to adjust the size of the array at the run time if we need to change some programming conditions. But here comes the role of vector in C++, vector solved this problem by allocating the memory as needed. Vectors in C++are also referred as sequence containers which utilize the continuous storage location to store the elements. But yes as result to this vector also consume more memory to handle the storage in compare to array.
Vector is dynamic in nature but we can use the standard array subscript notations to access the element of the vector.
So the template specification for the vector is given here and that would be like:-
Template<class T, class Allocator = allocator<T>> class vector.
T , here is the type of data that how it’s being stored and allocator specifies the allocator.
How is vector different from array in C++?
Difference between vector and array is as follow:-
Vector | Array |
Vector occupies more memory than an array occupies. | Basically arrays are much more memory efficient. |
To create a vector, we need sequential containers to store the elements. | To create the array, we only need original data structure, based on the index concept. |
In vector, the structs and classes are the same in C++. | In array, it does not support class-based declaration but instead of that, it has the interface to support the objects. |
The length of the vector varies. | The length of the array is fixed size. |
The structure of vector is template class and C++ construct. | The structure of the array is contiguous memory location. |
Talking about resizing in vector it is dynamic in nature. | Talking about array it is static and also named as strong typing discipline in terms of resizing. |
In vector the access element is time consuming although it is based on position of element. | In array the access element is constant and time operation irrespective of the elements location. |
Vector is based on non- index structure. | Array is basically based on index based with the lowest address as first, and the highest address as the last. |
As we know vectors are the sequential containers in C++. | Other side array is the lower level data structure in C++. |
Vector is basically come from the template class of C++ with the parent as the collection class. | On the other hand,arrays are the lowerlevel data structure with their own specific properties. |
- Vector is best for frequent insertion and deletion of element because it automatically increases the size when element is inserted and automatically decreases the size when element is deleted.
Here are some comparisons operators are defined for vector and those are as follow:-
==, <, <=, !=, >, >=.
Subscripting operator which is [] is also defined for vector. That means it allows us to access the elements of the vector using the standard array subscripting notation.