使用哪个 STL 容器来存储相互关联的数据?
首先,这是一个游戏项目。
我需要有名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不是通过容器需要包含什么来定义容器需求,而是通过哪些操作将是常见的以及它们需要运行的速度来定义它。
某个地方有一个很棒的图表,有点像流程图,可以指导您选择容器。如果我找到它,我会更新这个答案。
编辑:这是:在什么情况下我需要使用特定的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?
第一印象认为您应该使用
map
或set
。但这降低了在集合中“搜索”的灵活性。我会简单地从向量
开始,将该向量放入某个类中。该类将具有AppendSkill
并检查给定的Skill
是否已存在。如果不追加,则返回 false/失败。同一类别应该有助于组合和附加技能/技能集。
First impression argues that you should use
map
orset
. But this reduces flexibility to "search" in the collection. I would have simply started withvector
, put that vector in some class. That class would haveAppendSkill
and would check if givenSkill
already exits. If not append, or return false/failure.The same class should facilitate combining and appending skills/skill-set.