Container to map a vector of structures

J

jeff_j_dunlap

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
 
V

Victor Bazarov

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
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top