473,508 Members | 2,457 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2154
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
1974
by: Joseph Cook | last post by:
I was wondering if there was any way to do the following: If I have a class Base, and multiple Derived classes which all have some different behavior on some function foo()...is there any good...
10
1710
by: Paul Schneider | last post by:
I want to sort a class derived from std::vector with STL sort.: template<typename T, typename fitParaType, typename fitResType> class Manipulator{ // shouldn't I now be able to access private...
0
1569
by: Mainlander | last post by:
I have a class that I designed that contains some objects in its fields, that are derived from the TList class. I want to use the TList Sort procedure to sort the items that are in the derived...
4
2154
by: DancnDude | last post by:
I have a class that needs to have several different kinds of sorting routines on an ArrayList that it needs to conditionally do based upon the data. I have successfully created a class that...
0
3674
by: Frank King | last post by:
Hi, I am using CArray and quick sort funciton to sort an array of double type of data points. I found an article in MSDN HOWTO: Quick Sorting Using MFC CArray-Derived Classes ID: Q216858 ...
4
18190
by: Robin Tucker | last post by:
How do I sort the items in a list box? I am using a class derived from IComparer to sort items on columns in a ListView, but the ListBox doesn't support this kind of facility. The "items" in my...
3
1981
by: mweltin | last post by:
I have been looking in the archives and as of yet have not found an answer to my problem. Class B has four members, and class A is derived from class B. Class A only adds one new member to class...
3
1167
by: xkenneth | last post by:
All, Sorry for the vague topic, but I really didn't know how to describe what I want to do. I'd like to almost do a traceback of my code for debugging and I thought this would be a really cool...
1
2452
by: Markus Dehmann | last post by:
In the following code example, I define several Comparator classes which contain different compare functions to use with std::sort. I have a Sorter class that gets passed a Comparator and is...
0
7224
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7118
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7323
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7379
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7493
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5625
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4706
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3192
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.