M
Mike Copeland
How do I work with a std::list that might have multiple objects
having the same "key", but which have other data that is different?
Here's code that compiles, but doesn't do quite what I expect:
(Please note that there's some specialized I/o code here, but the
logic flow should be clear...)
struct GenCheck // Gender Check data
{
char genCode;
int useCount;
string firstName;
bool operator==(const GenCheck &rhs) const
{
return firstName == rhs.firstName;
}
bool operator<(const GenCheck &rhs) const
{
return firstName < rhs.firstName;
}
bool operator==(const string &rhs) const
{
return firstName == rhs;
}
bool operator<(const string &rhs) const
{
return firstName < rhs;
}
} gWork;
typedef list<GenCheck> NAMES;
NAMES genData;
list<GenCheck>::iterator gIter;
void nameGenderFile() // maintain NAMECHK.DAT file
{
int ii, jj = 0, kk = 0, mCt = 0, fCt = 0, uCt = 0;
char gCode;
Str24 sName, fName, lName, priorName = "";
strcpy(f3, "myfile.hst"), fv3.openFile("", f3, "", "rt");
while(getFinData(&fv3)) // *** this is my own I/o routine
{
strcpy(sName, FinData.EN), splitNames(sName, fName, lName);
if(strcmp(sName, priorName) == 0) continue; // same name
else strcpy(priorName, sName);
gCode = FinData.Sx, strcpy(sName, t_f.upper(TTB(fName))), kk++;
gIter = find(genData.begin(), genData.end(), sName);
if(gIter != genData.end()) // found a match
{
gWork = (*gIter);
if(gCode != gWork.genCode) // gender mismatch
{ // gender mismatch - make entry for other gender
gIter = find(gIter, genData.end(), sName);
if(gIter != genData.end()) // found another match
{ // update count for different gender
gWork = (*gIter), gWork.useCount++;
continue;
}
else
{ // different gender's object doesn't exist
// *** The following code block doesn't execute: if there isn't another
// matching name, this code should create a new std::lst object with the
// same name but different gender. That is, if the first "CHRIS" is a
// female, the code below (logic #2) creates a new std:list object (and
// it does), but if a 2nd "CHRIS" is found that's a male, this code
// should create a new std::list object.
// It doesn't... 8<{{
//
gWork.genCode = gCode, gWork.useCount = 1;
gWork.firstName = sName;
switch(gCode)
{
case 'F': fCt++; break;
case 'M': mCt++; break;
}
genData.push_back(gWork);
sprintf(emsg, "Males: %d Females: %d", mCt, fCt);
fastWrite(2, DSLINE, HINORM, emsg);
}
} // if
else
{ // name/gender match - adjust count
gWork.useCount++;
*gIter = gWork;
}
}
else // this name isn't on the database
{
// *** logic #2
gWork.genCode = gCode, gWork.useCount = 1;
gWork.firstName = sName;
switch(gCode)
{
case 'F': fCt++; break;
case 'M': mCt++; break;
}
genData.push_back(gWork);
sprintf(emsg, "Males: %d Females: %d", mCt, fCt);
fastWrite(2, DSLINE, HINORM, emsg);
}
} // while
fv3.closeFile();
return;
} // nameGenderFile
What have I done wrong here? Please advise. TIA
having the same "key", but which have other data that is different?
Here's code that compiles, but doesn't do quite what I expect:
(Please note that there's some specialized I/o code here, but the
logic flow should be clear...)
struct GenCheck // Gender Check data
{
char genCode;
int useCount;
string firstName;
bool operator==(const GenCheck &rhs) const
{
return firstName == rhs.firstName;
}
bool operator<(const GenCheck &rhs) const
{
return firstName < rhs.firstName;
}
bool operator==(const string &rhs) const
{
return firstName == rhs;
}
bool operator<(const string &rhs) const
{
return firstName < rhs;
}
} gWork;
typedef list<GenCheck> NAMES;
NAMES genData;
list<GenCheck>::iterator gIter;
void nameGenderFile() // maintain NAMECHK.DAT file
{
int ii, jj = 0, kk = 0, mCt = 0, fCt = 0, uCt = 0;
char gCode;
Str24 sName, fName, lName, priorName = "";
strcpy(f3, "myfile.hst"), fv3.openFile("", f3, "", "rt");
while(getFinData(&fv3)) // *** this is my own I/o routine
{
strcpy(sName, FinData.EN), splitNames(sName, fName, lName);
if(strcmp(sName, priorName) == 0) continue; // same name
else strcpy(priorName, sName);
gCode = FinData.Sx, strcpy(sName, t_f.upper(TTB(fName))), kk++;
gIter = find(genData.begin(), genData.end(), sName);
if(gIter != genData.end()) // found a match
{
gWork = (*gIter);
if(gCode != gWork.genCode) // gender mismatch
{ // gender mismatch - make entry for other gender
gIter = find(gIter, genData.end(), sName);
if(gIter != genData.end()) // found another match
{ // update count for different gender
gWork = (*gIter), gWork.useCount++;
continue;
}
else
{ // different gender's object doesn't exist
// *** The following code block doesn't execute: if there isn't another
// matching name, this code should create a new std::lst object with the
// same name but different gender. That is, if the first "CHRIS" is a
// female, the code below (logic #2) creates a new std:list object (and
// it does), but if a 2nd "CHRIS" is found that's a male, this code
// should create a new std::list object.
// It doesn't... 8<{{
//
gWork.genCode = gCode, gWork.useCount = 1;
gWork.firstName = sName;
switch(gCode)
{
case 'F': fCt++; break;
case 'M': mCt++; break;
}
genData.push_back(gWork);
sprintf(emsg, "Males: %d Females: %d", mCt, fCt);
fastWrite(2, DSLINE, HINORM, emsg);
}
} // if
else
{ // name/gender match - adjust count
gWork.useCount++;
*gIter = gWork;
}
}
else // this name isn't on the database
{
// *** logic #2
gWork.genCode = gCode, gWork.useCount = 1;
gWork.firstName = sName;
switch(gCode)
{
case 'F': fCt++; break;
case 'M': mCt++; break;
}
genData.push_back(gWork);
sprintf(emsg, "Males: %d Females: %d", mCt, fCt);
fastWrite(2, DSLINE, HINORM, emsg);
}
} // while
fv3.closeFile();
return;
} // nameGenderFile
What have I done wrong here? Please advise. TIA