Re: Changing STL map Object Data
* 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
--
Due to hosting requirements I need visits to <url: http://alfps.izfree.com/>.
No ads, and there is some C++ stuff! :-) Just going there is good. Linking
to it is even better! Thanks in advance!