Looking for a better way to sort based on a third argument.

V

velthuijsen

The STL sort only accepts a function that is defined in the form of
bool Fname(<type>, <type>) in which <type> is the type of range to be
sorted.
I'm looking for a way to be able to sort the range on a different set
of types then the range.

I currently have worked out a way to do it (see code below) but it
seems fairly involved and I was wondering if there is a better way of
doing it?

Point.h contains a simple struct plus a few basic operations on this
struct.
PointUtility.h contains advanced functions that work with the Point
struct.
Header:

#include "Point.h"
class MySort
{
public:
static void Sort(int *Begin, int *End, const Point *Argument);
static bool Compare(int Left, int Right);
private:
static const Point *Points;
};

CPP:

#include "MySort.h"
#include "PointUtility.h"
#include <algorithm>

const Point* MySort::points = 0;

void MySort::Sort(int *Begin, int *End, const Point *Argument)
{
Points = Argument;
std::sort(Begin, End, MySort::Compare);
Points = 0;
}

bool MySort::Compare(int Left, int Right)
{
return PointCompareXYorder(Points
, Points
);
}​
 
J

Jonathan Mcdougall

The STL sort only accepts a function that is defined in the form of
bool Fname(<type>, <type>) in which <type> is the type of range to be
sorted.
I'm looking for a way to be able to sort the range on a different set
of types then the range.

I currently have worked out a way to do it (see code below) but it
seems fairly involved and I was wondering if there is a better way of
doing it?

Point.h contains a simple struct plus a few basic operations on this
struct.
PointUtility.h contains advanced functions that work with the Point
struct.
Header:

#include "Point.h"
class MySort
{
public:
static void Sort(int *Begin, int *End, const Point *Argument);
static bool Compare(int Left, int Right);
private:
static const Point *Points;
};

CPP:

#include "MySort.h"
#include "PointUtility.h"
#include <algorithm>

const Point* MySort::points = 0;

void MySort::Sort(int *Begin, int *End, const Point *Argument)
{
Points = Argument;
std::sort(Begin, End, MySort::Compare);
Points = 0;
}

bool MySort::Compare(int Left, int Right)
{
return PointCompareXYorder(Points
, Points
);
}​


That works, but we usually use function objects:

class point_comparer
{
public:
point_comparer(Point &arg)
: arg_(arg)
{
}

bool operator()(int left, int right)
{
return PointCompareXYorder(arg_
, arg_
);
}

private:
Point &arg_;
};

Now I don't know how you were calling MySort::sort(), but something
like

std::sort(begin, end, point_comparer(points));

should do.

Also, your int* in MySort::sort() looks like an iterator. You should
typedef it
instead of using it directly, such as

class ListOfPoints
{
public:
typedef int* iterator;
};

Now, again, I don't know where the ints come from, but you should get
the picture.


Jonathan​
 
V

velthuijsen

That works, but we usually use function objects:
class point_comparer
{
public:
point_comparer(Point &arg)
: arg_(arg)
{
}

bool operator()(int left, int right)
{
return PointCompareXYorder(arg_
, arg_
);
}

private:
Point &arg_;
};​


A functor is exactly what I was looking for.
Thank you.
Now I don't know how you were calling MySort::sort(), but something
like

std::sort(begin, end, point_comparer(points));

should do.

Also, your int* in MySort::sort() looks like an iterator. You should
typedef it
instead of using it directly, such as

class ListOfPoints
{
public:
typedef int* iterator;
};

First setup of solving a specific problem. The next step would have
been templatisation so that any iterator (or iterator like) types could
have been used. But the functor solution made this approach obsolete.​
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top