473,666 Members | 2,048 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 2165
mw*****@gmail.c om 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.g02gfr 12...@ntlworld. comwrote:
mwel...@gmail.c om 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.c om 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
1978
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 way to have an object that can switch between these various functionalities..using a pointer to the generic class? as follows: class Status
10
1727
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 members with // Compare? friend class Compare; public: ...
0
1583
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 class. The derived class has an extra field that specifies how to sort the items. The problem I have is in the implementation of the Sort method in the way the class is implemented by Borland. The call to the Sort method must pass in the name of...
4
2162
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 implements IComparer and does the sort correctly for one of these sort types. This class's definition is like: // Sort class that implements IComparer so public class CompareFields1 : IComparer { public int Compare(object a, object b)
0
3687
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 The article works well with CStringArray. But when I wrote the following code about double type. It does not work for
4
18197
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 list box aren't just strings, they are a class I have defined for each list item. There is a comparitor function, but how can I write - ListBox.Items.Sort() in this instance? Do I need to do my own quicksort?
3
1988
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 B. Class A has '<', '>', '==' and '=' overloaded, as well as a copy constructor. Class B only has the copy constructor and assignment operator overloaded. I create a vector of A objects, and stuff them with random values. When I sort the...
3
1173
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 way to do it if possible. What I'd like to do, is define a base class. This base class would have a function, that gets called every time another function is called (regardless of whether in the base class or a derived class),
1
2463
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 supposed to sort using the specific Comparator that's passed to it. But it always uses the Comparator base class and not the derived class that it is supposed to use, although the functions are virtual. How can I make the code work? You might argue...
0
8444
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8869
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8781
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8551
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7386
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6198
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5664
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2771
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1775
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.