Re: Container to map a vector of structures
jeff_j_dunlap@yahoo.com 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