What to use: array-vector-list

This is a discussion on What to use: array-vector-list within the C++ Programming forums, part of the General Programming Boards category; I have a series of 12 strings of under 25 characters each. I have 1 two-digit number for each string. ...

  1. #1
    Registered User QuestionKing's Avatar
    Join Date
    Jan 2009
    Posts
    68

    What to use: array-vector-list

    I have a series of 12 strings of under 25 characters each.
    I have 1 two-digit number for each string.
    I would like to be able to access each element with an int (index #) and associate the corresponding two-digit number with the string.
    As an example:
    Code:
    std::string FRUITS[] = {"Fruit1", "Fruit2", "Fruit3", "Fruit4"}; // -through 12...
    int QTY[] = {02,09,12,24}; // -one per string
    //...so I have 12 of Fruit3 in the example.
    I also want to be able to access this 'storage' with a function, which will also access other 'storage' with the same design, but possibly a different number of entries. A loop will be run per entry, which is why I need the total (unknown) number of entries.
    Basically I stumbled upon this question trying to learn something else, and do not want to spend the next week learning something, only to find out there is a better way to approach what I am doing.
    I guess I would just like to be pointed in the most beneficial direction,
    arrays
    multidimensional array
    vector
    list
    other?
    Also, for curiosity, how would this answer differ if I needed the ability to add elements on the fly? (perhaps a project to play with after this)
    Asking a question you already know the answer to might teach you something you did not know...

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,440
    It sounds like you want a std::vector<std::pair<std::string, int> >, or maybe to define a Fruit class so you can have a std::vector<Fruit>. Or maybe you do not actually need a sequence to be accessed by index, and thus a std::map<std::string, int> is appropriate.

    It may be easier if you would tell us what you are trying to model instead of speaking cryptically though generally.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User QuestionKing's Avatar
    Join Date
    Jan 2009
    Posts
    68
    Thank you, it seems like class is what I will need. Basically I am trying to learn C++ without school (yet). I do not actually have a set of instructions to follow, or a desired result. Though sometimes when people post their homework instructions I copy those down and try to write it.
    I have had a hard time coming up with something to aim for so this is just a little program to manipulate data. So far it is (in my mind) at stage 0, having a need for a program.
    lol...
    So what I come up with is an inventory type program, like for a supermarket. (I have done more thinking about what i 'need') I guess I want to aim for something to accept inventory input, and display output about the inventory. I am learning classes at this point and that sounded like a good topic to organize... food
    I have 0 code yet, just coming up with ideas of what to do that can include things to learn.
    I will look into map like you suggested, it looks very useful.
    TY again and I am sure I will need another push at some point along the way.
    Asking a question you already know the answer to might teach you something you did not know...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multidimensional Array Addressing
    By BlackOps in forum C Programming
    Replies: 11
    Last Post: 07-21-2009, 09:26 PM
  2. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

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