如何在std :: map中使用struct作为键?

25 c++ stdmap c++11

我有以下代码,但我在最后一行收到错误:

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
Run Code Online (Sandbox Code Playgroud)

如何在地图中使用结构作为键?


我试图将代码更改为:

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);
    }
};
Run Code Online (Sandbox Code Playgroud)

但我仍然收到以下错误:

C:\ Users\tomc\Desktop\g> mingw32-make g ++ test.cpp -std = c ++ 0x包含在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++/bits/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 [with _ Tp = coord]':c:\ mingw\bin ../ lib/gcc/mingw32/4.5.2/include/c ++/bits/stl_tree.h:1184:4:inst从'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 :: alloc ator>]'c:\ mingw\bin ../ lib/gcc/mingw32/4.5.2/include/c ++/bits/stl_map.h:501:41:从'std :: pair,std :: _ Select1st安装>,_比较,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:从这里实例化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&)'的'this'参数'丢弃限定词mingw32-make:***[游戏]错误1

And*_*nck 44

尝试并制作operator < const:

bool operator<(const coord &o)  const {
Run Code Online (Sandbox Code Playgroud)

(你= operator也许应该是== operatorconst也)

  • 实际上,[我将使运算符成为一个自由函数](http://stackoverflow.com/questions/4421706/operator-overloading)而不是成员函数. (10认同)
  • 为什么添加`const`可以解决错误,你能解释一下吗?, 谢谢 (3认同)
  • @ x4:映射的键是"const Key"类型,因此您只能对结构上标记为"const"的操作执行操作.在这种情况下尝试使用未标记为"const"的方法会导致上述错误(使用XXX丢弃限定符) (2认同)

haa*_*vee 9

到目前为止,最简单的是为结构定义一个全局"小于"运算符而不是作为成员函数.

std :: map使用 - 默认情况下 - 'lessthan'仿函数,它反过来使用为映射的键类型定义的全局"operator <".

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


hon*_*onk 7

正如提到的答案安德里,你可以提供一个自定义的比较对象的map,而不是确定的operator<为你的结构。从C++11 开始,您还可以使用lambda 表达式而不是定义比较对象。此外,您不需要operator==为您的结构定义来完成map工作。因此,您可以保持结构如此简短:

struct coord {
    int x, y;
};
Run Code Online (Sandbox Code Playgroud)

其余的代码可以写成如下:

auto comp = [](const coord& c1, const coord& c2){
    return c1.x < c2.x || (c1.x == c2.x && c1.y < c2.y);
};
std::map<coord, int, decltype(comp)> m(comp);
Run Code Online (Sandbox Code Playgroud)

Ideone 上的代码

  • 当您无法访问结构体代码本身并需要将其添加到映射时,这非常有用。谢谢! (3认同)