Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Sort Vector of Objects (c++)

Name: Anonymous 2009-01-20 5:26

Hello,

I have the following code for sorting a Vector of vertices by their 'x' coordinate value:

(Vertex.h)
...
bool operator<(const Vertex& other) { return (this->x < other.x); }

(elsewhere)
    std::vector<Vertex*> vertices;
...    //vector is populated
    std::sort(vertices.begin(), vertices.end());

This doesn't do the desired sorting, for whatever reason.
Is something wrong with my code?

Name: T HELP HIM!!!! 2009-01-20 5:39

std::vector<Vertex*>
You're sorting the pointers, not the Vertices themselves.

bool CompareVertices(Vertex const* x, Vertex const* y) {
  //...
}

//...
std::sort(vertices.begin(), vertices.end(), CompareVertices);


Or a function object if you like.

Name: Anonymous 2009-01-20 6:27

yes something's wrong. youre using c++ like a BIG GAY FAG

Name: Anonymous 2009-01-20 6:50


    bool compareVertices(Vertex const* a, Vertex const* b)
    {
        return (a->getX() < b->getX());   //problem line
    }


"'Vertex::getX' : cannot convert 'this' pointer from 'const Vertex' to 'Vertex &'", so says VC++.

Name: Anonymous 2009-01-20 6:52

>>3

just fuck off

Name: Anonymous 2009-01-20 6:56

>>4
That's because your getters aren't const correct.

You probably have float getX(); but it should be
float getX() const;.

Name: Anonymous 2009-01-20 7:15

>>6
 
Thanks. I ran into a bunch of trouble with declaring the comparator function inside my class, but it works fine when global.

Name: Anonymous 2009-01-20 7:41

>>5
no u

Name: Anonymous 2009-03-06 10:35

The bindM and returnM.

Name: Anonymous 2011-01-31 20:41

<-- check em dubz

Don't change these.
Name: Email:
Entire Thread Thread List