# Thread: Storing Many Strings- Which STL Container?

1. ## Storing Many Strings- Which STL Container?

Hi,

Say you're writing a game that needs to store a load of pre-written text comments, which can be at most a couple of sentences long. There are around 5 different types of comment, such as insult/ compliment, and each of these types has approximately 5 sub-types- such as 'angry insult', 'very angry insult' etc. There are around 5 unique pre-written lines for each sub-type. So ultimately that's approximately 125 lines total.

What would be the best way to store these lines? I'm thinking I could have them each as a string, and then store them in a vector. I don't want the container to sort them out like a map would, because I want to be able to access them by their number.

The only thing I'm wondering though is because there are different categories, would something else be better than a vector? Or could you do a vector of vectors with compliment/ insult etc. being indexed by a different number or something?

Alternately I'm thinking about just having one vector of strings, and then having some define values for where each category starts e.g. if they are 30 insults, then 22 compliments, and 27 chat lines:

Code:
```#define INSULTS_ST 0
#define COMP_ST 30
#define CHAT_ST 52```
So which approach do you think would be the best?

Thanks.

2. vector<vector<string>>
vector[0] is a vector of strings containing all insults
vector[1] is a vector of strings containing all compliments
etc

3. I'd rather use const T rather than #define for constants, however, for obvious reasons (feel free to ask).

4. Just use 5 vectors. Or even 5 arrays if the number of comments is not expected to change.

5. Just go with 5 vectors. If it were a big game, I'd probably use 5 titled resource objects each with their own list. If you were to use one vector of strings, why would you need constants? When you load the strings, you know how many there are of each. You can set int insult_st = insult line count, comp_st = insult_st + comp line count, and so forth after loading each category. You don't need the counts predetermined unless you are storing them together with no separation. If that were the case, then sure, use const variables and go with arrays, instead of vectors.

6. If you need a lot of strings you can always hash the strings to create an index into a hash table.

7. I'd make a struct containing the comments, the sub-type and whatever else comes along in future, and then make a vector of those.

However it depends on whether you'd ever want to look them up by anything other than their index.

8. Originally Posted by Bubba
If you need a lot of strings you can always hash the strings to create an index into a hash table.
And then, when you want to "get the first compliment", you will have to get it from somewhere else, hash it, and use it to read the same string back from the hash table?