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

paired map question

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

Oct 26 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
TIT
re***********@gmail.com 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...

<snip>
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
Oct 26 '05 #2

P: n/a
TIT wrote:
re***********@gmail.com 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...

<snip>
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.


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

Oct 27 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.