By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,427 Members | 1,863 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,427 IT Pros & Developers. It's quick & easy.

Updating <map> Structure - Help!

P: n/a
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, ChipRecordBCI;
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
Nov 17 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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, ChipRecordBCI;
* * * * 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
Nov 17 '08 #2

P: n/a
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, ChipRecordBCI;
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<{{
Nov 17 '08 #3

P: n/a
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, ChipRecordBCI;
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<{{
Nov 17 '08 #4

P: n/a
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, ChipRecordBCI;
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
Nov 17 '08 #5

P: n/a
mr*****@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

Nov 17 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.