Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > std::list Dupli...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 5 Topic 45891 of 46738
Post > Topic >>

std::list Duplicates with Different Data

by mrc2323@[EMAIL PROTECTED] (Mike Copeland) May 12, 2008 at 09:49 PM

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
 




 5 Posts in Topic:
std::list Duplicates with Different Data
mrc2323@[EMAIL PROTECTED]  2008-05-12 21:49:44 
Re: std::list Duplicates with Different Data
"Alf P. Steinbach&qu  2008-05-13 07:04:36 
Re: std::list Duplicates with Different Data
Jerry Coffin <jcoffin@  2008-05-13 15:16:50 
Re: std::list Duplicates with Different Data
mrc2323@[EMAIL PROTECTED]  2008-05-13 14:35:15 
Re: std::list Duplicates with Different Data
Jerry Coffin <jcoffin@  2008-05-13 18:21:36 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed Jul 9 0:00:41 CDT 2008.