Can I run this straight away:
Pokemon pikachu = ...
pokemons[23].push_back(Pikachu);
Not straight away, because your pokemons
array does not have any elements yet, so pokemons[23]
will be an out-of-bounds error.
Otherwise, once your pokemons
array is populated, then yes, you can just push_back
on one of its elements.
Or do I need to:
pokemons.reserve(100);
for (int i =0;i<100;i++) pokemons.push_back(vector<Pokemon>());
reserve()
is only to allocate (reserve) memory for your vector, if you have a good idea of how many elements you will end up having, to avoid multiple memory allocations and potential moves. You do not need to reserve()
, but it is a good idea if you will need a decent amount of memory.