Sorting a deque of struct pointers

I'm attempting to sort a deque list containing a set of structures.

The structure is declared as follows:

struct coords
{
int x;
int y;
int score;
coords* parent;
}

The deque list is declared as follows:

deque <coords*> openList;


The list is filled with a set of coords* and i need to sort the list by the value of score. Im completely at a loss. This is for the Best First Search.

Much appreciated.
Not very sure what's use there for the element "parent".

Try this way.

1, define a comparator first

bool decompare(coords* o1, coords* o2)
{
return (o1->score > o2->score);
}

2, and then use "sort"

sort(openList.begin(), openList.end(), decompare);

Hopefully it helps.

b2ee
Last edited on
Just to add on, for coords* o1 and coords* o2 the passed in value can be NULL so it is actually safer to check explicitly for NULL. Reference variables don't suffer from this problem though as they always have to point to something.
Thanks for the replies. The reason for the parent is for backtracking during the algorithms implementation. and to produce the path at the end.
Thanks sohguanh for your adding.

b2ee
Topic archived. No new replies allowed.