| 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 |