By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,480 Members | 1,711 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,480 IT Pros & Developers. It's quick & easy.

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

P: n/a
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[left], Points[right]);
}

Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
ve*********@hotmail.com wrote:
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[left], Points[right]);
}


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_[left], arg_[right]);
}

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

Jul 23 '05 #2

P: n/a
> 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_[left], arg_[right]);
}

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.

Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.