Why can not we enter a vector when we enter a C ++ array?

advertisements

The wiki says:
The elements of a vector are stored contiguously. AND
Vectors allow random access; that is, an element of a vector may be referenced in the same manner as elements of arrays (by array indices).

So why can't we input the elements of a vector as:

vector<int> v;
for(int i=0;i<3;i++)
{
    cin>>v[i];
}


Your vector has zero elements right now. Try allocating it some space as:

vector<int> v(5);

Then your method would work.