Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: A std::map of sf::vector2f  (Read 1955 times)

0 Members and 1 Guest are viewing this topic.

schmitty

  • Newbie
  • *
  • Posts: 6
    • View Profile
A std::map of sf::vector2f
« on: December 15, 2012, 09:41:32 pm »
Is it possible to create a map with the key being an sf::vector2f? I think i need to overload the comparison operator to get it to work. If it is possible, could someone kindly explain how to accomplish this?

Thanks!

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: A std::map of sf::vector2f
« Reply #1 on: December 15, 2012, 09:47:07 pm »
Don't overload operator<, since it doesn't mathematically make sense for vectors.

You can write a functor. Here I just add the components, you can also check for x coordinates first and then y (lexicographically), for this std::pair already provides what you want.
struct VectorComparator
{
    bool operator() (sf::Vector2f lhs, sf::Vector2f rhs) const
    {
        return lhs.x + lhs.y < rhs.x + rhs.y;
    }
}

The associative STL containers take a further template parameter that specify the ordering criterion.
std::map<sf::Vector2f, Value, VectorComparator> map;
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: