I'm using a priority_queue
, this is the code:
typedef vector<int> state;
typedef bool (*comp)(const state&, const state&);
typedef priority_queue<state, vector<state>, comp> prioq;
prioq prio;
I'm using a custom comparator, that is defined here:
bool comparator (const state& a , const state& b) {
return a[0] < b[0];
}
I can push the first state, but when I try to push the second, I get a segmentation fault: 11.
I don't know if the comparator is right, I don't know really much how to create a custom comparator. I'm trying to order the priority_queue
by the first number of each state.
prioq prio;
constructs a priority queue with a default-constructed comparator (of type comp
in your case). This means that the internally stored pointer to a comparing function is actually initialized with zero. A comparator (if a default constructed one is not enough) can be initialized to a value passed as a constructor's argument:
prioq prio(&comparator);
// ~~~~~~~~~~^