Storing Many Strings- Which STL Container?

This is a discussion on Storing Many Strings- Which STL Container? within the C++ Programming forums, part of the General Programming Boards category; Hi, Say you're writing a game that needs to store a load of pre-written text comments, which can be at ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    127

    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. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,335
    vector<vector<string>>
    vector[0] is a vector of strings containing all insults
    vector[1] is a vector of strings containing all compliments
    etc
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,170
    I'd rather use const T rather than #define for constants, however, for obvious reasons (feel free to ask).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,011
    Just use 5 vectors. Or even 5 arrays if the number of comments is not expected to change.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    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.
    Last edited by Dae; 08-25-2009 at 05:57 PM.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  6. #6
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,584
    If you need a lot of strings you can always hash the strings to create an index into a hash table.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,261
    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.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,145
    Quote 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?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing strings in a linked list
    By dws90 in forum C Programming
    Replies: 1
    Last Post: 02-21-2009, 06:06 PM
  2. swap function of STL container
    By George2 in forum C++ Programming
    Replies: 11
    Last Post: 03-29-2008, 02:53 AM
  3. help with STL container remove_if on a class
    By Syneris in forum C++ Programming
    Replies: 19
    Last Post: 01-31-2006, 12:56 AM
  4. Problems with strings as key in STL maps
    By all_names_taken in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 10:34 AM
  5. Storing long strings?
    By thebudbottle in forum C++ Programming
    Replies: 1
    Last Post: 03-09-2005, 05:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21