re: Vector for() iterator returning a null (or zero) object for each iteration (solved) VIEW POST


You can try to use the STL algorithms for this kind of task and they avoid common mistakes when using loops. They should have almost the same code generated as your loop in your scenario. For example:

const auto result = std::find_if(std::begin(tiles), std::end(tiles), [](const Tile& t){
  sf::IntRect(t.getX(), t.getY(), t.getX() + 32, t.getY() + 32).contains(sf::Vector2i(x, y));
return (result != std::end(tiles)) ? *result : tiles.front();

You can watch the Sean Parent's C++ Seasoning video (a must watch to me) for more discussion on this topic.

Also, C++20 ranges would finally remove the stupid begin and end boilerplate. Though no standard library implementation ship that yet :-(.

code of conduct - report abuse