Re: Updating <map> Structure - Help!
Mike Copeland schrieb:
I'm having difficulty updating map objects. In the code I've
excerpted below, I store map objects without difficulty, but my attempts
to modify elements of the stored data don't work.
What means "don't work" in your case? Didn't it compile? Does it give
you false results? Explain what is wrong with your approach and what the
expected result is.
Also, please provide a *compilable* minimal example, with main()
function and all the stuff.
struct ChipRecord // Chip Times record
{
time_t hiStartTime; // Start Time
// much more...
} timeWork; // entrant info records
typedef map<int, ChipRecord> BCI;
BCI bci;
map<int, ChipRecord>::iterator bIter;
int nBib = 17;
time_t cTime = 11151544;
// initialize and populate "timeWork" structure, etc.
bci.insert(bci::value_type(bibNumber, timeWork));
//...
// obtain a cTime value...
bIter = bci.find(nBib);
It's better style to declare variables on first use. It's easier for the
reader of your program (co-workers, and in some weeks, yourself) to
understand the code:
BCI::iterator bIter = bci.find(nBib);
if(bIter != bci.end())
{
timeWork = bIter->second; // ???
Same here:
ChipRecord timeWork = bIter->second;
Note: This makes a copy of the object in the map. Changing timeWork only
changes this copy. If you want to modify the object inplace, take a
reference.
// pick a better name for the variable, too
ChipRecord& timeWork = bIter->second;
Changes to 'timeWork' will directly alter the object in the map.
timeWork = bci.find(nBib)->second; // ???
timeWork = (*bIter).second; // ???
(*bIter).second and bIter->second are the same.
if(cTime > timeWork.hiStartTime) // use highest time
{ // update the object
bIter->second.hiStartTime = cTime; // ???
timeWork.hiStartTime = cTime; // ???
} // if
} // if
It's the code directly above that doesn't do what I intend: change
the map object's value and update the object. There is much more going
on in the program, but this is the simplest amount that demonstrates the
issue. I don't know how to update the object. Please advise. TIA
--
Thomas