sort according to a particular value 
Author Message
 sort according to a particular value

Say I have a vector<vector<float> >, and vectors inside
are always size 3 (for instance, I do

vector<vector<float> > points;

points.resize(25, vector<float>(3))

to get 25 points, each with 3 coords.)  After storing
the values, I want to sort according to the X value
only (that is, sort points according to the [0] value of
the size-3 vectors inside).

Is there an easy way to use sort on this w/o defining a functor
class?

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Mon, 29 Jul 2002 03:00:00 GMT  
 sort according to a particular value
struct coords
{
        float    c[3];    // you could use vector<float> here, but that's
just needless overhead.

        bool operator<(const coords& rhs) const
        {   return c[0] < rhs.c[0]; }

Quote:
};

vector< coords> points;

--
Truth,
James Curran
http://www.NJTheater.com
http://www.NJTheater.com/JamesCurran

Quote:

> Say I have a vector<vector<float> >, and vectors inside
> are always size 3 (for instance, I do

> vector<vector<float> > points;

> points.resize(25, vector<float>(3))

> to get 25 points, each with 3 coords.)  After storing
> the values, I want to sort according to the X value
> only (that is, sort points according to the [0] value of
> the size-3 vectors inside).

> Is there an easy way to use sort on this w/o defining a functor
> class?



Mon, 29 Jul 2002 03:00:00 GMT  
 sort according to a particular value
Thanks -

So there's no way to do this without having to define
a class/struct?  It's just sometimes I only need a
{*filter*} computation like this one time in my code, and having
a separate class for every computation can be overkill.

Is there any way to "get at" the more internal data to use
as a comparison, or is there just not really a way to express
this with STL - so I can do

sort(points.begin(), points.end(), -some fancy expression to sort
based on the first coord of the vector in each element-);

?



Quote:
> struct coords
> {
>         float    c[3];    // you could use vector<float> here, but
that's
> just needless overhead.

>         bool operator<(const coords& rhs) const
>         {   return c[0] < rhs.c[0]; }
> };

> vector< coords> points;

> --
> Truth,
> James Curran
> http://www.*-*-*.com/
> http://www.*-*-*.com/




- Show quoted text -

Quote:
> > Say I have a vector<vector<float> >, and vectors inside
> > are always size 3 (for instance, I do

> > vector<vector<float> > points;

> > points.resize(25, vector<float>(3))

> > to get 25 points, each with 3 coords.)  After storing
> > the values, I want to sort according to the X value
> > only (that is, sort points according to the [0] value of
> > the size-3 vectors inside).

> > Is there an easy way to use sort on this w/o defining a functor
> > class?

Sent via Deja.com http://www.*-*-*.com/
Before you buy.


Tue, 30 Jul 2002 03:00:00 GMT  
 sort according to a particular value

Quote:

>Thanks -

>So there's no way to do this without having to define
>a class/struct?

You can do it without defining a class/struct for your 3-element
coordinate, but you'll still need to define a class/struct for a
comparison function object to pass to the sort() routine.  This is
quite easy, though.  See below...

Quote:
>It's just sometimes I only need a
>{*filter*} computation like this one time in my code, and having
>a separate class for every computation can be overkill.

>Is there any way to "get at" the more internal data to use
>as a comparison, or is there just not really a way to express
>this with STL - so I can do

>sort(points.begin(), points.end(), -some fancy expression to sort
>based on the first coord of the vector in each element-);

>?

// Comparison function object to pass to sort()...

struct xCoordLess {
   bool operator () (const vector<float> &lhs,
                        const vector<float> &rhs) const
   { return lhs[0] < rhs[0] }

Quote:
};

Now you can say:

sort(points.begin(), points.end(), xCoordLess());

HTH,
Andy C



Wed, 31 Jul 2002 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. How to sort list view according to column?

2. How can I sort according to column

3. How to sort structs by particular member?

4. How to show particular #define'd value?

5. How to read a particular key value from Registry

6. How to read a particular key value from Registry

7. How to read a particular key value from Registry

8. Sorting on double values.

9. SortedList sorted by value.

10. Sorting multiple values at once.

11. Sorting Array Error - left operand must be L-value

12. How To: Sort map by Value(second)

 

 
Powered by phpBB® Forum Software