Changing STL map Object Data

M

Mike Copeland

I have the following definition and declaration:

typedef struct ChipRecord // Chip Times record
{
int bibNum; // Bib #
short entAge; // Age
char entGender; // Gender (M/F)
char entRCode; // RCode
char entECode; // Entrant Type code
string entName; // Entrant Name
} tData; // entrant info records
tData tWork;
typedef map<int, ChipRecord> BCI;
BCI bci;
map<int, ChipRecord>::iterator bIter;

I'm reading a data file and storing objects of this type:

tWork.entName = "Doe, John";
tWork.entGender = 'M';
tWork.entAge = 89;
bci.insert(BCI::value_type(47, tWork));
[etc.]

However, in subsequent processing I have to _change_ some of
this data (e.g. modify the entAge value). For example:

bIter = bci.find(47);
if(bIter != bci.end()) // DBE record exists in base file
{
tData &qWork = bIter->second;
qWork.entAge = 35;
// What do I do here to update the map object I've accessed?
}
 
A

Alf P. Steinbach

* Mike Copeland:
I have the following definition and declaration:

typedef struct ChipRecord // Chip Times record
{
int bibNum; // Bib #
short entAge; // Age
char entGender; // Gender (M/F)
char entRCode; // RCode
char entECode; // Entrant Type code
string entName; // Entrant Name
} tData; // entrant info records
tData tWork;
typedef map<int, ChipRecord> BCI;
BCI bci;
map<int, ChipRecord>::iterator bIter;

I'm reading a data file and storing objects of this type:

tWork.entName = "Doe, John";
tWork.entGender = 'M';
tWork.entAge = 89;
bci.insert(BCI::value_type(47, tWork));
[etc.]

However, in subsequent processing I have to _change_ some of
this data (e.g. modify the entAge value). For example:

bIter = bci.find(47);
if(bIter != bci.end()) // DBE record exists in base file
{
tData &qWork = bIter->second;
qWork.entAge = 35;
// What do I do here to update the map object I've accessed?

You've already done that. :)

That's why you (?) used a reference to declare 'qWork'.

<code>
#include <iostream>
#include <string>
#include <map>

struct Entrant
{
short age; // Age
char gender; // Gender (M/F)
char rCode; // RCode
char eCode; // Entrant Type code
std::string name; // Entrant Name
};

struct ChipRecord // Chip Times record
{
int bibNum; // Bib #
Entrant entrant;
};

int main()
{
using namespace std;
typedef map<int, ChipRecord> ChipRecords;

ChipRecords records;
ChipRecord& r = records[47];

r.entrant.name = "Doe, John";
r.entrant.gender = 'M';
r.entrant.age = 89;

ChipRecords::iterator const bIter = records.find(47);
if(bIter != records.end()) // DBE record exists in base file
{
ChipRecord& rFound = bIter->second;
rFound.entrant.age = 35;
// You have already updated the map object that you've accessed.
}
cout << records[47].entrant.age << endl;
}
</code>

In addition to the C-ism and naming issues illustrated above, do consider
defining some constructors and so on. Also e.g. 'gender' should probably be an
enum type, and the 'rCode' and 'eCode' look very much like bug attractors.
Simply, with C++ you have the tools to abstract such things, and then your
application becomes so slick that the bugs find no traction and never get in.


Cheers & hth.,

- Alf
 
M

Mike Copeland

tWork.entName = "Doe, John";
tWork.entGender = 'M';
tWork.entAge = 89;
bci.insert(BCI::value_type(47, tWork));
[etc.]

However, in subsequent processing I have to _change_ some of
this data (e.g. modify the entAge value). For example:

bIter = bci.find(47);
if(bIter != bci.end()) // DBE record exists in base file
{
tData &qWork = bIter->second;
qWork.entAge = 35;
// What do I do here to update the map object I've accessed?
}

Nothing. You just did.

Hmmmm, it doesn't appear to work. Later in the program I "dump" the
map data to an output file, but nothing's changed from the original
values. That's why I posted the query, because although I can (and do)
add new objects that are there, none of the "updates" are reflected in
the data that's dumped. Strange... 8<{{
 
B

Bart van Ingen Schenau

Mike said:
tWork.entName = "Doe, John";
tWork.entGender = 'M';
tWork.entAge = 89;
bci.insert(BCI::value_type(47, tWork));
[etc.]

However, in subsequent processing I have to _change_ some of
this data (e.g. modify the entAge value). For example:

bIter = bci.find(47);
if(bIter != bci.end()) // DBE record exists in base file
{
tData &qWork = bIter->second;
qWork.entAge = 35;
// What do I do here to update the map object I've
accessed?
}

Nothing. You just did.

Hmmmm, it doesn't appear to work. Later in the program I "dump"
the
map data to an output file, but nothing's changed from the original
values. That's why I posted the query, because although I can (and
do) add new objects that are there, none of the "updates" are
reflected in
the data that's dumped. Strange... 8<{{

Are you sure you are not accidentally making copies of either the map or
the data entries you change?

Please post a complete example program that shows the problem you have,
so that we could debug/examine it. Please copy/paste the program (DO NOT
RETYPE).

Bart v Ingen Schenau
 
M

mzdude

   Hmmmm, it doesn't appear to work.  Later in the program I "dump"   the
map data to an output file, but nothing's changed from the original
values.  That's why I posted the query, because although I can (and do)
add new objects that are there, none of the "updates" are reflected in
the data that's dumped.  Strange... 8<{{- Hide quoted text -

Did you pass the map to an update function and forget to pass
it by reference?

void UpdateMap( ChipRecords chipMap ) { ... } // Updates a copy of
the map

void UpdateMap( ChipRecords &chipMap } { ... } // Update the map
itself
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top