使用哪个 STL 容器来存储相互关联的数据?

发布于 2024-11-16 11:05:22 字数 245 浏览 2 评论 0原文

首先,这是一个游戏项目。

我需要有名为 Skill 的对象,其中包含其字符串名称、一些其他成员,以及一个由一组称为“需求”的其他 Skill 对象组成的成员。 (这将是给定技能所需的先决技能列表)

我应该将一组所有技能对象放入哪种 STL 容器中?向量?放?地图?
这个容器是否也可以用作成员“需求”的类型?

技能必须是独一无二的。
至于我将对技能集做什么 - 主要是按名称搜索并组合技能集,并将技能附加到该集。

First of all, this is a game project.

I need to have objects called Skill, that contain their string name, some other members, and a member that is a set of other Skill objects called "requirements". (This will be a list of prerequisite Skills that the given Skill requires)

In what sort of STL container should I put a set of all Skill objects? vector? set? map?
Is this container also to be used as the type of the member "requirements"?

Skills need to be unique.
As for what I'll be doing to the set of Skills - searching by name, mostly and combining sets of Skills, and appending Skills to the set.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

余生共白头 2024-11-23 11:05:22

您不是通过容器需要包含什么来定义容器需求,而是通过哪些操作将是常见的以及它们需要运行的速度来定义它。

某个地方有一个很棒的图表,有点像流程图,可以指导您选择容器。如果我找到它,我会更新这个答案。

编辑:这是:在什么情况下我需要使用特定的STL容器?

You don't define container requirements by what they need to contain, you define it by what operations will be common and how fast they need to operate.

Somewhere there's a wonderful diagram, kind of like a flow chart, that guides you through selecting a container. If I find it I'll update this answer.

Edit: Here it is: In which scenario do I use a particular STL container?

太阳男子 2024-11-23 11:05:22

技能必须是独一无二的。

第一印象认为您应该使用 mapset。但这降低了在集合中“搜索”的灵活性。我会简单地从向量开始,将该向量放入某个类中。该类将具有 AppendSkill 并检查给定的 Skill 是否已存在。如果不追加,则返回 false/失败。

同一类别应该有助于组合和附加技能/技能集。

Skills need to be unique.

First impression argues that you should use map or set. But this reduces flexibility to "search" in the collection. I would have simply started with vector, put that vector in some class. That class would have AppendSkill and would check if given Skill already exits. If not append, or return false/failure.

The same class should facilitate combining and appending skills/skill-set.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文