Advantages of vector over array in C++
Last Updated :
30 Jan, 2023
We have already discussed arrays and vectors. In this post, we will discuss advantages of vector over normal array. Advantages of Vector over arrays :
- Vector is template class and is C++ only construct whereas arrays are built-in language construct and present in both C and C++.
- Vector are implemented as dynamic arrays with list interface whereas arrays can be implemented as statically or dynamically with primitive data type interface.
CPP
#include <bits/stdc++.h>
using namespace std;
int main()
{
int array[100];
int * arr = new int [100];
vector< int > v;
return 0;
}
|
Size of arrays are fixed whereas the vectors are resizable i.e they can grow and shrink as vectors are allocated on heap memory.
CPP
#include <bits/stdc++.h>
using namespace std;
int main()
{
int array[100];
cout << "Size of Array " << sizeof (array) / sizeof (array[0]) << "\n" ;
vector< int > v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
v.push_back(5);
cout << "Size of vector Before Removal=" << v.size() << "\n" ;
for ( auto it : v)
cout << it << " " ;
v.erase(v.begin() + 2);
cout << "\nSize of vector After removal=" << v.size() << "\n" ;
for ( auto it : v)
cout << it << " " ;
return 0;
}
|
Output
Size of Array 100
Size of vector Before Removal=5
1 2 3 4 5
Size of vector After removal=4
1 2 4 5
Arrays have to be deallocated explicitly if defined dynamically whereas vectors are automatically de-allocated from heap memory.
CPP
#include <bits/stdc++.h>
using namespace std;
int main()
{
int * arr = new int [100];
delete [] arr;
vector< int > v;
return 0;
}
|
Size of array cannot be determined if dynamically allocated whereas Size of the vector can be determined in O(1) time.
When arrays are passed to a function, a separate parameter for size is also passed whereas in case of passing a vector to a function, there is no such need as vector maintains variables which keeps track of size of container at all times.
CPP
#include <bits/stdc++.h>
using namespace std;
int main()
{
int * arr = new int [100];
cout << "Size of array= " ;
cout << sizeof (arr) / sizeof (*arr) << "\n" ;
return 0;
}
|
When array becomes full and new elements are inserted; no reallocation is done implicitly whereas When vector becomes larger than its capacity, reallocation is done implicitly.
Arrays cannot be returned unless dynamically allocated from a function whereas vectors can be returned from a function.
CPP
#include <bits/stdc++.h>
using namespace std;
int * getValues()
{
int arr[10];
for ( int i = 0; i < 10; i++)
arr[i] = i + 1;
return arr;
}
int main()
{
int * array;
array = getValues();
for ( int i = 0; i < 10; i++) {
cout << "*(array + " << i << ") : " ;
cout << *(array + i) << endl;
}
return 0;
}
|
Output:
warning: address of local variable 'arr' returned [-Wreturn-local-addr]
Segmentation Fault (SIGSEGV)
CPP
#include <bits/stdc++.h>
using namespace std;
vector< int > getValues()
{
vector< int > v;
for ( int i = 0; i < 10; i++)
v.push_back(i + 1);
return v;
}
int main()
{
vector< int > get;
get = getValues();
for ( auto it : get)
cout << it << " " ;
return 0;
}
|
Output
1 2 3 4 5 6 7 8 9 10
Arrays cannot be copied or assigned directly whereas Vectors can be copied or assigned directly.
CPP
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector< int > v;
for ( int i = 0; i < 10; i++)
v.push_back(i + 1);
vector< int > get;
get = v;
cout << "vector get:\n" ;
for ( auto it : get)
cout << it << " " ;
int arr[10];
for ( int i = 0; i < 10; i++)
arr[i] = i + 1;
int copyArr[10];
copyArr = arr;
return 0;
}
|
Output:
vector get:
1 2 3 4 5 6 7 8 9 10
error: invalid array assignment
copyArr=arr;
Following are the list of advantages of vector over array:
- Resizing: Vectors can dynamically resize themselves, while arrays have a fixed size.
- Memory allocation: Vectors handle memory allocation and deallocation automatically, while arrays require manual management.
- Element access: Vectors provide direct access to elements using an iterator, while arrays require using an index.
- Sorting: Vectors provide built-in sorting functions, while arrays require manual sorting.
- Insertion and deletion: Vectors provide efficient insertions and deletions at the end, while arrays require shifting of elements.
- STL compatibility: Vectors are a part of the Standard Template Library (STL) and are compatible with other STL functions and algorithms.
- Exception handling: Vectors have built-in exception handling for out-of-bounds access, while arrays do not.
- Standardization: Vectors are a standardized feature of modern programming languages, and therefore more widely supported
- Memory utilization: Vector can save memory space if the number of elements increases after the array created by using dynamic memory allocation.
- Iterators: Vectors provide iterators that make it easy to traverse the elements, whereas arrays require manual pointer manipulation.
- Better cache performance: Vectors allow for better cache performance.
- Simplified code: Using vectors instead of arrays can simplify the code as it eliminates the need for manual memory management.
- Highly expressive: Vectors are more expressive and easy to understand than arrays, as they have names and provide more useful methods.
- Exception safety: Vectors provide exception safety, which means that they are less likely to cause errors and crashes.
Share your thoughts in the comments
Please Login to comment...