Thread: Choosing the right container for the job

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    2

    Choosing the right container for the job

    I've only worked with arrays and list at this point so for this project I've been using list but I've been wondering if there's a better way. I have 2 sets of data that need to be stored, in both sets the data is a class I have written.

    For the first set has an unknown number of elements but will be several thousand. After the list is filled no elements will be modified. The elements will need to be sorted by a date stamp which is a member of the class. Almost all access to the elements will be done in sequential order. The exception to this is in rare instances the set will need be searched forward for the next 5 instances of certain value. Processing will then continue from the element that triggered the search.

    The second set again has an unknown number of elements but will be ~70. The set will be searched for an element, have a value read from that element, then have the search repeated for a new key. Some math is done on the two read values which modifies them. The changes made to the values must be updated to the elements in the set. (At the moment I'm thinking I'll achieve this by leaving a pointer to the value after the element has been searched for.)

    To sum up my question: What is the best container to use for storing and processing these two sets of data?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Your first "set" would be most simply done with a std::vector.

    Your second requirement is a bit ambiguous, since it is not clear what you mean by searching for a "new key", or what you mean by "the changes made to the values must be updated to elements in the set". However assuming your description means you intend to regularly add elements to the middle of the set, a std::list might work. If you are regularly doing a search based on a key, look up std::set (an associative set).


    Have a look sometime at the required properties of the various containers, to better understand my response. Bear in mind that there is rarely a "best" container - most containers have some set of advantages for a task at hand,and disadvantages. A std::vector is sometimes considered a default choice since it is somewhat easier for mere mortal programmers to use, and has fairly good performance characteristics in a lot of common use cases.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Sep 2013
    Posts
    2
    Sorry if I was unclear with the 2nd set. A better way of putting it might be:

    a class has two members x and y
    search for an x, read y
    search for a new x, read y
    do some math with both y's
    update both y members of the elements they came from to new values

    Neither set would have members added to the middle. Both sets have all elements added at once, sorted, then processed.

    What started my wondering if there was a better way was reading that searching list can be slow since you must iterate through the whole list. I suppose that wouldn't matter for the first list since if searching it I specifically want the next 5 instances of my search term. But for the second set which I will be searching often on I thought a list might not be the best.

    Is there a better word to use than 'set' for what I'm trying to describe?

    Based on what you've said it seems a list would be appropriate for the first set but I'm still a little unsure on the second set since I need to search for and access specific elements often.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Choosing the right container
    By manasij7479 in forum C++ Programming
    Replies: 6
    Last Post: 04-11-2011, 03:59 AM
  2. Choosing a GPS
    By laserlight in forum General Discussions
    Replies: 14
    Last Post: 11-20-2009, 11:12 AM
  3. Replies: 1
    Last Post: 01-23-2006, 07:12 PM
  4. Choosing motherboard
    By Micko in forum Tech Board
    Replies: 8
    Last Post: 10-07-2005, 02:32 PM
  5. Replies: 4
    Last Post: 03-21-2004, 03:34 PM