paired map question

R

repairman2003

I'm trying to keep track of two objects, a vector of strings and a user
defined date, ints set up in a struct. Having some trouble with it
keeping track of the information though.

//Defined as private data memebers of a class
vector<string> serviceRecord;
map<Date,vector<string> > serviceMap;

//In a constructor of a class
serviceMap.insert(make_pair(date1,serviceRecord));

//Function that adds a new service to the map
void addService(Date newDate,string newService)
{
serviceRecord.push_back(newService);
serviceMap.insert(make_pair(newDate,serviceRecord));
}



//In the main program that should output all of the service records for
each date

Date tempDate;
vector<string>::iterator sIter=services.begin();
std::map<Date,vector<string> >::iterator mIter;
std::map<Date,vector<string> >::iterator mIter2;
mIter=map.begin();


while(mIter!=map.end())
{
mIter2=map.find(mIter->first);
sIter=mIter->second.begin();
cout<<"\nOn "<<mIter->first.day<<"/"<<mIter->first.month<<"/"
<<mIter->first.year<<": ";
while(sIter!=mIter2->second.end())
{
cout<<"\n"<<*sIter;
sIter++;
}
mIter++;
}

First a record is created using the constructor, should have this as
the values:

date.day=26
date.month=10
date.year=2005
service="Client recieved a new service"

Then the record is updated using the addService function and should
then have these values:

date.day=26
date.month=10
date.year=2005
service="Client recieved a new service"

date.day=27
date.month=10
date.year=2005
service="Client recieved a new service today"

When the records are outputed with that while loop, it outputs this
instead:

On 10/26/2005:
Client recieved a new service

On 10/27/2005:
Client recieved a new service
Client recieved a new service today

It should only output the second statement, not both.

What should I change in my program to make it work correctly?

Thanks
 
T

TIT

(e-mail address removed) sade:
I'm trying to keep track of two objects, a vector of strings and a user
defined date, ints set up in a struct. Having some trouble with it
keeping track of the information though.

//Defined as private data memebers of a class
vector<string> serviceRecord;
map<Date,vector<string> > serviceMap;

//In a constructor of a class
serviceMap.insert(make_pair(date1,serviceRecord));

//Function that adds a new service to the map
void addService(Date newDate,string newService)
{
serviceRecord.push_back(newService);

You push back the newService string into serviceRecord, and then
serviceMap.insert(make_pair(newDate,serviceRecord));

you copy the entire vector into serviceMap, and...

Then the record is updated using the addService function and should
then have these values:

date.day=26
date.month=10
date.year=2005
service="Client recieved a new service"

date.day=27
date.month=10
date.year=2005
service="Client recieved a new service today"

....when you call addService the second time, you push back yet another
string into serviceRecord, which now contains 2 (TWO) strings, and you
copy it into serviceMap, thus...
When the records are outputed with that while loop, it outputs this
instead:

On 10/26/2005:
Client recieved a new service

On 10/27/2005:
Client recieved a new service
Client recieved a new service today

....getting this output.
It should only output the second statement, not both.

What should I change in my program to make it work correctly?

Perhaps you should change:

map<Date,vector<string> > serviceMap;

to

map<Date,string> serviceMap;

and abolish the serviceRecord detour.

TIT
 
G

Greg

TIT said:
(e-mail address removed) sade:

You push back the newService string into serviceRecord, and then


you copy the entire vector into serviceMap, and...



...when you call addService the second time, you push back yet another
string into serviceRecord, which now contains 2 (TWO) strings, and you
copy it into serviceMap, thus...


...getting this output.


Perhaps you should change:

map<Date,vector<string> > serviceMap;

to

map<Date,string> serviceMap;

and abolish the serviceRecord detour.

A std::map<Date, std::string> would only allow only one service record
per Date. The vector of strings is one way of associating multiple
service records with one date. Using a multimap would be another
solution.

A std::multimap<Date, std:string> would allow multiple entries with the
same date and also eliminate the vector of strings. The program could
then use a lower_bound and upper_bound (or equal_range) to enumerate
all the service records, if any, for a particular date.

Greg
 

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

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top