Как я могу использовать структуру в качестве ключа в 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 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 = c ++ 0x In file включены из c: \ mingw \ bin ../ lib / gcc / mingw32 / 4.5.2 / include / c ++ / string: 5 0: 0, из c: \ mingw \ bin ../ lib / gcc / mingw32 / 4.5.2 / include / c ++ / bits / loc ale_classes.h: 42, из c: \ mingw \ bin ../ lib / gcc / mingw32 / 4.5.2 / include / c ++ / биты / ios _base.h: 43, из c: \ mingw \ bin ../ lib / gcc / mingw32 / 4.5.2 / include / c ++ / ios: 43, из c: \ mingw \ bin ../ lib / gcc / mingw32 / 4.5.2 / include / c ++ / ostream: 40, из c: \ mingw \ bin ../ lib / gcc / mingw32 / 4.5.2 / include / c ++ / iostream: 40, из test.cpp: 1: c: \ mingw \ bin ../ lib / gcc / mingw32 / 4.5.2 / include / c ++ / bits / stl_function.h: В функции-члене bool std :: less <_Tp> :: operator () (const _Tp &, const _Tp &) const [с _ Tp = координатами] ': c: \ mingw \ bin ../ lib / gcc / mingw32 / 4.5.2 / include / c ++ / bits / stl_tree.h: 1184: 4: инсталлирован из 'std :: pair, bool> std :: _ Rb_tree <_Key, _Val, _KeyOfValue, _Compare, _Alloc> :: _ M_insert_unique (const _Val &) [с _Key = координата, _Val = std :: pair, _KeyOfValue = std :: _ Select1st>, _Compare = std :: less, _Alloc = std :: allocator>] ' c: \ mingw \ bin ../ lib / gcc / mingw32 / 4.5.2 / include / c ++ / bits / stl_map.h: 501: 41: создается из '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 \ bin ../ lib / gcc / mingw32 / 4.5.2 / include / c ++ / bits / stl_function.h: 230: 22: ошибка: передача 'constordin' в качестве 'этого' аргумента 'const bool Coord :: operator <(co nst corre &) 'отбрасывает квалификаторы mingw32-make: *** [игра] Ошибка 1

25
задан honk 28 February 2019 в 15:18
поделиться