C ++中使用结构的数组类,不知道如何大数组,我需要 [英] C++ a class with an array of structs, without knowing how large an array I need

查看:129
本文介绍了C ++中使用结构的数组类,不知道如何大数组,我需要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类的字段的姓名,年龄,学校等,我需要能够存储例如像,他们已经走过的其他信息,这是在哪一年。我不能宣布另一个类专门召开旅行社和哪一年,所以我认为一个结构可能是最好的。这只是一个例子:

I have a class with fields like firstname, age, school etc. I need to be able to store other information like for instance, where they have travelled, and what year it was in. I cannot declare another class specifically to hold travelDestination and what year, so I think a struct might be best. This is just an example:

struct travel {
    string travelDest;
    string year;
};

问题是人都可能有不同的旅行数额。我想只是有出行结构的数组来保存数据。但我怎么创建一个固定大小的数组来保存它们,不知道我有多大需要它?

The issue is people are likely to have travelled different amounts. I was thinking of just having an array of travel structs to hold the data. But how do I create a fixed sized array to hold them, without knowing how big I need it to be?

也许我会对此完全错误的方式,因此任何建议,更好的办法是AP preciated。

Perhaps I am going about this the completely wrong way, so any suggestions as to a better way would be appreciated.

我知道有本质上是一个类和结构没有什么区别,但对于分配标准的目的,我不允许一个阶级,所以是的。

I realise there is essentially no difference between a class and struct, but for the purposes of assignment criteria I am not allowed a "class", so yeah.

推荐答案

您可以尝试一个关联的std ::与每个人的矢量,用含有一个结构向量的每个条目:

You could try associating a std::vector with each person, with each entry in the vector containing a struct:

typedef struct travel {
    string travelDest;
    string year;
} travelRecord;

std::vector<travelRecord> travelInfo;

您可以再添加项目到矢量您认为合适的:

You can then add items to the vector as you see fit:

travelRecord newRecord1 = {"Jamaica", "2010"};
travelInfo.push_back(newRecord1);

travelRecord newRecord2 = {"New York", "2011"};
travelInfo.push_back(newRecord2);

关于向量运算的一些详细信息,可以发现这里

这篇关于C ++中使用结构的数组类,不知道如何大数组,我需要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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