Updated July 3, 2023
Definition of C++ vector and array
In C++, vectors are sequential containers that enable continuous elements storage. Vectors are template classes and are specific to C++, meaning they must be included in the C++ library if they are to be used in a program. In C++, arrays are lower-level data structures with a fixed size and allow for index-based storage 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 are the top 8 differences between C++ vector vs array.
Key Differences between C++ vector vs array
- Vector is best for the programmers in case of frequent insertion and deletions, whereas Arrays are best to work in case of frequent access of elements.
- A vector is a sequential container that 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 access of elements using the direct indexes.
- 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.
- 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 the case of Vectors.
- Vectors use pointers 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. | In C++, the vector class is a template class that is provided by the C++ library. When needed, the vector class is included in the C++ library to use its functions and features. | The array is not a template class but is a lower-level data structure that can be used anytime. |
2. | In C++, vectors can be seen as dynamic arrays that can be resized when elements are inserted or deleted. | In C++, arrays can be seen as static data structures where the size is fixed once the array is 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, 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 to 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 the case of Array is not done implicitly. |
7. | In programming, vectors can be directly copied or assigned to another vector. | In programming, arrays can never be copied or assigned directly. |
8. | Vectors in C++ follow an index-based structure, where elements are stored sequentially and can be accessed using index values. | It follows the index-based structure with the elements stored in the contiguous memory, with the first element at the lowest address and the last at the highest. |
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
The above description clearly explains what is a vector and array in C++ and the major differences between the two. However, they have different features and are suited for different scenarios. The programmer needs to understand the requirements of the program and choose the appropriate data structure accordingly.
Recommended Article
This has guided 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 –