0) An empty vector is not a special case. There is no benefit to making that check separately. If the vector is empty, then .begin() == .end(), and the loop simply executes zero times, which is exactly the behaviour you want.
1) You are trying to check if the tempbox is "inside" each sprite, where tempbox is default-constructed the first time around. What position will you be checking?
2) I assume "inside" actually checks if two things intersect, not if one is inside the other. You should name it accordingly.
3) It looks like you're *rejecting* the new sprite if it is *not* "inside" any given existing sprite, i.e. requiring it to be "inside" *all* of them. I strongly suspect that's the opposite of what you want.
4) When you detect a collision, you reset 'iter = Sprites.begin()', but then the end of the loop is reached, causing the 'iter++' to happen. That means that the first sprite will not be checked against the new box position.
What you want to do is separate the "check for collision against any sprite" step from the "generate a random coordinate" step. The process logically looks like this:
To spawn: Create a box. do: set the box's position. while intersects_with_anything(box). Add the box to Sprites.To test intersects_with_anything(box): for each sprite in Sprites: if inside(box, sprite): answer True. answer False.
In C++, that becomes:
bool inside_anything(const Sprite& box) { for (vector<Sprite>::iterator iter = Sprites.begin(); iter != Sprites.end(); ++iter) { if (inside(box,(*iter))) { return true; } } return false;}void spawnsprites() { Sprite tempbox; do { tempbox.body->SetOriginPosition( b2Vec2(screen->w ,rand() % (screen->h-tempbox.height/2)),0); } while (inside_anything(tempbox)); // i.e., until not inside anything. Sprites.push_back(tempbox);}