Yoho all, Is it possible to use a sf::Vector*<*> as the key for a std::map? I've been trying to use it and I keep getting errors about it attempting to compare them past checking the x/y/z matches up.
The Error being " /usr/include/c++/4.6/bits/stl_function.h|236|error: no match for ‘operator<’ in ‘__x < __y’| "
Here's minimal code just to show it.
#include <SFML/System.hpp>
#include <map>
int main()
{
std::map<sf::Vector3i,int> mymap;
mymap[sf::Vector3i(1,1,1)] = 1;
return 0;
}
Is there any solution for me or should I just use strings or something as the key instead (obviously putting the numbers together in a stringstream).
Yes it's possible, since the requirements for the key type are that it must be copiable and assignable.
But since std::map is ordered it also requires a ordering function, by default (i.e. if you don't specify one) it will use operator<, which for sf::Vector3<T> is not declared (see documentation (http://www.sfml-dev.org/documentation/2.0/classsf_1_1Vector3.php)), that's why you're getting that error.
The std::map deceleration is:
template < class Key, class T, class Compare = less<Key>, class Allocator = allocator<pair<const Key,T> > > class map;
For which the reference (http://www.cplusplus.com/reference/stl/map/) states:
- Key: Type of the key values. Each element in a map is uniquely identified by its key value.
- T: Type of the mapped value. Each element in a map is used to store some data as its mapped value.
- Compare: Comparison class: A class that takes two arguments of the key type and returns a bool. The expression comp(a,b), where comp is an object of this comparison class and a and b are key values, shall return true if a is to be placed at an earlier position than b in a strict weak ordering operation. This can either be a class implementing a function call operator or a pointer to a function (see constructor for an example). This defaults to less<Key>, which returns the same as applying the less-than operator (a<b).
The map object uses this expression to determine the position of the elements in the container. All elements in a map container are ordered following this rule at all times. - Allocator: Type of the allocator object used to define the storage allocation model. By default, the allocator class template is used, which defines the simplest memory allocation model and is value-independent.
Thus you'll have to define a compare functor to pass as third argument of the vector decelration. ;)