2

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.

vedran
  • 751
  • 1
  • 5
  • 16

2 Answers2

9

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?

Community
  • 1
  • 1
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • 1
    Is this what you are looking for? http://stackoverflow.com/questions/471432/in-which-scenario-do-i-use-a-particular-stl-container – GWW Jun 20 '11 at 18:43
  • @GWW, yes that was it, I found it myself within a minute of posting the answer. Thanks though. – Mark Ransom Jun 20 '11 at 18:44
  • No problem, I must have commented while you were editing your answer. IT's a neat chart :) – GWW Jun 20 '11 at 18:45
1

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.

Ajay
  • 18,086
  • 12
  • 59
  • 105
  • You just helped me a lot with this idea of a class that gets all this done! Thank you! And, yes, it turns out I'll be using vector after all. Hope it's not too slow. – vedran Jun 20 '11 at 19:00
  • 1
    vector isn't slow. But it depends how you would "search" for specific data elements, and how many elements would be in vector. Since you do have multiple criteria to search-on, you may use a map to hold those strings and the "value" part would be iterator/index of vector. But again, if the search-string is exact search string. If partial string comes in, you better just search in vector. Hope you are getting my point. – Ajay Jun 20 '11 at 19:07
  • There will be about a thousand objects in the vector. I think there will be no searching for partial strings. So, you mean have a separate map of all the strings I'll be searching for, with the value being the vector index where the corresponding Skill is located in the vector? Then just find my wanted Skill by string key in map. Did I understand well? – vedran Jun 20 '11 at 20:42
  • 1
    Provided that string search is case sensitive, otherwise you have have to write-up some comparator function for that too. Also ensure those strings are unique, otherwise consider using multimap. – Ajay Jun 21 '11 at 02:57