473,569 Members | 2,700 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

STL sort with user defined data type

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 vector of A objects only the member unique to the A
object changes.

For example say I have three A objects in a vector blah

blah[0] {one = 5 two = 0.8 three = 0.1 mysortvar = 1}
blah[1] {one = 2 two = 0.7 three = 0.2 mysortvar = 0}
blah[2] {one = 7 two = 0.6 three = 0.3 mysortvar = 3 }

I expect
blah[0] {one = 2 two = 0.7 three = 0.2 mysortvar = 0}
blah[1] {one = 5 two = 0.8 three = 0.1 mysortvar = 1}
blah[2] {one = 7 two = 0.6 three = 0.3 mysortvar = 3 }

but I get

blah[0] {one = 5 two = 0.8 three = 0.1 mysortvar = 0}
blah[1] {one = 2 two = 0.7 three = 0.2 mysortvar = 1}
blah[2] {one = 7 two = 0.6 three = 0.3 mysortvar = 3 }

Note:ONLY THE mysortvar values change.

could someone clue me into what I'm doing wrong?
thank you,
Markus

Jul 17 '07 #1
3 1984
mw*****@gmail.c om wrote:
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 vector of A objects only the member unique to the A
object changes.

For example say I have three A objects in a vector blah

blah[0] {one = 5 two = 0.8 three = 0.1 mysortvar = 1}
blah[1] {one = 2 two = 0.7 three = 0.2 mysortvar = 0}
blah[2] {one = 7 two = 0.6 three = 0.3 mysortvar = 3 }

I expect
blah[0] {one = 2 two = 0.7 three = 0.2 mysortvar = 0}
blah[1] {one = 5 two = 0.8 three = 0.1 mysortvar = 1}
blah[2] {one = 7 two = 0.6 three = 0.3 mysortvar = 3 }

but I get

blah[0] {one = 5 two = 0.8 three = 0.1 mysortvar = 0}
blah[1] {one = 2 two = 0.7 three = 0.2 mysortvar = 1}
blah[2] {one = 7 two = 0.6 three = 0.3 mysortvar = 3 }

Note:ONLY THE mysortvar values change.

could someone clue me into what I'm doing wrong?
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
Jul 17 '07 #2
On Jul 16, 3:00 pm, Kai-Uwe Bux <jkherci...@gmx .netwrote:
mwel...@gmail.c om wrote:
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 vector of A objects only the member unique to the A
object changes.
For example say I have three A objects in a vector blah
blah[0] {one = 5 two = 0.8 three = 0.1 mysortvar = 1}
blah[1] {one = 2 two = 0.7 three = 0.2 mysortvar = 0}
blah[2] {one = 7 two = 0.6 three = 0.3 mysortvar = 3 }
I expect
blah[0] {one = 2 two = 0.7 three = 0.2 mysortvar = 0}
blah[1] {one = 5 two = 0.8 three = 0.1 mysortvar = 1}
blah[2] {one = 7 two = 0.6 three = 0.3 mysortvar = 3 }
but I get
blah[0] {one = 5 two = 0.8 three = 0.1 mysortvar = 0}
blah[1] {one = 2 two = 0.7 three = 0.2 mysortvar = 1}
blah[2] {one = 7 two = 0.6 three = 0.3 mysortvar = 3 }
Note:ONLY THE mysortvar values change.
could someone clue me into what I'm doing wrong?

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
I'm in American Samoa and my internet connection is not what you would
call good, or even bad, its worse than bad. I'm having trouble
posting to this list so please forgive me if I reply more than once.
You are correct. To let you in on some more detail it is for a
genetic algorithm.
The base class is a "genome" and the derived class is an "individual .
Their overloaded assignment operators are posted below. As you can
see in the old version of the individual class assignment operator I
was copying all the members explicitly, and not using the base class
assignment operator. I'm not sure how these differ. I ran a test
with the newly suggested assignment operator and it works. Clearly,
there is something about the "sort", or other aspect of the STL I have
no idea about. If you have the time and would care to enlighten me I
would be grateful.

NNGenome & NNGenome::opera tor=(const NNGenome & oldNNG){
if(this != &oldNNG){ //Check for self assignment.
this->Epochs = oldNNG.Epochs;
this->LearnRate = oldNNG.LearnRat e;
this->Momentum = oldNNG.Momentum ;
this->NNStrDef = oldNNG.NNStrDef ;
}
return *this;
}

Individual & Individual::ope rator=(const Individual& GNN){
//check for self assigement.
if(this != &GNN){
this->BestFitness = GNN.BestFitness ;
this->Gepochs = GNN.Gepochs;
this->GlearnRate = GNN.GlearnRate;
this->Gmomentum = GNN.Gmomentum;
this->GnnStrDef = GNN.GnnStrDef;
}
return *this;
}
I switched the above to the following.
Individual & Individual::ope rator=(const Individual& GNN){
//check for self assigement.
if(this != &GNN){
NNGenome::opera tor=(GNN);
this->BestFitness = GNN.BestFitness ;
}
return *this;
}

Jul 17 '07 #3
mw*****@gmail.c om wrote:
On Jul 16, 3:00 pm, Kai-Uwe Bux <jkherci...@gmx .netwrote:
> mwel...@gmail.c om wrote:
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 vector of A objects only the member unique to the A
object changes.
For example say I have three A objects in a vector blah
blah[0] {one = 5 two = 0.8 three = 0.1 mysortvar = 1}
blah[1] {one = 2 two = 0.7 three = 0.2 mysortvar = 0}
blah[2] {one = 7 two = 0.6 three = 0.3 mysortvar = 3 }
I expect
blah[0] {one = 2 two = 0.7 three = 0.2 mysortvar = 0}
blah[1] {one = 5 two = 0.8 three = 0.1 mysortvar = 1}
blah[2] {one = 7 two = 0.6 three = 0.3 mysortvar = 3 }
but I get
blah[0] {one = 5 two = 0.8 three = 0.1 mysortvar = 0}
blah[1] {one = 2 two = 0.7 three = 0.2 mysortvar = 1}
blah[2] {one = 7 two = 0.6 three = 0.3 mysortvar = 3 }
Note:ONLY THE mysortvar values change.
could someone clue me into what I'm doing wrong?

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
I'm in American Samoa and my internet connection is not what you would
call good, or even bad, its worse than bad. I'm having trouble
posting to this list so please forgive me if I reply more than once.
You are correct. To let you in on some more detail it is for a
genetic algorithm.
The base class is a "genome" and the derived class is an "individual .
Their overloaded assignment operators are posted below. As you can
see in the old version of the individual class assignment operator I
was copying all the members explicitly, and not using the base class
assignment operator. I'm not sure how these differ. I ran a test
with the newly suggested assignment operator and it works. Clearly,
there is something about the "sort", or other aspect of the STL I have
no idea about. If you have the time and would care to enlighten me I
would be grateful.

NNGenome & NNGenome::opera tor=(const NNGenome & oldNNG){
if(this != &oldNNG){ //Check for self assignment.
this->Epochs = oldNNG.Epochs;
this->LearnRate = oldNNG.LearnRat e;
this->Momentum = oldNNG.Momentum ;
this->NNStrDef = oldNNG.NNStrDef ;
}
return *this;
}

Individual & Individual::ope rator=(const Individual& GNN){
//check for self assigement.
if(this != &GNN){
this->BestFitness = GNN.BestFitness ;
this->Gepochs = GNN.Gepochs;
this->GlearnRate = GNN.GlearnRate;
this->Gmomentum = GNN.Gmomentum;
this->GnnStrDef = GNN.GnnStrDef;
}
return *this;
}
Well, I note that the base class assignment mentioness the members

Epochs
LearnRate
Momentum
NNStrDef

whereas the assignment operator for the derived class is working on

BestFitness // new member
Gepochs // ?
GlearnRate // ?
Gmomentum // ?
GnnStrDef // ?

Without seeing more code, it is hard to be certain, but the mismatch in
identifiers could indicate that the copying of the base-class member is not
handled correctly (which matches the output).
I switched the above to the following.
Individual & Individual::ope rator=(const Individual& GNN){
//check for self assigement.
if(this != &GNN){
NNGenome::opera tor=(GNN);
this->BestFitness = GNN.BestFitness ;
}
return *this;
}
I do not quite see the need for avoiding self-assignment in the above
assignment operators. In fact, I do not see the need for declaring the
assignment operator at all: The compiler generated assignment operator does
member-wise assignment. Thus, you should be fine with removing all
assignment operators from both classes. (Similarly, you probably also don't
need to define the copy constructor).
Best

Kai-Uwe Bux
Jul 17 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2060
by: Derek Tinney | last post by:
Hi, I'm having difficulty building an XLST file that allows me to sort a list of log records. I put together an XSL file that allows me to output a copy of the input file and then I attempted to sort it. Eventually I want to filter it based on the "when" element (and/or others) but I cannot proceed until I get the sort to work. I have...
9
13088
by: JasBascom | last post by:
say i had 97456 and 23456 is there already a sort function to check which one begins with the smaller number rearrange it. in this case the bottom number should clearly be rearranged to the top as it lower in numerical order.
11
2985
by: JerryJ | last post by:
I have to fetch values from a database and store them in a vector in my c++ application. What is faster? if i fetch the values sorted from the database with a 'order by' clause in the sql-statement or if i fetch them into the vector unsorted and sort the vector then? Thanx for your help
6
2880
by: John Black | last post by:
I have a sort statement for vectr like this, sort(vec.begin(), vec.end(), EleSmaller); in running I find that the sort procedure falls into an endless loop, by adding some debug statement I see sort is endlessly doing some comparision on non-exist number! Then I replace sort() with stable_sort(), it works fine. This really confuses me,...
13
10404
by: dawatson833 | last post by:
I have several stored procedures with parameters that are defined with user defined data types. The time it takes to run the procedures can take 10 - 50 seconds depending on the procedure. If I change the parameter data types to the actual data type such as varchar(10), etc., the stored procedure takes less that a second to return records. ...
3
3983
by: gambler | last post by:
let's say you have: var games = new Array(); games = new GAME(gameNum, rotNum1, rotNum2, ... ); ( so a sparsley populate array which enables me to locate a game usin the game number without having to implement a "search" function on th games array
3
10267
by: RJN | last post by:
Hi I have a user defined data type which implements IComparable interface and has an implementation for the CompareTo method. Public Class MyDataType Implements IComparable --Private members --Public properties
8
4164
by: markww | last post by:
Hi, If I have a vector of structs like this: struct IMAGE { unsigned char *pPix; string strName; int nNumber; };
2
2062
by: joesindel | last post by:
So heres the deal... I work for a company in which numbers are dropping. The owners created a game where calling potential clients racks up points for crap like basketball ticekts and such. The owners called upon me to develop a system where users could enter information to list user name, data stamp it, and then add form data such as the...
0
13285
by: JosAH | last post by:
Greetings, I was asked to write a Tip Of the Week; so here goes: a lot of topics are started here in this forum (and a lot of other forums too) mentioning a problem about sorting data. Examples of two main problem scenarios are like this: 1) an array containing a first name and another array containing a last name and possibly a...
0
7698
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...
0
7612
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
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. ...
1
7673
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...
0
7970
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5513
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...
0
3653
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2113
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

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.