[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Updating Structure - Help!

mrc2323

11/17/2008 6:55:00 PM

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.

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);
if(bIter != bci.end())
{
timeWork = bIter->second; // ???
timeWork = bci.find(nBib)->second; // ???
timeWork = (*bIter).second; // ???
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
5 Answers

AnonMail2005@gmail.com

11/17/2008 7:32:00 PM

0

On Nov 17, 1:54 pm, mrc2...@cox.net (Mike Copeland) wrote:
>    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.
>
> 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);
>     if(bIter != bci.end())
>     {
>         timeWork = bIter->second;              // ???
>         timeWork = bci.find(nBib)->second;     // ???
>         timeWork = (*bIter).second;            // ???
>         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

In your posted code you insert an element with the key bibNumber. But
then you find with the key nBib. The former is not defined in your
posted code.

Why not just do a simple program and print out some results or run it
through a debugger?

HTH

mrc2323

11/17/2008 9:58:00 PM

0

> > I'm having difficulty updating map objects. =A0In 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.
> >
> > 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;
> >
> > nBib = 17;
> > time_t cTime = 11151544;
> >
> > // initialize and populate "timeWork" structure, etc.
> > bci.insert(bci::value_type(nBib, timeWork));
> > //...
> > // obtain a cTime value...
> > bIter =3D bci.find(nBib);
> > if(bIter !=3D bci.end())
> > {
> > timeWork = bIter->second; //???
> > timeWork = bci.find(nBib)->second; // ???
> > timeWork = (*bIter).second; // ???
> > 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. =A0There is much more going
> > on in the program, but this is the simplest amount that demonstrates the
> > issue. =A0I don't know how to update the object. =A0Please advise. TIA
>
> In your posted code you insert an element with the key bibNumber. But
> then you find with the key nBib. The former is not defined in your
> posted code.

Yes, my apologies. I tried to post a simple segment of my program,
and I made that mistake.
Nevertheless, I still need help on this. I had hoped my code
explained the problem... 8<{{

mrc2323

11/17/2008 9:58:00 PM

0

> > I'm having difficulty updating map objects. =A0In 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.
> >
> > 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;
> >
> > nBib = 17;
> > time_t cTime = 11151544;
> >
> > // initialize and populate "timeWork" structure, etc.
> > bci.insert(bci::value_type(nBib, timeWork));
> > //...
> > // obtain a cTime value...
> > bIter =3D bci.find(nBib);
> > if(bIter !=3D bci.end())
> > {
> > timeWork = bIter->second; //???
> > timeWork = bci.find(nBib)->second; // ???
> > timeWork = (*bIter).second; // ???
> > 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. =A0There is much more going
> > on in the program, but this is the simplest amount that demonstrates the
> > issue. =A0I don't know how to update the object. =A0Please advise. TIA
>
> In your posted code you insert an element with the key bibNumber. But
> then you find with the key nBib. The former is not defined in your
> posted code.

Yes, my apologies. I tried to post a simple segment of my program,
and I made that mistake.
Nevertheless, I still need help on this. I had hoped my code
explained the problem... 8<{{

Thomas J. Gritzan

11/17/2008 10:33:00 PM

0

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

Paavo Helde

11/17/2008 11:18:00 PM

0

mrc2323@cox.net (Mike Copeland) kirjutas:

> 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.
[...]
> map<int, ChipRecord>::iterator bIter;
[...]
> bIter = bci.find(nBib);
> if(bIter != bci.end())
> {
[...]
> bIter->second.hiStartTime = cTime; // ???

This seems to be basically correct, so your error is probably elsewhere.
Try to prepare a compilable short example of the problem; during this you
will probably find the cause of the problem; if not, post here again!

hth
Paavo