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 :-(.
begin
end
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
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:
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
andend
boilerplate. Though no standard library implementation ship that yet :-(.