473,326 Members | 2,095 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Container to map a vector of structures

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 <stringvKey;

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 '07 #1
2 2198
je***********@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 <stringvKey;

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
Apr 26 '07 #2
On 2007-04-26 21:12, je***********@yahoo.com 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 <stringvKey;

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
Apr 26 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: skscpp | last post by:
What's wrong with the following code? Compiler error is at the bottom. The compiler I am using is gcc version 2.95. ============================= // traits.h =============================...
7
by: Dave | last post by:
Hello all, I'm pondering why the default underlying container for std::priority_queue<> is std::vector<>. It would seem that inserts are liable to happen anywhere, which would make std::list<>...
2
by: Patrick Kowalzick | last post by:
Dear NG, I have two containers (standard library) which are nested, e.g.: std::vector< std::vector <int> > A; std::list< std::vector<int> > B; These structures where put in another class...
3
by: toton | last post by:
Hi, I have a container class, and I want to iterate over a portion of the container class while I insert/remove item from it. Noting down the present location & constructing iterator from there is...
20
by: Martin Jørgensen | last post by:
Hi, I'm reading a number of double values from a file. It's a 2D-array: 1 2 3 4 5 6 7 ------------- 1 3.2 2 0 2.1 3 9.3 4
8
by: toton | last post by:
Hi, I am facing problem some times, and not finding a suitable answer to solve it. I have a vector (or some other container, sometimes) of some class, named CC like vector<CCv, NOT vector<CC*; ...
11
by: food4uk | last post by:
Dear all : I am not good at programming, please give a hand. My data structure is very similar as an array. I actually can use the std::vector as container to organize my data objects. However,...
18
by: Goran | last post by:
Hi @ all! Again one small question due to my shakiness of what to use... What is better / smarter? private: vector<MyClass_t* itsVector; OR...
7
by: ademirzanetti | last post by:
Hi there !!! I would like to listen your opinions about inherit from a STL class like list. For example, do you think it is a good approach if I inherit from list to create something like...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.