Data Structure to Store a Map

In the past, I have stored all of my maps inside of multidimensional vectors, but I find it too irritating to work with. I recently tried to use a std::map, but it didn't work out as planned.
std::map< sf::Vector2<int>, int > myMap;
Apparently, the object of an sf::Vector2<int> cannot be in the Key slot because it has two values? Oh well...

My goal is to store the map inside of a data structure that I can store the coordinates and the integer value of the point inside. A std::map would've been great. If someone can think of a better way to store and access a map, please be my guest. Otherwise, I need to know what data structure would be best to use to store a binary map of numbers. Maybe something in STL, SFML, Boost... I don't know. Haha. I am at a loss for ideas after trying to figure out why my std::map wasn't work for 2 hours. Hahahaha!

If we can't come up with any good ideas, I have a backup plan. ;)
typedef std::pair<sf::Vector2i, int> terrain;
> Apparently, the object of an sf::Vector2<int> cannot be in the Key slot because it has two values?

It cannot be the key because the map needs to compare keys, the default for that is to use std::less<>, and no operator< is defined for objects of type sf::Vector2<int>

Provide a binary predicate for comparison and it can be the key in a std::map<>. For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
struct X
{
	int a ;
	int b ;
};

struct compare_X
{
    inline bool operator() ( const X& first, const X& second ) const
    { return first.a == second.a ? first.b < second.b : first.a < second.a ; }
};

int main()
{
    std::map< X, int, compare_X > map ;

    map[ X{22,33} ] = 100 ;
}


Important: The binary predicate for comparing keys must be one that imposes a strict weak ordering. http://www.sgi.com/tech/stl/StrictWeakOrdering.html
Last edited on
Would it work if I extended the sf::Vector2<int> class to contain the comparison operator?
Yes, it would - a bool sf::Vector2<int>::operator< ( const sf::Vector2<int>& that ) const ;

It may be simpler to write a function object, and provide that as the third template parameter to the map.

Last edited on
Okay thank you. :)
Topic archived. No new replies allowed.