如何在std :: map中将结构用作键? [英] How can I use a struct as key in a std::map?

查看:79
本文介绍了如何在std :: map中将结构用作键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 == 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 key in a map?

我尝试将代码更改为此:

I tried to change 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);
    }
};

但是我仍然遇到以下错误:

But I'm still getting the following error:


C:\Users\tomc\Desktop\g> mingw32-make g ++ test.cpp -std = c ++ 0x $ b中包含文件
$ bc:\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,
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,
来自
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中()(缺点t _Tp& ;, const
_Tp&)const [with _ Tp = coord]':
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&)[with _Key
= coord,_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,类型名_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>,类型名std :: _ Rb_tree< _键,std :: pair,std :: _ Select1st>,_ Compare,
typename _Alloc :: rebind :: value_ty pe> :: other> ::它erator =
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:
错误:将'const coord'传递为'const bool
coord :: operator<(co nst coord&))的'this'参数时,将丢弃限定符mingw32-make:***
[game]错误1

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:\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


推荐答案

尝试使操作符< const

bool operator<(const coord &o)  const {

(您的 =运算符 ==运算符 const

这篇关于如何在std :: map中将结构用作键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆