解决由于在哈希函数中使用了不完整的类型而导致的编译失败



由于在编译unordered_set的哈希函数时使用了不完整的类型Dir,以下代码未能编译。有办法绕过这个吗。我猜添加另一个类可能会解决这个问题。但是,解决这样的案件的最佳做法是什么呢。

auto pred = [](const auto& v) {
return std::hash<std::string>()(v.name);
};
struct Dir{
bool operator==(Dir const& other) {
return name == other.name;
}
std::string name;
std::unordered_set<Dir, decltype(pred)> down;
};

这解决了问题,唯一性、添加/删除Dir都得到了解决。但我们需要严格跟踪列表和无序映射,以确保它正常工作。不确定这是否是最好的方式。

struct Dir{
std::string name;
std::list<Dir> down;
std::unordered_map<std::string, std::list<Dir>::iterator> downMap;
};

最新更新