Rohan Akut
Rohan Akut

Reputation: 33

What is the difference in the following vector creations?

vector<int>myvec;

and

vector<int>myvec[20];

When i tried to do a standard push_back operation in second case(myvec.push_back(41))i got a compile error. I think i need to mention the position where 41 needs to be inserted i this case.Am I right?

Upvotes: 1

Views: 65

Answers (1)

cdhowie
cdhowie

Reputation: 168978

The second declaration does not create a single vector, it creates an array of 20 vectors. You would have to do myvec[0].push_back(...) instead, to add an element to the first vector in the array.

Upvotes: 7

Related Questions