Some basics concepts of Vectors in C++

I couldn’t understand why can’t we use v[i] for inserting elements?

Hi @Tiwary725,
Vectors are dynamic array. So, when an element is inserted in vector, it changes its size since we generally do not define its size in the beginning. So,we have to use push_back while inserting elements in order to allocate more memory to vector so that vector could change its size. If we use v[i] for inserting element when i>size of vector, it will give error(since we haven’t defined that part of vector yet).
But still if u want to use v[i] to insert, u can declare the size of vector in beginning using
vector v(n); here n is size of vector u want to make. But if u want to add a (n+1)th element now, u will have to use push_back size u have defined size of vector to be n.

Hi, if u still have any doubt regarding dis topic, u can post it here.