473,372 Members | 1,074 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

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 1978
mw*****@gmail.com 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.com 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::operator=(const NNGenome & oldNNG){
if(this != &oldNNG){ //Check for self assignment.
this->Epochs = oldNNG.Epochs;
this->LearnRate = oldNNG.LearnRate;
this->Momentum = oldNNG.Momentum;
this->NNStrDef = oldNNG.NNStrDef;
}
return *this;
}

Individual & Individual::operator=(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::operator=(const Individual& GNN){
//check for self assigement.
if(this != &GNN){
NNGenome::operator=(GNN);
this->BestFitness = GNN.BestFitness;
}
return *this;
}

Jul 17 '07 #3
mw*****@gmail.com wrote:
On Jul 16, 3:00 pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:
> mwel...@gmail.com 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::operator=(const NNGenome & oldNNG){
if(this != &oldNNG){ //Check for self assignment.
this->Epochs = oldNNG.Epochs;
this->LearnRate = oldNNG.LearnRate;
this->Momentum = oldNNG.Momentum;
this->NNStrDef = oldNNG.NNStrDef;
}
return *this;
}

Individual & Individual::operator=(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::operator=(const Individual& GNN){
//check for self assigement.
if(this != &GNN){
NNGenome::operator=(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
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...
9
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...
11
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...
6
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...
13
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...
3
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...
3
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...
8
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
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...
0
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. ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.