开发者

which STL container to use for inter-related data?

开发者 https://www.devze.com 2023-03-14 09:08 出处:网络
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\

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 tha开发者_如何学运维t 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.


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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号