I want to implement a code similar to the below-mentioned one using vectors in the most efficient and easiest manner. How do I do it?
int a[1005];
for(int i=1;i<=1000;i+=5)
a[i]=i*i;
When I try doing the same using vectors,compiler throws assertion failed error..subscript out of range...
vector<int> a;
for(int i=1;i<=1000;i+=5)
a[i]=i*i;
So,I tried pushback function...However the index of vector could only be increased from 0 to number of calls to pushback and not in the way I want i.e 1,6,11,16
vector<int> a;
for(int i=1;i<=1000;i+=5)
a.pushback(i*i);
Declare the vector with the size you want as a constructor argument:
vector<int> a(1000);