Connecting Tech Pros Worldwide Forums | Help | Site Map

Vector sort with a struct?

{AGUT2} {H}-IWIK
Guest
 
Posts: n/a
#1: Jul 19 '05
Guys, I have these headers:
#include <stdlib>
#include <math>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>

*amongst others) and a a data structure:

// Creates part of basic framework of the output numbering system
struct vertexPoints {
int num;
double xc;
double yc;
double zc;
};

from this, I have created a vector

vector<vertexPoints> myPoints;

with vertices push_back()'ed onto it. I want to be able to sort this -
mainly by x, but ideally by x, then y, then z.

if I type mypoints.sort(myPoints.begin(), myPoints.end, comparex);

with

bool comparex(myPoints& x, myPoints& y)
{
return x.xc < y.xc;
}

the compiler throws a tantrum saying that sort is not a member function of
the vertex<myPoints> vector.

What am I doing wrong?

TIA,

Alex.

--
Reply to:alex an.ti livingstone sp@am btinternet.com cutting the usual...

tom_usenet
Guest
 
Posts: n/a
#2: Jul 19 '05

re: Vector sort with a struct?


On Fri, 12 Sep 2003 12:39:26 +0100, {AGUT2} {H}-IWIK
<alexan.tilivingstonesp@ambtinternet.com> wrote:
[color=blue]
>Guys, I have these headers:
>#include <stdlib>
>#include <math>
>#include <iostream>
>#include <iomanip>
>#include <fstream>
>#include <vector>
>#include <string>
>#include <algorithm>
>
>*amongst others) and a a data structure:
>
>// Creates part of basic framework of the output numbering system
>struct vertexPoints {
> int num;
> double xc;
> double yc;
> double zc;
>};
>
>from this, I have created a vector
>
>vector<vertexPoints> myPoints;
>
>with vertices push_back()'ed onto it. I want to be able to sort this -
>mainly by x, but ideally by x, then y, then z.
>
>if I type mypoints.sort(myPoints.begin(), myPoints.end, comparex);[/color]

std::sort(myPoints.begin(), myPoints.end, comparex);
[color=blue]
>
>with
>
>bool comparex(myPoints& x, myPoints& y)[/color]

bool comparex(myPoints const& x, myPoints const& y)

[color=blue]
>{
> return x.xc < y.xc;
>}
>
>the compiler throws a tantrum saying that sort is not a member function of
>the vertex<myPoints> vector.[/color]

Sort is not a member of vector, it is a namespace std function.

Tom
Peter Kragh
Guest
 
Posts: n/a
#3: Jul 19 '05

re: Vector sort with a struct?



"tom_usenet" <tom_usenet@hotmail.com> wrote in message
news:u9d3mvcv8p7tvvsa4qsfeu0kuuloj067m6@4ax.com...[color=blue]
> On Fri, 12 Sep 2003 12:39:26 +0100, {AGUT2} {H}-IWIK
> <alexan.tilivingstonesp@ambtinternet.com> wrote:
>[color=green]
> >Guys, I have these headers:
> >#include <stdlib>
> >#include <math>
> >#include <iostream>
> >#include <iomanip>
> >#include <fstream>
> >#include <vector>
> >#include <string>
> >#include <algorithm>
> >
> >*amongst others) and a a data structure:
> >
> >// Creates part of basic framework of the output numbering system
> >struct vertexPoints {
> > int num;
> > double xc;
> > double yc;
> > double zc;
> >};
> >
> >from this, I have created a vector
> >
> >vector<vertexPoints> myPoints;
> >
> >with vertices push_back()'ed onto it. I want to be able to sort this -
> >mainly by x, but ideally by x, then y, then z.
> >
> >if I type mypoints.sort(myPoints.begin(), myPoints.end, comparex);[/color]
>
> std::sort(myPoints.begin(), myPoints.end, comparex);[/color]

Should probably be:

std::sort(myPoints.begin(), myPoints.end(), comparex);
[color=blue]
>[color=green]
> >
> >with
> >
> >bool comparex(myPoints& x, myPoints& y)[/color]
>
> bool comparex(myPoints const& x, myPoints const& y)[/color]

Try:

bool comparex(const vertexPoints& x, const vertexPoints& y)

instead.[color=blue]
>
>[color=green]
> >{
> > return x.xc < y.xc;
> >}
> >
> >the compiler throws a tantrum saying that sort is not a member function[/color][/color]
of[color=blue][color=green]
> >the vertex<myPoints> vector.[/color]
>
> Sort is not a member of vector, it is a namespace std function.
>
> Tom[/color]

You could also consider making a "bool operator<(const vertexPoints&)" in
your vertextPoints struct. Then you could use the sort function like this:

std::sort(myPoints.begin(), myPoints.end());

That is IMHO a more object oriented way of doing this.

BR,
Peter Kragh


Closed Thread