1
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?

Rohan Akut
  • 33
  • 1
  • 7
  • 2
    One is an array of vectors the other isnt... [Maybe this will help?](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Borgleader Mar 30 '17 at 20:05
  • 1
    You probably meant the second example to be `vector myvec(20);` which creates a vector of 20 ints. – doug Mar 30 '17 at 20:15

1 Answers1

7

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.

cdhowie
  • 158,093
  • 24
  • 286
  • 300