Jonathan Mcdougall wrote:
re***********@gmail.com wrote: What my program is doing is keeping track of customer information. And
one of the things to be kept track of is a service record which
includes a date. The problem is there can be more than one service per
each date. I wrote it first as only one service on a given date and
then converting it to be able to have multiple services on a particular
date. A vector was my first thought and am seeing that it isn't
working out. Any ideas of how to keep track of this?
That certainly looks like a map:
# include <vector>
# include <map>
class service
{
// ..
};
class date
{
// ..
};
int main()
{
typedef std::vector<service> services;
typedef std::map<date, services> my_map;
my_map m;
services s;
s.push_back(service(..));
s.push_back(service(..));
m.insert(std::make_pair(date(25, 12, 2005), s));
}
The map is certainly a good suggestion. It expresses the "one-to-many"
relationship between a date and a series of service records for that
date.
Along the same lines, it would be possible to declare a single struct
that combined one date with one or more service records:
struct ServiceRecord
{
Date date;
std::vector<std::string> services;
};
It would then be possible to store this struct in a vector, or even in
a std::set, provided that the less than operator were defined for
ServiceRecords. To do so, I would first define the less than operator
for a Date:
bool operator<(const Date& lhs, const Date& rhs)
{
return lhs.year != rhs.year ? lhs.year < rhs.year :
lhs.month != rhs.month ? lhs.month < rhs.month :
lhs.day < rhs.day;
}
bool operator<(const ServiceRecord& lhs, const ServiceRecord& rhs)
{
return lhs.date < rhs.date;
}
These two functions would then allow ServiceRecord's to be stored in a
std::set<ServiceRecord> which would be useful if you wished to the keep
the ServiceRecords sorted, for example.
Greg