Sorting a deque of struct pointers

Dec 6, 2010 at 1:59am
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.
Dec 6, 2010 at 4:47am
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 Dec 6, 2010 at 4:50am
Dec 6, 2010 at 8:28am
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.
Dec 6, 2010 at 10:18am
Thanks for the replies. The reason for the parent is for backtracking during the algorithms implementation. and to produce the path at the end.
Dec 7, 2010 at 12:07am
Thanks sohguanh for your adding.

b2ee
Topic archived. No new replies allowed.