and how can I do it in C++?
You can use any type as a map key, as long as it implements an operator<
(plus the usual copy-and-assign requirements for values stored in containers).
For instance:
struct example { int x; }
bool operator < (const example &l, const example &r) { return l.x < r.x; }
std::map<example, int> values;
Alternatively, you may provide a comparison function as the third argument of the map template instead of defining operator<
. More details here (parameter Compare
).