Vector vs Arrays. Please help me clear this doubt!

Hey, I just wanted to know if arrays are faster than vectors? I came across a few questions, where the solution got accepted on using array and rejected on using a vector. And the time difference was quite a lot. Can someone explain why? Because i was always taught that using vectors over arrays is always better!!!

I really need help on this issue. Kindly help. Do link resources on this topic if you feel like.

Just learn one thing , the complex the data structure more the time constant factor of time complexity , and most probably you would not have specified the size initially of vector thus increasing its constant factor even further.

2 Likes

The following are the differences between vector and array −

Vector is a sequential container to store elements and not index based.
Array stores a fixed-size sequential collection of elements of the same type and it is index based.
Vector is dynamic in nature so, size increases with insertion of elements.
As array is fixed size, once initialized can’t be resized.
Vector occupies more memory.
Array is memory efficient data structure.
Vector takes more time in accessing elements.
Array access elements in constant time irrespective of their location as elements are arranged in a contiguous memory allocation.
Vectors and arrays can be declared with the following syntax −

Vector declaration:vectorarray name;
Array declaration:type array_name[array_size];
Vector initialization:vectorarray name={values};
Array initialization:datatype arrayname[arraysize] = {values};
std::vector:
Example Code
#include
#include
using namespace std;
int main() {
vector<vector>v{ { 4, 5, 3 }, { 2, 7, 6 }, { 3, 2, 1 ,10 } };
cout<<“the 2D vector is:”<<endl;
for (int i = 0; i < v.size(); i++) {
for (int j = 0; j < v[i].size(); j++)
cout << v[i][j] << " ";
cout << endl;
}
return 0;
}
Output
the 2D vector is:
4 5 3
2 7 6
3 2 1 10
std:: array:
Example Code
#include
#include
using namespace std;

int main() {
array<int,4>a = {10, 20, 30, 40};
cout << "The size of array is : ";
//size of the array using size()
cout << a.size() << endl;
//maximum no of elements of the array
cout << "Maximum number of elements array can hold is : ";
cout << a.max_size() << endl;
// Printing array elements using at()
cout << "The array elements are (using at()) : ";
for ( int i=0; i<4; i++)
cout << a.at(i) << " ";
cout << endl;
// Filling array with 1
a.fill(1);
// Displaying array after filling
cout << "Array after filling operation is : ";
for ( int i=0; i<4; i++)
cout << a[i] << " ";
return 0;
}
Output
The size of array is : 4
Maximum number of elements array can hold is : 4
The array elements are (using at()) : 10 20 30 40
Array after filling operation is : 1 1 1 1

1 Like

all right man, I get some idea. But could you help me understand that when you look at a problem, how do you decide what to use? You must have attempted many problems, there must be a pattern, right?

You can probably use either with minimal consequences.
But obviously, when you know how many elements there will be, use an array. When you don’t, use a vector. Using a vector in both cases should probably not make much of a difference

For example, when you want an array of prime numbers between say 2 and N, you don’t know how many prime numbers there are. So you could use a vector

1 Like

You can use whichever you want , It is rare that during contests such things give TLE , by such I mean using vector in place of array and long long int in place of int .

1 Like

ok

ohh now i get it. thanks

For me, there has been precisely one time when using a vector instead of an array was the difference between TLE and AC, and that was in this problem (which requires a 3D array).

Variable-length arrays like you often see in CP solutions are invalid C++ anyway, and are prone to Undefined Behaviour if they are too large.

I always just use a vector unless there are very good reasons not to. Bonus: you can deterministically find out-of-bounds access just by setting a compiler flag (like here :slight_smile: )

1 Like