Vector sort with a struct?

Discussion in 'C++' started by {AGUT2} {H}-IWIK, Sep 12, 2003.

  1. 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...
    {AGUT2} {H}-IWIK, Sep 12, 2003
    #1
    1. Advertising

  2. {AGUT2} {H}-IWIK

    tom_usenet Guest

    On Fri, 12 Sep 2003 12:39:26 +0100, {AGUT2} {H}-IWIK
    <> wrote:

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


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

    >
    >with
    >
    >bool comparex(myPoints& x, myPoints& y)


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


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


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

    Tom
    tom_usenet, Sep 12, 2003
    #2
    1. Advertising

  3. {AGUT2} {H}-IWIK

    Peter Kragh Guest

    "tom_usenet" <> wrote in message
    news:...
    > On Fri, 12 Sep 2003 12:39:26 +0100, {AGUT2} {H}-IWIK
    > <> wrote:
    >
    > >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);

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


    Should probably be:

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

    >
    > >
    > >with
    > >
    > >bool comparex(myPoints& x, myPoints& y)

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


    Try:

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

    instead.
    >
    >
    > >{
    > > return x.xc < y.xc;
    > >}
    > >
    > >the compiler throws a tantrum saying that sort is not a member function

    of
    > >the vertex<myPoints> vector.

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


    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
    Peter Kragh, Sep 13, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Angus Comber
    Replies:
    7
    Views:
    1,140
    Richard Heathfield
    Feb 5, 2004
  2. Chris Fogelklou
    Replies:
    36
    Views:
    1,335
    Chris Fogelklou
    Apr 20, 2004
  3. Oliver Block

    sort with vector<struct>

    Oliver Block, Sep 14, 2005, in forum: C++
    Replies:
    2
    Views:
    442
    Oliver Block
    Sep 16, 2005
  4. Replies:
    8
    Views:
    1,877
    Csaba
    Feb 18, 2006
  5. Navin
    Replies:
    1
    Views:
    660
    Ken Schaefer
    Sep 9, 2003
Loading...

Share This Page