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

structures and vector

P: n/a
Considering this struct:

typedef struct ServiceRecord
{
vector<string> service;
vector<Date> date;

} ServiceRecord;

Date is declared elsewhere as:

typedef struct Date
{
int month;
int day;
int year;

} Date;

And this declaration in a class:
ServiceRecord serviceRecord;
How would I go about pushing values onto one of the vectors?
like 10/10/2005 as the first date on the date vector.

serviceRecord.date.push_back( ??? )

Thanks

Oct 24 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
re***********@gmail.com wrote:
Considering this struct:

typedef struct ServiceRecord
{
vector<string> service;
vector<Date> date;

} ServiceRecord;
Don't do that. Do

struct ServiceRecord
{
vector<string> service;
vector<Date> date;
};

Date is declared elsewhere as:

typedef struct Date
{
int month;
int day;
int year;

} Date;
Unless you need to be compatible with C, don't do that either.
And this declaration in a class:
ServiceRecord serviceRecord;
How would I go about pushing values onto one of the vectors?
like 10/10/2005 as the first date on the date vector.

serviceRecord.date.push_back( ??? )


Date d = { 10, 10, 2005 };
serviceRecord.date.push_back(d);

Since 'Date' struct doesn't have a [parameterized] constructor, you cannot
write a single expression that would take "10/10/2005" or three numbers,
and convert it into an instance of 'Date'. You can write a function that
would do that:

Date createDate(int a, int b, int c} {
Date d = { a, b, c };
return d;
}

but it would probably be better to define a constructor or two in 'Date'
struct.

V
Oct 24 '05 #2

P: n/a
> re***********@gmail.com <re***********@gmail.com> wrote:
Considering this struct:

typedef struct ServiceRecord
{
vector<string> service;
vector<Date> date;

} ServiceRecord;
In C++, you do not need to use a typedef for structs. Change it to:

struct ServiceRecord
{
vector<string> service;
vector<Date> date;
};
Date is declared elsewhere as:

typedef struct Date
{
int month;
int day;
int year;

} Date;
See above for comment about typedef. Also, consider adding a
constructor for Date:

struct Date
{
int year;
int month;
int day;

Date(int year_, int month_, int day_)
: year(year_)
, month(month_)
, day(day_)
{ }
};
And this declaration in a class:
ServiceRecord serviceRecord;
How would I go about pushing values onto one of the vectors?
like 10/10/2005 as the first date on the date vector.

serviceRecord.date.push_back( ??? )


If you define a constructor as I did above, you should be able to do:

serviceRecord.date.push_back(Date(2005, 10, 10));

--
Marcus Kwok
Oct 24 '05 #3

P: n/a
Thanks guys! That helped. Just couldn't remember how to do it.

Oct 24 '05 #4

P: n/a
re***********@gmail.com wrote:
Considering this struct:

typedef struct ServiceRecord
{
vector<string> service;
vector<Date> date;

} ServiceRecord;

Date is declared elsewhere as:

typedef struct Date
{
int month;
int day;
int year;

} Date;

And this declaration in a class:
ServiceRecord serviceRecord;
How would I go about pushing values onto one of the vectors?
like 10/10/2005 as the first date on the date vector.

serviceRecord.date.push_back( ??? )


Are you sure you dont't want a vector of Service Records? Something
like:

struct ServiceRecord
{
Date date;
std::string service;
}

std::vector<ServiceRecord> serviceRecords;

It just strikes me as odd to have two vectors internal to a struct.
What is the relationship between the elements in each vector? And how
is that relationship kept consistent?

Greg

Oct 24 '05 #5

P: n/a
re***********@gmail.com wrote:
Considering this struct:

typedef struct ServiceRecord
{
vector<string> service;
vector<Date> date;

} ServiceRecord;

Date is declared elsewhere as:

typedef struct Date
{
int month;
int day;
int year;

} Date;

And this declaration in a class:
ServiceRecord serviceRecord;
How would I go about pushing values onto one of the vectors?
like 10/10/2005 as the first date on the date vector.

serviceRecord.date.push_back( ??? )


Are you sure you dont't want a vector of Service Records? Something
like:

struct ServiceRecord
{
Date date;
std::string service;
};

std::vector<ServiceRecord> serviceRecords;

It just strikes me as odd to have two vectors internal to a struct.
What is the relationship between the elements in each vector? And how
is that relationship kept consistent?

Greg

Oct 24 '05 #6

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

Oct 24 '05 #7

P: n/a
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));
}
Jonathan

Oct 24 '05 #8

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

Oct 25 '05 #9

P: n/a
I got rid of the service struct and have the data memembers private in
a class. When doing this:

m.insert(std::make_pair(date,service));

(service is a vector<string>)

I get a bunch of could not deduce template errors, if service is first
instead of date it doesn't give those errors.

Oct 25 '05 #10

P: n/a
I chose to use the map STL here. But I'm getting a problem with it
pairing up right.

This is an output loop in the program:

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

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

Things are paired up in two places, constructor of a class, and an
accessor function in that class:

serviceMap.insert(make_pair(date1,serviceRecord));

I tried a second iterator and set it to mIter2=map.find(mIter->first)
inside of the while loop, which I think should point to all
serviceRecords on that given date. What is my misinterpretation?

Oct 25 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.