How can I use a struct as key in a std::map?

后端 未结 4 563
攒了一身酷
攒了一身酷 2020-12-09 16:00

I have the following code, but I get an error on on the last line:

struct coord { 
    int x, y; 

    bool operator=(const coord &o) {
        return x          


        
4条回答
  •  执笔经年
    2020-12-09 16:09

    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.

    bool operator<(const coord& l, const coord& r) {
         return (l.x

提交回复
热议问题