Definition of C++ vector and array
Vectors in C++ are sequential containers that allow the continuous storage of elements. Vectors are dynamic in nature which can be resized on the insertion and deletion of elements. As it is not index-based, accessing elements is done through the use of iterators and is more time-consuming. Vector is a template class and is only a C++ construct which means it needs to be shipped from the C++ library if it needs to be used in the program.
Arrays in C++ are fixed-sized, lower-level data structure which allows the index-based storing of elements. It can be considered as a collection of the same type of elements stored in contiguous memory locations. These are static in nature and it cannot be resized on insertion and deletion of elements. As it is an index-based structure, accessing elements in Arrays takes constant time providing only the index. Arrays are faster and give high performance when accessing the elements.
Head to Head Comparison Between C++ vector vs array (Infographics)
Below is the top 8 differences between C++ vector vs array.
Key Differences between C++ vector vs array
Some of the key differences between the C++ vector and array are given below:
- Vector is best for the programmers in case of frequent insertion and deletions whereas Arrays are best to work within case of frequent access of elements.
- Vector is a sequential container which stores the elements dynamically whereas an array is a sequential collection of elements that allows the index-based storage of elements.
- Vectors allow the access of elements using the subscript operator whereas array allows the accessing of elements using the direct indexes.
- Vector is a template class having a parent as Collection class which is shipped from C++ library whereas Vector is a lower-level data structure which is used normally and has its own functions and methods.
- Arrays are very efficient in terms of performance, speed, and supporting multiple dimensions whereas Vectors is a type-safe version.
- Automatic deallocation of memory is done in the case of Vectors whereas arrays need to be de-allocated explicitly if allocated dynamically by the programmer.
- When it comes to working with functions, size value need not be passed as a parameter in the case of vectors as it maintains variables to keep the size of container whereas size needs to be passed as a parameter in the case of Arrays.
- Programmers can return the vector from the function whereas arrays can not be returned as a function in the code.
- The basic syntax of initializing the array in C++:
int marks = [100, 50, 46, 67, 87];
Syntax of initializing the vector in C++ :
vector <int> marks;
//elements in the vector can be added using the push_back() function
- All the elements in the Arrays need to be of the same type and are stored sequentially and in continuous memory, whereas it is not so in case of Vectors.
- Vectors make use of the pointers in order to access the container elements whereas array elements are accessed normally by providing the index.
C++ Vector vs C++ Array Comparison Table
Below given is the head to head comparison table between the C++ vector and array:
S.No. | C++ Vector | C++ Array |
1. | Vector is a template class in C++ that will be shipped from the C++ library if needed to use the vector functions. | Array is not a template class but is a lower-level data structure which can be used anytime. |
2. | Vectors in C++ can be considered as a dynamic array whose size can be changed with the insertion and deletion of elements. | Array in C++ can be considered as a static array whose size is fixed once initialized. |
3. | When it comes to memory management, vectors occupy more memory in comparison to Arrays. | Arrays are memory efficient and occupy less memory than vectors. |
4. | As the Vectors follow the dynamic structure, so it takes more time to access the elements in Vectors. | Arrays are static in nature so it takes constant time to access any element in the array using the index operator. |
5. | The size of the vectors is not fixed (it can be resized) and it can grow and shrink on insertion and deletion. They are allocated on heap memory. | The size of the array is fixed, unlike vectors. |
6. | Reallocation of memory in the case of Vectors is done implicitly. | Reallocation of memory in case of Array is not done implicitly. |
7. | In programming, vectors can be copied or assigned directly. | In programming, arrays can never be copied or assigned directly. |
8. | It follows the non-index based structure as the elements are stored dynamically | It follows the index-based structure with the elements stored in the contiguous memory having the first element at the lowest address and the last element at the highest address. |
Examples
Let us discuss examples of C++ vector vs array.
Example #1 – Vector in C++
Code:
#include <vector>
using namespace std;
int main() {
// Initializing the vector of integer values
vector<int> marks;
// Inserting the elements in the vector
marks.push_back(10);
marks.push_back(45);
marks.push_back(89);
marks.push_back(87);
// Getting the size of vector using the size() function
cout<< "Vector size is : "<< marks.size() << endl;
cout<<"Elements of the vector are as follows :"<<endl;
// Iterator to retrieve the elements of vector
for (auto it : marks)
cout << it << " ";
cout << endl;
//Removing the first vector element using erase() function
cout << "Removing the first vector element "<< endl;
marks.erase(marks.begin());
cout<< "Vector size is : "<< marks.size() << endl;
cout<< "Now the vector elements are : "<< endl;
for (auto it : marks)
cout << it << " ";
return 0;
}
Output:
Example #2 – Array in C++
Code:
#include<iostream>
#include<array>
using namespace std;
int main()
{
//Initialising the array
int marks[10] = {90, 87, 76, 56, 67};
// getting the size of array
cout << "Size of array is : ";
cout << sizeof(marks) / sizeof(marks[0]) <<endl;
// Printing elements of the array marks
cout << "The array elements are given below : ";
for ( int x=0; x<5; x++)
cout << marks [x] << endl;
return 0;
}
Output:
Conclusion
Above description clearly explains what is a vector and array in C++ and the major differences between the two. Arrays and vectors both are commonly used data structures to store the elements sequentially. As both have different features and are used in different scenarios, so the programmer needs to understand the requirements first and then implement them according to it.
Recommended Article
This has been a guide to the top differences between C++ vector vs array. Here we also discuss the key differences with infographics and comparison table. You may also have a look at the following articles –
4 Online Courses | 5 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses