C++ unordered_map< pair<uint, uint>, UserClass > 插入
C++ unordered_map< pair<uint, uint>, UserClass > insertion
我正在尝试使用 unordered_map
将键作为一对 uint
和 class Tile
.
的值
#include <iostream>
#include <unordered_map>
#include <boost/functional/hash.hpp>
class Tile {
public:
std::pair<uint, uint> coordinate;
bool operator==(const Tile &t) {
return this->coordinate.first == t.coordinate.first
&& this->coordinate.second == t.coordinate.second;
}
Tile (uint x, uint y) {
this->coordinate.first = x;
this->coordinate.second = y;
}
std::pair<uint, uint> GetCoor() const
{return this->coordinate;}
uint GetX() const
{return coordinate.first;}
uint GetY() const
{return coordinate.second;}
};
struct TileHash {
std::size_t operator()(const Tile &t) const
{
size_t seed = 0;
boost::hash_combine (seed, t.GetX());
boost::hash_combine (seed, t.GetY());
return seed;
}
};
int main ()
{
std::unordered_map<std::pair<uint, uint>, Tile, TileHash> board;
Tile t1 = Tile (0, 0);
board.insert (std::make_pair (t1.GetCoor(), t1));
//board[t1.GetCoor()] = t1; // This causes a different error at compile time...
}
我收到以下错误,然后是一些我没有粘贴到这里的注释
/usr/include/c++/v1/unordered_map:400:17: error: no matching function for call to object of type 'const TileHash'
{return static_cast<const _Hash&>(*this)(__x.__cc.first);}
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/v1/__hash_table:1712:21: note: in instantiation of member function 'std::__1::__unordered_map_hasher<std::__1::pair<unsigned int, unsigned int>, std::__1::__hash_value_type<std::__1::pair<unsigned int, unsigned int>,
Tile>, TileHash, true>::operator()' requested here
size_t __hash = hash_function()(__x);
^
/usr/include/c++/v1/__hash_table:1692:12: note: in instantiation of member function 'std::__1::__hash_table<std::__1::__hash_value_type<std::__1::pair<unsigned int, unsigned int>, Tile>,
std::__1::__unordered_map_hasher<std::__1::pair<unsigned int, unsigned int>, std::__1::__hash_value_type<std::__1::pair<unsigned int, unsigned int>, Tile>, TileHash, true>, std::__1::__unordered_map_equal<std::__1::pair<unsigned
int, unsigned int>, std::__1::__hash_value_type<std::__1::pair<unsigned int, unsigned int>, Tile>, std::__1::equal_to<std::__1::pair<unsigned int, unsigned int> >, true>,
std::__1::allocator<std::__1::__hash_value_type<std::__1::pair<unsigned int, unsigned int>, Tile> > >::__insert_unique_value' requested here
return __insert_unique_value(__x);
我知道我对 unordered_map
的实例化没问题,当我编译时没有调用 insert all 就很好。
通过 TileHash
,您提供了一个采用 const Tile&
.
的哈希函数
该实现正在寻找采用 const std::pair<uint, uint>&
.
的哈希函数
那是因为它是被散列的键,而不是映射值。
所以,你在散列错误的东西。
标准没有为我们提供对的哈希器,但 Boost 有:
std::unordered_map<
std::pair<uint, uint>,
Tile,
boost::hash<std::pair<uint, uint>>
> board;
我正在尝试使用 unordered_map
将键作为一对 uint
和 class Tile
.
#include <iostream>
#include <unordered_map>
#include <boost/functional/hash.hpp>
class Tile {
public:
std::pair<uint, uint> coordinate;
bool operator==(const Tile &t) {
return this->coordinate.first == t.coordinate.first
&& this->coordinate.second == t.coordinate.second;
}
Tile (uint x, uint y) {
this->coordinate.first = x;
this->coordinate.second = y;
}
std::pair<uint, uint> GetCoor() const
{return this->coordinate;}
uint GetX() const
{return coordinate.first;}
uint GetY() const
{return coordinate.second;}
};
struct TileHash {
std::size_t operator()(const Tile &t) const
{
size_t seed = 0;
boost::hash_combine (seed, t.GetX());
boost::hash_combine (seed, t.GetY());
return seed;
}
};
int main ()
{
std::unordered_map<std::pair<uint, uint>, Tile, TileHash> board;
Tile t1 = Tile (0, 0);
board.insert (std::make_pair (t1.GetCoor(), t1));
//board[t1.GetCoor()] = t1; // This causes a different error at compile time...
}
我收到以下错误,然后是一些我没有粘贴到这里的注释
/usr/include/c++/v1/unordered_map:400:17: error: no matching function for call to object of type 'const TileHash'
{return static_cast<const _Hash&>(*this)(__x.__cc.first);}
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/v1/__hash_table:1712:21: note: in instantiation of member function 'std::__1::__unordered_map_hasher<std::__1::pair<unsigned int, unsigned int>, std::__1::__hash_value_type<std::__1::pair<unsigned int, unsigned int>,
Tile>, TileHash, true>::operator()' requested here
size_t __hash = hash_function()(__x);
^
/usr/include/c++/v1/__hash_table:1692:12: note: in instantiation of member function 'std::__1::__hash_table<std::__1::__hash_value_type<std::__1::pair<unsigned int, unsigned int>, Tile>,
std::__1::__unordered_map_hasher<std::__1::pair<unsigned int, unsigned int>, std::__1::__hash_value_type<std::__1::pair<unsigned int, unsigned int>, Tile>, TileHash, true>, std::__1::__unordered_map_equal<std::__1::pair<unsigned
int, unsigned int>, std::__1::__hash_value_type<std::__1::pair<unsigned int, unsigned int>, Tile>, std::__1::equal_to<std::__1::pair<unsigned int, unsigned int> >, true>,
std::__1::allocator<std::__1::__hash_value_type<std::__1::pair<unsigned int, unsigned int>, Tile> > >::__insert_unique_value' requested here
return __insert_unique_value(__x);
我知道我对 unordered_map
的实例化没问题,当我编译时没有调用 insert all 就很好。
通过 TileHash
,您提供了一个采用 const Tile&
.
该实现正在寻找采用 const std::pair<uint, uint>&
.
那是因为它是被散列的键,而不是映射值。
所以,你在散列错误的东西。
标准没有为我们提供对的哈希器,但 Boost 有:
std::unordered_map<
std::pair<uint, uint>,
Tile,
boost::hash<std::pair<uint, uint>>
> board;