STL sort with derived class

M

mweltin

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
 
B

bnonaj

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
 
M

mweltin

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

jpalecek

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,814
Latest member
SpicetreeDigital

Latest Threads

Top