STL sort with derived class

Discussion in 'C++' started by mweltin@gmail.com, Jul 17, 2007.

  1. Guest

    I have a derived class that extends a base class by adding a float
    member. The strange part is when I use the STL sort algorithm on a
    vector of derived classes, only hte float member is sorted.
    For example assume a vector containg three derived class objects
    called blah.
    blah[0] { ... int one = 7 , int two = 5, int three =8 , float five =
    1}
    blah[1] { ... int one = 6 , int two = 6, int three =4 , float five =
    0}
    blah[2] { ... int one = 5 , int two = 7, int three =5 , float five =
    3}
    both the derived class and base class have a copy constructor and the
    assignment operator overloaded. The derived class also overloads the
    binary operators '<', '>', and '==', al of which define their return
    value based only on the float member (five).
    After a sort I end up with
    blah[0] { ... int one = 7 , int two = 5, int three =8 , float five =
    0}
    blah[1] { ... int one = 6 , int two = 6, int three =4 , float five =
    1}
    blah[2] { ... int one = 5 , int two = 7, int three =5 , float five =
    3}
    But what I expect and would like is
    blah[0] { ... int one = 6 , int two = 6, int three =4 , float five =
    0}
    blah[1] { ... int one = 7 , int two = 5, int three =8 , float five =
    1}
    blah[2] { ... int one = 5 , int two = 7, int three =5 , float five =
    3}

    I'm having a devil of a time trying to figure this out.
    thank you for your time,
    Markus
    , Jul 17, 2007
    #1
    1. Advertising

  2. bnonaj Guest

    wrote:
    > I have a derived class that extends a base class by adding a float
    > member. The strange part is when I use the STL sort algorithm on a
    > vector of derived classes, only hte float member is sorted.
    > For example assume a vector containg three derived class objects
    > called blah.
    > blah[0] { ... int one = 7 , int two = 5, int three =8 , float five =
    > 1}
    > blah[1] { ... int one = 6 , int two = 6, int three =4 , float five =
    > 0}
    > blah[2] { ... int one = 5 , int two = 7, int three =5 , float five =
    > 3}
    > both the derived class and base class have a copy constructor and the
    > assignment operator overloaded. The derived class also overloads the
    > binary operators '<', '>', and '==', al of which define their return
    > value based only on the float member (five).
    > After a sort I end up with
    > blah[0] { ... int one = 7 , int two = 5, int three =8 , float five =
    > 0}
    > blah[1] { ... int one = 6 , int two = 6, int three =4 , float five =
    > 1}
    > blah[2] { ... int one = 5 , int two = 7, int three =5 , float five =
    > 3}
    > But what I expect and would like is
    > blah[0] { ... int one = 6 , int two = 6, int three =4 , float five =
    > 0}
    > blah[1] { ... int one = 7 , int two = 5, int three =8 , float five =
    > 1}
    > blah[2] { ... int one = 5 , int two = 7, int three =5 , float five =
    > 3}
    >
    > I'm having a devil of a time trying to figure this out.
    > thank you for your time,
    > Markus
    >


    Without sample code to demonstrate the problem it is difficult to give
    much or any help with your problem,
    It would appear to me the operator <() for the derived class only
    compares the float member which is why the sort is done against the
    float. To sort based on both you will need comparison function which can
    be passed to the stl::sort() which does the required comparison.

    JB
    bnonaj, Jul 17, 2007
    #2
    1. Advertising

  3. Guest

    On Jul 16, 9:35 pm, bnonaj <> wrote:
    > wrote:
    > > I have a derived class that extends a base class by adding a float
    > > member. The strange part is when I use the STL sort algorithm on a
    > > vector of derived classes, only hte float member is sorted.
    > > For example assume a vector containg three derived class objects
    > > called blah.
    > > blah[0] { ... int one = 7 , int two = 5, int three =8 , float five =
    > > 1}
    > > blah[1] { ... int one = 6 , int two = 6, int three =4 , float five =
    > > 0}
    > > blah[2] { ... int one = 5 , int two = 7, int three =5 , float five =
    > > 3}
    > > both the derived class and base class have a copy constructor and the
    > > assignment operator overloaded. The derived class also overloads the
    > > binary operators '<', '>', and '==', al of which define their return
    > > value based only on the float member (five).
    > > After a sort I end up with
    > > blah[0] { ... int one = 7 , int two = 5, int three =8 , float five =
    > > 0}
    > > blah[1] { ... int one = 6 , int two = 6, int three =4 , float five =
    > > 1}
    > > blah[2] { ... int one = 5 , int two = 7, int three =5 , float five =
    > > 3}
    > > But what I expect and would like is
    > > blah[0] { ... int one = 6 , int two = 6, int three =4 , float five =
    > > 0}
    > > blah[1] { ... int one = 7 , int two = 5, int three =8 , float five =
    > > 1}
    > > blah[2] { ... int one = 5 , int two = 7, int three =5 , float five =
    > > 3}

    >
    > > I'm having a devil of a time trying to figure this out.
    > > thank you for your time,
    > > Markus

    >
    > Without sample code to demonstrate the problem it is difficult to give
    > much or any help with your problem,
    > It would appear to me the operator <() for the derived class only
    > compares the float member which is why the sort is done against the
    > float. To sort based on both you will need comparison function which can
    > be passed to the stl::sort() which does the required comparison.
    >
    > JB


    Dear List:
    My internet connection is very bad, currently I'm in American Samoa
    and I ended up posting this question twice. Below is a response I got
    that fixed my problem.
    Post at least the copy-constructors and assignment operators. My guess
    is
    that at least one of them (for class A) does not forward the call to
    the
    B-subobject. If I had to bet, I would venture the conjecture that the
    first
    line is missing in A::eek:perator=:

    A & operator= ( A const & other ) {
    B::eek:perator=( other ); // assign the B subobject
    mysortvar = other.mysortvar; // assign the additional member
    return ( *this );
    }

    Best

    Kai-Uwe Bux

    thanks you and I apologize for the multiple posts.
    , Jul 17, 2007
    #3
  4. Guest

    On Jul 17, 9:06 am, wrote:
    > I have a derived class that extends a base class by adding a float
    > member. The strange part is when I use the STL sort algorithm on a
    > vector of derived classes, only hte float member is sorted.
    > For example assume a vector containg three derived class objects
    > called blah.
    > blah[0] { ... int one = 7 , int two = 5, int three =8 , float five =
    > 1}
    > blah[1] { ... int one = 6 , int two = 6, int three =4 , float five =
    > 0}
    > blah[2] { ... int one = 5 , int two = 7, int three =5 , float five =
    > 3}
    > both the derived class and base class have a copy constructor and the
    > assignment operator overloaded. The derived class also overloads the
    > binary operators '<', '>', and '==', al of which define their return
    > value based only on the float member (five).
    > After a sort I end up with
    > blah[0] { ... int one = 7 , int two = 5, int three =8 , float five =
    > 0}
    > blah[1] { ... int one = 6 , int two = 6, int three =4 , float five =
    > 1}
    > blah[2] { ... int one = 5 , int two = 7, int three =5 , float five =
    > 3}
    > But what I expect and would like is
    > blah[0] { ... int one = 6 , int two = 6, int three =4 , float five =
    > 0}
    > blah[1] { ... int one = 7 , int two = 5, int three =8 , float five =
    > 1}
    > blah[2] { ... int one = 5 , int two = 7, int three =5 , float five =
    > 3}
    >
    > I'm having a devil of a time trying to figure this out.
    > thank you for your time,


    It is hard to judge without the code, but it seems that the assignment
    operator (or swap, if you had defined it) is not working properly. For
    example, you will get similar results if the assignment operator is a
    no-op. Try to look at this.

    Regards
    Jiri Palecek
    , Jul 18, 2007
    #4
    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. Vincent RICHOMME

    STL derived class : parse list from inside

    Vincent RICHOMME, Nov 8, 2006, in forum: C++
    Replies:
    1
    Views:
    391
    Salt_Peter
    Nov 9, 2006
  2. Replies:
    1
    Views:
    383
    myork
    May 23, 2007
  3. Replies:
    1
    Views:
    371
    Victor Bazarov
    May 23, 2007
  4. David
    Replies:
    3
    Views:
    384
    Grizlyk
    Jan 29, 2008
  5. Navin
    Replies:
    1
    Views:
    668
    Ken Schaefer
    Sep 9, 2003
Loading...

Share This Page