我尝试使用unordered_map为网格实现*算法,我有自己的优先级队列(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;
}
}
}
}
答案 0 :(得分:1)
我假设您的意思是square_nod
类型,您没有列出其定义。你需要为你的班级添加一个。
借用this question which might be a duplicate
namespace std {
template <> struct hash<Foo>
{
size_t operator()(const Foo & x) const
{
/* your code here, e.g. "return hash<int>()(x.value);" */
}
};
}