I am curious about how pushing back into a vector works. I want a way to push back an element and then be able to add it’s location in the vector to a double array serving as a type of map.
Something like this:
// Create a bomb
Bomb b;
b.currentTime = SDL_GetTicks();
b.explodeTime = SDL_GetTicks() + 3000;
b.owner = player;
b.power = 2;
b.x = x;
b.y = y;
bombVec.push_back(b);
bombs[y][x] = THIS_IS_WHAT_I_WANT;
This way when I explode the bomb, I can check the map and then have an ID in the vector to deal with. Every non bomb square will have a -1. Also, just curious. Imagine I have 3 elements in a vector. I delete the second one and then add another. Does the new element go in the same location as the one that was deleted?
Thanks!
After you
push_back, you can usebackto get the element.Or do you want the index?
Obligatory warning: If you push another item in, and the capacity isn’t large enough, the vector will be resized. This will invalidate the reference and will cause program errors if the reference is accessed. This isn’t a problem with (1) accessing by index (2) if pointers are stored in the
vector(3) if you uselistinstead (4) if you don’t resize thevector.Getting the index: