I was just about to put this into the wiki but my account was flagged for some reason. I'll just put it here for now and edit this to just include link when GitHub support fixes the issue.
This is my first contribution so all feedback is appreciated.
Title: How to use sf::Vector2f as a key-type in unordered associative containers
#include <SFML/Graphics.hpp>
#include <iostream>
#include <string>
#include <unordered_map>
struct Key
{
sf::Vector2f key;
bool operator==(const Key &other) const
{
return (key.x == other.key.x && key.y == other.key.y);
}
};
struct KeyHasher
{
std::size_t operator()(const Key& k) const
{
return ((std::hash<float>()(k.key.x) ^ (std::hash<float>()(k.key.y) << 1)) >> 1);
}
};
int main()
{
std::unordered_map<Key, std::string, KeyHasher> Map =
{
{ { sf::Vector2f(1, 1) }, "one" } ,
{ { sf::Vector2f(2, 2) }, "two" }
};
// Individual Call
std::cout << Map[{ sf::Vector2f(2, 2) }];
// Loop
for (auto& k : Map)
{
std::cout << k.second << std::endl;
}
return 0;
}