使用用户定义的结构作为映射C++的键

Use user defined struct as the key for map C++

本文关键字:映射 C++ 的键 结构 用户 定义      更新时间:2023-10-16

我正在尝试将我自己的类映射到一个整数:

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;

但我对比较者并不完全合适;