【问题标题】:C++ standard doesn t provide a hash for this type.C++ 标准不提供这种类型的散列。
【发布时间】:2018-04-26 09:19:12
【问题描述】:

我尝试使用 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++11 unordered-map


    【解决方案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);" */
        }
      };
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-11
      • 1970-01-01
      • 2017-10-13
      • 1970-01-01
      • 1970-01-01
      • 2012-07-17
      • 2022-10-13
      • 2014-06-08
      相关资源
      最近更新 更多