Thread: What to use: array-vector-list

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

    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:
    std::string FRUITS[] = {"Fruit1", "Fruit2", "Fruit3", "Fruit4"}; // -through 12...
    int QTY[] = {02,09,12,24}; // -one per string
    // 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,
    multidimensional array
    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
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    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.
    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