首页 > 解决方案 > 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;
                }
            }
        }
    }

标签: c++11unordered-map

解决方案


我假设你的意思是 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);" */
    }
  };
}

推荐阅读