结构和矢量 [英] structures and vector

查看:73
本文介绍了结构和矢量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑这个结构:


typedef struct ServiceRecord

{

vector< string>服务;

vector< Date>日期;


} ServiceRecord;


日期在别处声明为:


typedef struct Date

{

int month;

int day;

int year;


}日期;


这个声明在课堂上:

ServiceRecord serviceRecord;

我该怎么办?将值推到其中一个向量上?

像2005年10月10日那样是日期向量的第一个日期。


serviceRecord.date.push_back(? ??)


谢谢

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

推荐答案

re *********** @ gmail.com 写道:
考虑这个结构:

typedef struct ServiceRecord
{
vector< string>服务;
向量<日期>日期;

} ServiceRecord;


别这么做。请


struct ServiceRecord

{

vector< string>服务;

vector< Date>日期;

};

日期在别处声明为:

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

}日期;


除非你需要与C兼容,否则也不要这样做。

这个声明在课堂上:
ServiceRecord serviceRecord ;

我如何将值推到其中一个向量上?
像2005年10月10日那样是日期向量的第一个日期。

serviceRecord .date.push_back(???)
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( ??? )




日期d = {10,10,2005};

serviceRecord.date.push_back( d);


由于''Date''struct没有[参数化]构造函数,你不能

写一个表达式" 10 /2005分之10"或三个数字,

并将其转换为''Date'的实例。你可以编写一个

会这样做的函数:

日期createDate(int a,int b,int c} {

日期d = {a,b,c};

返回d;

}


但它可能会更好在''Date'中定义一个或两个构造函数'

struct。


V



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


> re***********@gmail.com < re ***********@gmail.com>写道:
> re***********@gmail.com <re***********@gmail.com> wrote:
考虑这个结构:

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

} ServiceRecord;


在C ++中,你不需要使用typedef对于结构。将其更改为:


struct ServiceRecord

{

vector< string> service;

vector< Date> date;

};

日期在别处声明为:

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

}日期;


有关typedef的评论,请参见上文。另外,考虑为Date添加一个

构造函数:


struct Date

{

int year ;

int month;

int int;


日期(int year_,int month_,int day_)

:年(年_)

,月(月_)

,日(day_)

{}

};

这个类中的声明:
ServiceRecord serviceRecord;

如何将值推送到其中一个向量上?
喜欢10/10/2005作为日期向量的第一个日期。

serviceRecord.date.push_back(???)
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( ??? )




如果你像我上面那样定义构造函数,你应该可以这样做:


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


-

Marcus Kwok



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


谢谢大家!这有帮助。只是不记得怎么做。

Thanks guys! That helped. Just couldn''t remember how to do it.


这篇关于结构和矢量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆