473,396 Members | 1,777 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

structures and vector

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
10 1658
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
> 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
Thanks guys! That helped. Just couldn't remember how to do it.

Oct 24 '05 #4
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Peter Olcott | last post by:
I want to be able to efficiently build data structures at run-time. These data structures need to be accessed with minimal time. The only a few ways that come immediately to mind would be some...
0
by: Jason | last post by:
Could someone offer some insight on this: I have started implementing a Trie, somewhere along the lines of the Aho Corasick algorithm (but somewhat different). The structures I would like to...
2
by: wtnt | last post by:
Hello, I've been using the STL libraries for some time, but still don't know the ins and outs of its implementation. Could this be because there's more than 1 implementation? Does anyone know of...
8
by: michi | last post by:
Hello everybody, I have following problem: I have an array of pointers to structures: table* tab = new table; and structure table is like this: struct table{ CSLL::node* chain;
3
by: koperenkogel | last post by:
Dear cpp-ians, I have a vector of structures: struct meta_group { float id; float num; float mean; float sum;
2
by: steflhermitte | last post by:
Dear cpp-ians, I have two structures: struct metaSegment { vector <long double> mean; bool full; };
8
by: Generic Usenet Account | last post by:
I have a need for a set operation (actually multi-set operation) on sorted structures that is not in the STL library. I call it the set_retain operation. It's kinda similar to the...
2
by: jeff_j_dunlap | last post by:
I recently began using STL containers, mainly to store pointers to structures using a vector: std::vector <dataStruc*vdata; Next, I fill the container within a loop: vdata.push_back(new...
10
by: barabenka | last post by:
How can I use find_if with vector of data structures? I have something like this: struct Record{ int ID; int Data1; int Data2; };
13
by: Cell | last post by:
I have structures like this in my program - typedef vector vectorstruct { double x, y,z; } vector; typedef struct verticesstruct { vector v;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.