c++ c++11

c++ - Struct como clave en un std:: map



struct vs class c# (3)

struct coord { int x, y; bool operator=(const coord &o) { return x == o.x && y == o.y; } bool operator<(const coord &o) { return x < o.x || (x == o.x && y < o.y); } }; map<coord, int> m; pair<coord, int> p((coord{0,0}),123); m.insert(p); // ERROR here

¿Cómo puedo usar una estructura como clave para un mapa?

EDITAR:

Cambió el código a esto:

struct coord { int x, y; bool const operator==(const coord &o) { return x == o.x && y == o.y; } bool const operator<(const coord &o) { return x < o.x || (x == o.x && y < o.y); } };

aún recibiendo este error:

C: / Users / tomc / Desktop / g> mingw32-make g ++ test.cpp -std = c ++ 0x En el archivo incluido desde c: / mingw / bin ../ lib / gcc / mingw32 / 4.5.2 / include / c ++ / string: 5 0: 0, desde c: / mingw / bin ../ lib / gcc / mingw32 / 4.5.2 / include / c ++ / bits / loc ale_classes.h: 42, desde c: / mingw / bin. ./lib/gcc/mingw32/4.5.2/include/c++/bits/ios_base.h: 43, desde c: / mingw / bin ../ lib / gcc / mingw32 / 4.5.2 / include / c ++ / ios : 43, desde c: / mingw / bin ../ lib / gcc / mingw32 / 4.5.2 / include / c ++ / ostream: 40, desde c: / mingw / bin ../ lib / gcc / mingw32 / 4.5.2 / include / c ++ / iostream: 40, de test.cpp: 1: c: / mingw / bin ../ lib / gcc / mingw32 / 4.5.2 / include / c ++ / bits / stl_function.h: En la función miembro ''bool std :: less <_Tp> :: operator () (const _Tp &, const _Tp &) const [with _ Tp = coord] '': c: / mingw / bin ../ lib / gcc / mingw32 / 4.5.2 / include / c ++ / bits / stl_tree.h: 1184: 4: se ha identificado desde ''std :: pair, bool> std :: _ Rb_tree <_Key, _Val, _KeyOfValue, _Compare, _Alloc> :: _ M_insert_unique (const _Val &) [with _Key = answer , _Val = std :: pair, _KeyOfValue = std :: _ Select1st>, _Compare = std :: less, _Alloc = std :: alloc ator

] ''c: / mingw / bin ../ lib / gcc / mingw32 / 4.5.2 / include / c ++ / bits / stl_map.h: 501: 41: se establece desde'' std :: pair, std :: _ Select1st>, _Compare, nombre tipográfico _Alloc :: rebind :: value_type> :: other> :: iterator, bool> std :: map <_Key, _Tp, _Compare, _Alloc> :: insert (const std :: map <_Key, _Tp, _Compare, _ Alloc> :: value_type &) [con _Key = coord, _Tp = int, _Compare = std :: less, _Alloc = std :: allocator>, typename std :: _ Rb_tree <_ Key, std :: pair, std :: _ Select1st >, _ Compare, nombre de tipo _Alloc :: rebind :: value_ty pe> :: other> :: iterator = std :: _ Rb_tree_iterator>, st d :: map <_Key, _Tp, _Compare, _Alloc> :: value_type = std :: pair] ''test.cpp: 56: 12: se crea una instancia desde aquí c: / mingw / bin ../ lib / gcc / mingw32 / 4.5.2 / include / c ++ / bits / stl_function.h: 230: 22: er ror: pasar ''const coord'' como ''este'' argumento de ''const bool coord :: operator <(co nst coord &)'' descarta los calificadores mingw32-make: * [juego] Error 1


Con mucho, lo más simple es definir un operador global "menor que" para su estructura en lugar de una función miembro.

std :: map utiliza, por defecto, el functor ''lessthan'' que, a su vez, usa el "operador <" global definido para el tipo de clave del mapa.

bool operator<(const coord& l, const coord& r) { return (l.x<r.x || (l.x==r.x && l.y<r.y)); }


Otra solución, que puede utilizarse para tipos de datos de terceros, es pasar un Comparison object como tercer parámetro de plantilla. example


Probar y hacer operator < const :

bool operator<(const coord &o) const {

(Su = operator probablemente debería ser == operator y const también)