Ever since I started the competitive programming, I always found that people always used to initialize the single or 2D arrays based on a constant maximum value from the maximum value of the input specification.
But I've always wondered why people do that when you can initialize the array based on the input.
For example, let's say a problem has an input specification of zero to ten to the power of five when receiving the input for the size of an array (or a vector).
Why do people do this:
Notation: The integer n
represents the input, and a
represents the array:
#include<bits/stdc++.h>
using namespace std;
const int MXN = 1e5;
int a[MXN], n;
int main() {
cin >> n;
for(int i = 0; i<n; i++)
cin >> a[i];
}
Instead of this:
#include<bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[n];
for(int i = 0; i<n; i++)
cin >> a[i];
}
Or is this the best method (which is what I normally do):
#include<bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int>a(n);
for(int i = 0; i<n; i++)
cin >> a[i];
//for(auto &i:a) cin >> i;
}
Is there any advantages of doing this? Thank you.