SFML community forums

Help => Graphics => Topic started by: schmitty on December 15, 2012, 09:41:32 pm

Title: A std::map of sf::vector2f
Post by: schmitty 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!
Title: Re: A std::map of sf::vector2f
Post by: Nexus 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;