1

My question is how to use a custom class as a key. I have found a few links for what I need to do such as create a custom hash function on my class, as well as change both () and == (I believe). The problem is that I cannot comment on those threads and I am lost as to how to implement these 3 things in a .h file. C++ unordered_map using a custom class type as the key this website just about sums up the problem but I cannot ask how or where to put my information to customize it to my code. Any help is appreciated, thank you.

Edit: my class code:

class apples{
public:
    apples(){
        removed = false;
        hasPortal = false;
        distance = 9999;
        from.first = -1;
        from.second = -1;
        location.first=-1;
        location.second=-1;
        portal.first = -1;
        portal.second = -1;
    }
    bool removed,hasPortal;
    int distance;
    pair<int,int> from;
    pair<int, int> location;
    pair<int,int> portal;
    int portalDistance;
};

then I set up the minPriorityQueue for Dijkstra's

//Dijkstra's Algorithm
//1. Put all nodes (apples) into the queue with high distance all except the begining apple
//2. pop out and update all distances from distance + distanace to neighboring apple
//3. continuously pop out and update the neighboring apples of the popped apple
MinPriorityQueue<apples> tree;
for(int i=0; i<m.size(); i++){
    for(int j=0; j<m[i].size(); j++){
        if(!fruits[i][j].removed){
            tree.push(fruits[i][j], fruits[i][j].distance);
        }
    }
}

my minPriorityQueue:

#include <unordered_map>
#include <vector>
#include <utility> // Has pair and swap

using namespace std;

template <typename T>
class MinPriorityQueue
{
public:
//Here there is push, pop, decrease_key, and front
//They all work as was tested somewhere else
private:
    unordered_map<T, int> I; // Maps elements to indices in H.
    vector< pair<T, int> > H;     // The array containing the heap.
};

So the problem comes when trying to use the custom class as a key. I only vaguely understand the answer for the other question in the link. I guess I am asking for clarification and just understanding where the stuff is supposed to go and how to go about writing the code.

Liam
  • 33
  • 2

0 Answers0