使用用户定义的结构作为映射C++的键
Use user defined struct as the key for map C++
我正在尝试将我自己的类映射到一个整数:
struct point{
int x;
int y;
bool operator==(const point& b) {
return (x==b.x && y==b.y);
};
bool operator<(const point& b) {
return (x < b.x && y < b.y);
};
};
int main() {
point a, b;
a.x = 0;
a.y = 1;
b.x = 0;
b.y = 1;
map<point, int> myMap;
myMap[a] = 1;
cout << (a==b) << endl;
cout << myMap.count(b) << endl;
}
此代码无法编译,我无法理解错误消息。似乎如果我改成map<point*, int>
它会编译,但这不是我想要的。我的目的是当我调用mayMap.count(b)
时,它将返回 1。我应该进行哪些更改?谢谢!错误信息:
-*- mode: compilation; default-directory: "~/Dropbox/Documents/UVa/Others/UVa Online Judge/" -*-
Compilation started at Tue Nov 12 14:04:15
clang++ testset.cpp
In file included from testset.cpp:1:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/iostream:38:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/ios:216:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/__locale:15:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/string:434:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/algorithm:594:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/memory:597:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/__functional_base:56:21: error:
invalid operands to binary expression ('const point' and 'const point')
{return __x < __y;}
~~~ ^ ~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/map:1032:17: note:
in instantiation of member function 'std::__1::less<point>::operator()'
requested here
if (__tree_.value_comp().key_comp()(__k, __nd->__value_.first))
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/map:1272:36: note:
in instantiation of member function 'std::__1::map<point, int,
std::__1::less<point>, std::__1::allocator<std::__1::pair<const point,
int> > >::__find_equal_key' requested here
__node_base_pointer& __child = __find_equal_key(__parent, __k);
^
testset.cpp:41:12: note: in instantiation of member function
'std::__1::map<point, int, std::__1::less<point>,
std::__1::allocator<std::__1::pair<const point, int> > >::operator[]'
requested here
secondMap[a] = 1;
^
testset.cpp:13:8: note: candidate function not viable: 'this' argument has type
'const point', but method is not marked const
bool operator<(const point& b) {
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/utility:375:1: note:
candidate template ignored: failed template argument deduction
operator< (const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/iterator:565:1: note:
candidate template ignored: failed template argument deduction
operator<(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_I...
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/iterator:960:1: note:
candidate template ignored: failed template argument deduction
operator<(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/iterator:1269:1: note:
candidate template ignored: failed template argument deduction
operator<(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y...
^
1 error generated.
Compilation exited abnormally with code 1 at Tue Nov 12 14:04:16
更新后的代码仍然不起作用的原因是operator <
没有const
。将其更改为:
bool operator<(const point& b) const
{
return (x < b.x && y < b.y);
};
您的问题在于:map 是存储有序数据的 stl 容器。它需要定义的静态布尔运算符,具有 2 个常量点参数,如下所示:
static bool operator<(const point b, const point a) {
return (a.x<b.x && a.y<b.y);
};
或定义函数比较器,并将其作为附加[3d]参数添加到映射
template < class Key, // map::key_type
class T, // map::mapped_type
class Compare = less<Key>, // map::key_compare
class Alloc = allocator<pair<const Key,T> > // map::allocator_type
> class map;
但我对比较者并不完全合适;
相关文章:
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 将函数类成员映射到类本身内部
- 如何在 C# 中映射双 C 结构指针?
- 如何在C++中使用结构生成映射
- 使用std::函数映射对象方法
- 如何加载(或映射)文件部分的最大大小,但适合在Windows上的RAM
- C++映射分割错误(核心转储)
- 内联映射初始化的动态atexit析构函数崩溃
- 使用"std::unordereded_map"映射到"std::list"对象
- 如何从多映射中删除特定的重复项
- 在未初始化映射的情况下,将值插入到映射的映射中
- QT通过C++添加映射QML项目
- 在c++中访问int到类对象的映射时出错
- 在C++中搜索嵌套多映射值
- 错误处理.将系统错误代码映射到泛型
- C++匿名结构作为std::映射值
- 如何从存储在std::映射中的std::集中删除元素
- 递归无序映射
- 正在VS调试器中监视映射条目