Could a variable of Data
be used as a map key?
struct Data {
Data(int X, int Y) {x=X; y=Y;}
int x; int y;
}
int main()
{
std::map<Data, int> map_;
map_.insert(std::make_pair(Data(1,2), 0)); //error inserting
}
Could a variable of Data
be used as a map key?
struct Data {
Data(int X, int Y) {x=X; y=Y;}
int x; int y;
}
int main()
{
std::map<Data, int> map_;
map_.insert(std::make_pair(Data(1,2), 0)); //error inserting
}
Yes, but you either need to define operator<
for the class type or use a custom comparison function for the std::map
.
There is an example of using a custom comparison function in the STL documentation.
if you don't want an operator<, you can use boost::unordered_map.