Подтвердить что ты не робот

Структурировать как ключ в std:: map

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

Как я могу использовать struct как ключ к карте?

EDIT:

Изменен код:

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);
    }
};

все еще получает эту ошибку:

C:\Users\tomc\Desktop\g > mingw32-make g++ test.cpp -std = С++ 0x В файле включены из c:\mingw\bin../lib/gcc/mingw32/4.5.2/include/С++/string: 5 0: 0,                  из C:\MinGW\бен../Library/GCC/mingw32/4.5.2/включать/С++/бит/LOC ale_classes.h: 42,                  из C:\MinGW\бен../Library/GCC/mingw32/4.5.2/включать/С++/бит/ИОС _base.h: 43,                  из C:\MinGW\бен../Library/GCC/mingw32/4.5.2/включить/С++/ИОС: 43,                  из c:\mingw\bin../lib/gcc/mingw32/4.5.2/include/С++/ostream: 40,                  из c:\mingw\bin../lib/gcc/mingw32/4.5.2/include/С++/iostream: 40,                  от test.cpp: 1: C:\MinGW\бен../Library/GCC/mingw32/4.5.2/включать/С++/бит/stl_function.h: В функции-члена 'bool std:: less < _Tp > :: operator() (const _Tp &, const _Tp &) const [с _ Tp = coord] ': C:\MinGW\бен../Library/GCC/mingw32/4.5.2/включать/С++/бит/stl_tree.h: 1184: 4: inst antiated from 'std:: pair, bool > std:: _ Rb_tree < _Key, _Val, _KeyOfValue, _Compare, _Alloc > :: _ M_insert_unique (const _Val &) [с помощью _Key = coord, _Val = std:: pair, _KeyOfValue = std:: _ Select1st > , _Compare = std:: less, _Alloc = std:: allocator

]   C:\MinGW\бен../Library/GCC/mingw32/4.5.2/включать/С++/бит/stl_map.h: 501: 41:   insta ntiated from 'std:: pair, std:: _ Select1st > , _Compare, typename _Alloc:: rebind:: value_type > :: other > :: iterator, bool > std   :: map < _Key, _Tp, _Compare, _Alloc > :: insert (const std:: map < _Key, _Tp,   _Compare, _ Alloc > :: value_type &) [с _Key = coord, _Tp = int,   _Compare = std:: less, _Alloc = std:: allocator > , typename std:: _ Rb_tree < _ Key, std:: pair, std:: _ Select1st > , _ Compare,   typename _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: созданный здесь   C:\MinGW\бен../Library/GCC/mingw32/4.5.2/включать/С++/бит/stl_function.h: 230: 22:   er ror: передача 'const coord' в качестве аргумента 'this' аргумента 'const bool   Координатор:: operator < (co nst coord &) 'отбрасывает квалификаторы mingw32-make: *   [game] Ошибка 1

4b9b3361

Ответ 1

Попробуйте сделать operator < const:

bool operator<(const coord &o)  const {

(= operator должен быть == operator и const)

Ответ 2

Самым простым является определение глобального оператора "меньше" для вашей структуры вместо функции-члена.

std:: map использует - по умолчанию - функтор 'lessthan', который, в свою очередь, использует глобальный оператор < определенный для ключевого типа карты.

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

Ответ 3

Другим решением, которое может использоваться для сторонних типов данных, является передача Comparison object в качестве третьего параметра шаблона. пример