Struct作为std::map中的键

时间:2021-09-22 20:48:18
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

How can I use a struct as a key to a map?

如何使用struct作为映射的键?

EDIT:

编辑:

Changed the code to this:

将代码改为:

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

still getting this error:

仍然得到这个错误:

C:\Users\tomc\Desktop\g>mingw32-make g++ test.cpp -std=c++0x In file included from c:\mingw\bin../lib/gcc/mingw32/4.5.2/include/c++/string:5 0:0, from c:\mingw\bin../lib/gcc/mingw32/4.5.2/include/c++/bits/loc ale_classes.h:42, from c:\mingw\bin../lib/gcc/mingw32/4.5.2/include/c++/bits/ios _base.h:43, from c:\mingw\bin../lib/gcc/mingw32/4.5.2/include/c++/ios:43, from c:\mingw\bin../lib/gcc/mingw32/4.5.2/include/c++/ostream: 40, from c:\mingw\bin../lib/gcc/mingw32/4.5.2/include/c++/iostream :40, from test.cpp:1: c:\mingw\bin../lib/gcc/mingw32/4.5.2/include/c++/bits/stl_function.h: In member function '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: inst antiated from 'std::pair, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(const _Val&) [with _Key = coord, _Val = std::pair, _KeyOfValue = std::_Select1st >, _Compare = std::less, _Alloc = std::allocator

C:\Users\tomc\Desktop\ g > mingw32-make g++测试。cpp -std=c:\mingw\bin../lib/gcc/mingw32/4.5.2/include/c++/string:5 0:0, from c:\mingw\bin.. ./ gcc/mingw32/4.5.2/include/c++/bits/loc ale_classes。h:42岁,从c:\ mingw \ bin . . / lib / gcc / mingw32/4.5.2 /包括/ 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:cpp:1:c:\ mingw \ bin . . / lib / gcc / mingw32/4.5.2 / / stl_function包括/ c++ /位。h:在成员函数'bool std::less<_Tp>::operator()(const _tp&, const _tp&) const [with _Tp = coord] c:\mingw\bin../lib/ mingw32/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 _valand) [with _Key = coord, _Val = std:: _select1 >, _Compare = std::less, _Alloc = std::allocator。

]' c:\mingw\bin../lib/gcc/mingw32/4.5.2/include/c++/bits/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&) [with _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: instantiated from here c:\mingw\bin../lib/gcc/mingw32/4.5.2/include/c++/bits/stl_function.h:230:22: er ror: passing 'const coord' as 'this' argument of 'const bool coord::operator<(co nst coord&)' discards qualifiers mingw32-make: * [game] Error 1

]“c:\ mingw \ bin . . / lib / gcc / mingw32/4.5.2 / / stl_map包括/ c++ /位。h:501:41:英斯达ntiated从“std::对,std::_Select1st >,_Compare,typename _Alloc::重新绑定::value_type >::>::iterator,bool > std::map < _Key、_Tp _Compare,_Alloc >::插入(const std::map < _Key,_Tp、_Compare _ Alloc >::value_type&)[_Key = coord _Tp = int,_Compare = std::少,_Alloc = std::分配器>,typename std::_Rb_tree < _关键,std::对,std::_Select1st >,_比较,typename _Alloc::重新绑定::value_ty pe >::>::iterator = std::_Rb_tree_iterator >,圣d::map < _Key、_Tp _Compare,_Alloc >::value_type = std::一对]“测试。c:\mingw\bin. /lib/gcc/mingw32/4.5.2/include/c++/bits/stl_function。h:230:22: er ror:通过‘const coord’作为‘const bool coord::operator<(co nst coord&)' discards限定符mingw32-make: * [game] Error 1。

3 个解决方案

#1


35  

Try and make operator < const:

试制操作符< const:

bool operator<(const coord &o)  const {

(Your = operator should probably be == operator and const as well)

(您的=操作符可能也是==操作符和const)

#2


5  

By far the simplest is to define a global "less than" operator for your struct in stead of as a member function.

到目前为止,最简单的方法是为结构体定义一个全局“小于”运算符,而不是作为成员函数。

std::map uses - by default - the 'lessthan' functor which, in turn, uses the global "operator<" defined for the key type of the map.

默认情况下,map使用的是“lessthan”函数,而后者又使用为map的键类型定义的全局“操作符<”。

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

#3


1  

Another solution, which may be used for third-party data types, is to pass a Comparison object as third template parameter. example

另一个可以用于第三方数据类型的解决方案是将比较对象作为第三个模板参数传递。例子

#1


35  

Try and make operator < const:

试制操作符< const:

bool operator<(const coord &o)  const {

(Your = operator should probably be == operator and const as well)

(您的=操作符可能也是==操作符和const)

#2


5  

By far the simplest is to define a global "less than" operator for your struct in stead of as a member function.

到目前为止,最简单的方法是为结构体定义一个全局“小于”运算符,而不是作为成员函数。

std::map uses - by default - the 'lessthan' functor which, in turn, uses the global "operator<" defined for the key type of the map.

默认情况下,map使用的是“lessthan”函数,而后者又使用为map的键类型定义的全局“操作符<”。

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

#3


1  

Another solution, which may be used for third-party data types, is to pass a Comparison object as third template parameter. example

另一个可以用于第三方数据类型的解决方案是将比较对象作为第三个模板参数传递。例子