c++11,为什么这个为 vector <int> 定义的 unordered_map 不起作用?

标签 c++ c++11

我正在尝试创建一个 unordered_map<vector<int>, int> .我知道我需要有自己的哈希函数,所以我尝试构建一个。我能够很好地创建和编译它,但是当我尝试访问它时出现编译错误。

代码如下:

#include <bits/stdc++.h>
using namespace std;

class mhash {
public:
  std::size_t operator()(std::vector<int> const& vec) const {
    std::size_t seed = vec.size();
    for(auto& i : vec) {
     seed ^= i + 0x9e3779b9 + (seed << 6) + (seed >> 2);
     }
  return seed;
    }
};

map <vector <int>,int, mhash> M;

int main(){
    vector <int> V;
    M[V]=5;
    cout << M[V];
}

我正在使用 g++ -std=c++11 进行编译.

如果我只声明 unordered_map它编译得很好,但是当我尝试使用它时,出现以下错误:

In file included from /usr/include/c++/5/map:61:0,
                 from /usr/include/x86_64-linux-gnu/c++/5/bits/stdc++.h:80,
                 from exp.cpp:1:
/usr/include/c++/5/bits/stl_map.h: In instantiation of ‘std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const key_type&) [with _Key = std::vector<int>; _Tp = int; _Compare = mhash; _Alloc = std::allocator<std::pair<const std::vector<int>, int> >; std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = int; std::map<_Key, _Tp, _Compare, _Alloc>::key_type = std::vector<int>]’:
exp.cpp:19:5:   required from here
/usr/include/c++/5/bits/stl_map.h:481:32: error: no match for call to ‘(std::map<std::vector<int>, int, mhash>::key_compare {aka mhash}) (const key_type&, const std::vector<int>&)’
  if (__i == end() || key_comp()(__k, (*__i).first))
                                ^
exp.cpp:6:15: note: candidate: std::size_t mhash::operator()(const std::vector<int>&) const
   std::size_t operator()(std::vector<int> const& vec) const {
               ^
exp.cpp:6:15: note:   candidate expects 1 argument, 2 provided
In file included from /usr/include/c++/5/map:60:0,
                 from /usr/include/x86_64-linux-gnu/c++/5/bits/stdc++.h:80,
                 from exp.cpp:1:
/usr/include/c++/5/bits/stl_tree.h: In instantiation of ‘std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_lower_bound(std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Link_type, std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Link_type, const _Key&) [with _Key = std::vector<int>; _Val = std::pair<const std::vector<int>, int>; _KeyOfValue = std::_Select1st<std::pair<const std::vector<int>, int> >; _Compare = mhash; _Alloc = std::allocator<std::pair<const std::vector<int>, int> >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator = std::_Rb_tree_iterator<std::pair<const std::vector<int>, int> >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Link_type = std::_Rb_tree_node<std::pair<const std::vector<int>, int> >*]’:
/usr/include/c++/5/bits/stl_tree.h:1091:30:   required from ‘std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::lower_bound(const key_type&) [with _Key = std::vector<int>; _Val = std::pair<const std::vector<int>, int>; _KeyOfValue = std::_Select1st<std::pair<const std::vector<int>, int> >; _Compare = mhash; _Alloc = std::allocator<std::pair<const std::vector<int>, int> >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator = std::_Rb_tree_iterator<std::pair<const std::vector<int>, int> >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::key_type = std::vector<int>]’
/usr/include/c++/5/bits/stl_map.h:916:36:   required from ‘std::map<_Key, _Tp, _Compare, _Alloc>::iterator std::map<_Key, _Tp, _Compare, _Alloc>::lower_bound(const key_type&) [with _Key = std::vector<int>; _Tp = int; _Compare = mhash; _Alloc = std::allocator<std::pair<const std::vector<int>, int> >; std::map<_Key, _Tp, _Compare, _Alloc>::iterator = std::_Rb_tree_iterator<std::pair<const std::vector<int>, int> >; std::map<_Key, _Tp, _Compare, _Alloc>::key_type = std::vector<int>]’
/usr/include/c++/5/bits/stl_map.h:479:28:   required from ‘std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const key_type&) [with _Key = std::vector<int>; _Tp = int; _Compare = mhash; _Alloc = std::allocator<std::pair<const std::vector<int>, int> >; std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = int; std::map<_Key, _Tp, _Compare, _Alloc>::key_type = std::vector<int>]’
exp.cpp:19:5:   required from here
/usr/include/c++/5/bits/stl_tree.h:1628:6: error: no match for call to ‘(mhash) (const std::vector<int>&, const std::vector<int>&)’
  if (!_M_impl._M_key_compare(_S_key(__x), __k))
      ^
exp.cpp:6:15: note: candidate: std::size_t mhash::operator()(const std::vector<int>&) const
   std::size_t operator()(std::vector<int> const& vec) const {
               ^
exp.cpp:6:15: note:   candidate expects 1 argument, 2 provided

最佳答案

你写的是 map,不是 unordered_map。

但是...如果您想要一张 map : ma​​p 的第三个模板参数不是散列,而是比较器。 bool 运算符()(a, b)

关于c++11,为什么这个为 vector <int> 定义的 unordered_map 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43190858/

相关文章:

c++ - E_FAIL D3D11CreateDevice

c++ - 如何用元组模拟 std::array<15,int &>

c++ - 避免 C++ 中重复的子类定义

c++ - 模板化静态成员函数是如何解析的?

c++ - C++0x中的统一初始化,什么时候用()代替{}?

c++ - 判断是否过了 5 秒

c++ - 为什么 std::tie 没有标记为 C++14 的 constexpr?

异常情况下的 C++ 错误 C2228( '.val' 左侧必须有类/结构/union )

C++ 捕获对 bool 错误值的引用

c++ - 哪些 Boost 功能与 C++11 重叠?