Container to map a vector of structures

Discussion in 'C++' started by jeff_j_dunlap@yahoo.com, Apr 26, 2007.

  1. Guest

    I recently began using STL containers, mainly to store pointers to
    structures using a vector:
    std::vector <dataStruc*> vdata;

    Next, I fill the container within a loop:
    vdata.push_back(new dataStruc(*it, param1, param2, param3);

    Finally, traverse the vector within a loop (starting at the first
    element and ending at the last) to create a report, thus, no random
    access is required.


    Now I have a new requirement and I need random access to each
    structure contained in the vector. There will be thousands of
    structures contained by my vector and I will be accessing structures
    randomly multiple times--for instance, I may need to access any given
    structure 10 or 20 times as I generate data for my report.

    The easiest way I concieve this is to build a parallel vector that
    stores the key string, such as:
    std::vector <string> vKey;

    Then I can 'seek' the string, obtain it's position, and using that
    position, efficiently obtain the structure stored in the structure.

    Would it be better to accomplish this with a different container, such
    as a map or something else?

    Thanks in advance
    , Apr 26, 2007
    #1
    1. Advertising

  2. wrote:
    > I recently began using STL containers, mainly to store pointers to
    > structures using a vector:
    > std::vector <dataStruc*> vdata;
    >
    > Next, I fill the container within a loop:
    > vdata.push_back(new dataStruc(*it, param1, param2, param3);
    >
    > Finally, traverse the vector within a loop (starting at the first
    > element and ending at the last) to create a report, thus, no random
    > access is required.
    >
    >
    > Now I have a new requirement and I need random access to each
    > structure contained in the vector. There will be thousands of
    > structures contained by my vector and I will be accessing structures
    > randomly multiple times--for instance, I may need to access any given
    > structure 10 or 20 times as I generate data for my report.
    >
    > The easiest way I concieve this is to build a parallel vector that
    > stores the key string, such as:
    > std::vector <string> vKey;
    >
    > Then I can 'seek' the string, obtain it's position, and using that
    > position, efficiently obtain the structure stored in the structure.
    >
    > Would it be better to accomplish this with a different container, such
    > as a map or something else?


    Probably. And if you have several such mappings, you can have several
    maps, each setting the association between a key and the index of the
    item in, say, a vector.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Apr 26, 2007
    #2
    1. Advertising

  3. On 2007-04-26 21:12, wrote:
    > I recently began using STL containers, mainly to store pointers to
    > structures using a vector:
    > std::vector <dataStruc*> vdata;


    There's a good reason not to put the actual structure in the vector I
    suppose?

    > Now I have a new requirement and I need random access to each
    > structure contained in the vector. There will be thousands of
    > structures contained by my vector and I will be accessing structures
    > randomly multiple times--for instance, I may need to access any given
    > structure 10 or 20 times as I generate data for my report.
    >
    > The easiest way I concieve this is to build a parallel vector that
    > stores the key string, such as:
    > std::vector <string> vKey;
    >
    > Then I can 'seek' the string, obtain it's position, and using that
    > position, efficiently obtain the structure stored in the structure.
    >
    > Would it be better to accomplish this with a different container, such
    > as a map or something else?


    If you only need to access them through one key a map is perfect, if you
    have several keys follow Victor's advice.

    --
    Erik Wikström
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Apr 26, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alfonso Morra
    Replies:
    11
    Views:
    713
    Emmanuel Delahaye
    Sep 24, 2005
  2. Replies:
    8
    Views:
    1,915
    Csaba
    Feb 18, 2006
  3. ehui928
    Replies:
    2
    Views:
    461
    ehui928
    May 29, 2006
  4. puzzlecracker
    Replies:
    8
    Views:
    300
  5. Simon
    Replies:
    2
    Views:
    489
    Simon
    May 31, 2009
Loading...

Share This Page