468,746 Members | 1,855 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,746 developers. It's quick & easy.

STL sort with derived class

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 '07 #1
3 1907
mw*****@gmail.com 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
Jul 17 '07 #2
On Jul 16, 9:35 pm, bnonaj <n2xssvv.g02gfr12...@ntlworld.comwrote:
mwel...@gmail.com 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::operator=:

A & operator= ( A const & other ) {
B::operator=( 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 '07 #3
On Jul 17, 9:06 am, mwel...@gmail.com 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 17 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Joseph Cook | last post: by
10 posts views Thread by Paul Schneider | last post: by
reply views Thread by Mainlander | last post: by
4 posts views Thread by DancnDude | last post: by
4 posts views Thread by Robin Tucker | last post: by
3 posts views Thread by mweltin | last post: by
3 posts views Thread by xkenneth | last post: by
1 post views Thread by CARIGAR | last post: by
xarzu
2 posts views Thread by xarzu | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.