c++11 - C++ 标准不提供这种类型的散列。
问题描述
我尝试使用 unordered_map 为网格实现 a* 算法,我有自己的优先级队列(whitch)工作正常。问题是当我运行程序时出现这个错误:C++ 标准不提供这种类型的哈希。
A* 可以用其他结构实现吗?或者我该如何解决这个问题?
int main()
{
std::ifstream file("Labirint.txt");
char **labirint;
int nr_linii, nr_coloane;
file >> nr_linii >> nr_coloane;
locatie soricel;
locatie branza;
file >> soricel.x >> soricel.y;
file >> branza.x >> branza.y;
int deplasare_linie[] = { 0,0,-1,1 };
int deplasare_coloana[] = { -1,1,0,0 };
labirint = new char*[nr_linii];
for (int i = 0; i < nr_linii; ++i)
labirint[i] = new char[nr_coloane];
for (int i = 0; i < nr_linii; i++)
for (int j = 0; j < nr_coloane; ++j)
file >> labirint[i][j];
square_nod start,goal;
start.pozitie = soricel;
start.prioritate = 0;
goal.pozitie = branza;
PriorityQueue frontier;
frontier.Insert(start);
std::unordered_map<square_nod, int> came_from;
std::unordered_map<square_nod, int> cost_so_far;
cost_so_far.insert(std::make_pair(start, 0));
while (!frontier.isEmpty())
{
square_nod current = frontier.minElement();
frontier.extractMin();
if (current == goal)
{
break;
}
for (int i = 0; i < 4; i++)
{
square_nod next;
next.pozitie.x = current.pozitie.x + deplasare_linie[i];
next.pozitie.y = current.pozitie.y + deplasare_coloana[i];
int new_cost = cost_so_far[current] + 1;
auto gasit = cost_so_far.find(next);
if (gasit == cost_so_far.end() || new_cost < cost_so_far[next])
{
cost_so_far[next] = new_cost;
int priority = new_cost + city_block_distance(goal, next);
next.prioritate = priority;
frontier.Insert(next);
came_from[next] = current.prioritate;
}
}
}
}
解决方案
我假设你的意思是 type square_nod
,你没有列出它的定义。您需要为您的班级添加一个。借用这个可能是重复的问题
namespace std {
template <> struct hash<Foo>
{
size_t operator()(const Foo & x) const
{
/* your code here, e.g. "return hash<int>()(x.value);" */
}
};
}
推荐阅读
- java - JBPM - WorkItemNode 不能转换为 org.jbpm.workflow.core.node.HumanTaskNode
- python - 根据特定的日期时间和条件逐列删除行
- python - print中间的逗号(,)是干什么用的?
- sql-server - SQL 搜索多个日期范围内出现的交易类型
- javascript - 在 react-redux useSelector() 中获取 axios 数据后,状态更改不是重新渲染组件
- javascript - 如何使用按钮为输入添加值?
- javascript - 即使为空白,HTML 输入属性“模式”也会强制执行模式
- php - 从 PDO 查询创建 JSON - PHP
- java - 我可以更改java中静态变量的值吗?你能告诉我为什么它显示这个输出吗?
- sql - 星期一从付款_日期列发生了多少付款?下面的代码给我零计数