構造体の一意のリストを維持するために unordered_set を使用しようとしています。構造体 Name のハッシュ関数を定義しましたが、別の構造体メンバー Address を含むように Name 構造体を拡張すると、コンパイル エラーが発生します。Address 構造体をハッシュする方法を指定する必要があることはわかっていますが、どこでどのようにハッシュするかがわかりません。
#include <unordered_set>
#include <string>
using namespace std;
struct Address
{
int num;
};
struct Name
{
string first;
string second;
Address address;
};
struct hashing_fn {
size_t operator()(const Address &a ) const
{
return hash<int>()(a.num);
}
size_t operator()(const Name &name ) const
{
return hash<string>()(name.first) ^ hash<string>()(name.second) ^ hash<Address>()(name.address);
}
};
int main(int argc, char* argv[])
{
unordered_set<Name,hashing_fn> ids;
return 0;
}
アップデート
完了するために、これは修正でした:
template<>
struct hash<typename Address> {
size_t operator()(const Address &a ) const
{
return hash<int>()(a.num);
}
};