从错误信息来看,代码中使用了非静态数据成员 pos,这是无效的。
在 C++ 中,如果要在 unordered_map 的值类型中存储自定义结构或类对象,需要确保该类型具有可哈希化和相等比较的能力。对于自定义类型,可以通过提供一个自定义哈希函数和相等比较函数来实现这一点。
首先,请确保你的 pos 类已经正确实现了哈希函数和相等比较操作符。然后,根据你的需求,在 unordered_map 的模板参数中指定正确的键类型和值类型。
下面是一个示例:
#include <iostream>
#include <unordered_map>
struct pos {
int x;
int y;
// 自定义哈希函数
struct HashFunc {
std::size_t operator()(const pos& p) const {
return std::hash<int>()(p.x) ^ std::hash<int>()(p.y);
}
};
// 自定义相等比较操作符
bool operator==(const pos& other) const {
return (x == other.x && y == other.y);
}
};
int main() {
std::unordered_map<ListNode*, pos, pos::HashFunc> res;
// 在此处进行 unordered_map 操作...
return 0;
}
请根据你的实际需求对代码进行修改,并确保 pos 类型具有正确的哈希函数和相等比较操作符。
内容由零声教学AI助手提供,问题来源于学员提问




