473,386 Members | 1,812 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,386 software developers and data experts.

appropriate container?

I'm converting some old handle-based code, updating it to use stl
containers. I've wondering if someone can suggest the proper container type
for the following:

I've got a bunch of bitmaps (stored as resources in a Windows program), each
identified by a resource ID (a short integer, say). I'll be loading them
all into memory and creating my own object (CachedPic) to hold (and later
manipulate) each image. So what I'll have is an association between a short
and a CachedPic.

I'll only be inserting them all once, deleting once when the program ends,
and retrieving pointers (references? iterators?) to the objects often. I
need fast access via the id.

I'm thinking that maybe a map<short,CachedPic> would be the answer, but I'm
not sure. Maybe map<short,CachedPic*>?

I don't really need to sort the objects, except that that probably makes
retrieval faster, I'd assume. Which is why I'm thinking map. Usually I use
vectors instead of arrays, but I thought maybe since I need to retrieve
specific items based on an id, a map might be faster.

I think I need to get a better book on the stl, since the one I have (the
C++ Programming Lanugage, Stroustrup) is technically complete but short on
examples (at least ones I find useful, in this case). But in the meantime,
if someone could give me their opinion on what container I should use, and
whether to store the object or a pointer there, I'd appreciate it.

Many thanks,

Howard
Jul 23 '05 #1
6 1440
Howard wrote:
I'm converting some old handle-based code, updating it to use stl
containers. I've wondering if someone can suggest the proper container type
for the following:

I've got a bunch of bitmaps (stored as resources in a Windows program), each
identified by a resource ID (a short integer, say). I'll be loading them
all into memory and creating my own object (CachedPic) to hold (and later
manipulate) each image. So what I'll have is an association between a short
and a CachedPic.

I'll only be inserting them all once, deleting once when the program ends,
and retrieving pointers (references? iterators?) to the objects often. I
need fast access via the id.

I'm thinking that maybe a map<short,CachedPic> would be the answer, but I'm
not sure. Maybe map<short,CachedPic*>?
If copying of 'CachedPic' object is expensive (and probably is considering
that it holds an image), then storing pointers is better.
I don't really need to sort the objects, except that that probably makes
retrieval faster, I'd assume. Which is why I'm thinking map. Usually I use
vectors instead of arrays, but I thought maybe since I need to retrieve
specific items based on an id, a map might be faster.
Sounds about right.
I think I need to get a better book on the stl, since the one I have (the
C++ Programming Lanugage, Stroustrup) is technically complete but short on
examples (at least ones I find useful, in this case). But in the meantime,
if someone could give me their opinion on what container I should use, and
whether to store the object or a pointer there, I'd appreciate it.


Hope my short reply reaffirms some of your thoughts already.

V
Jul 23 '05 #2
In article <PA********************@bgtnsc04-news.ops.worldnet.att.net>,
"Howard" <al*****@hotmail.com> wrote:
I'm converting some old handle-based code, updating it to use stl
containers. I've wondering if someone can suggest the proper container type
for the following:

I've got a bunch of bitmaps (stored as resources in a Windows program), each
identified by a resource ID (a short integer, say). I'll be loading them
all into memory and creating my own object (CachedPic) to hold (and later
manipulate) each image. So what I'll have is an association between a short
and a CachedPic.

I'll only be inserting them all once, deleting once when the program ends,
and retrieving pointers (references? iterators?) to the objects often. I
need fast access via the id.

I'm thinking that maybe a map<short,CachedPic> would be the answer, but I'm
not sure. Maybe map<short,CachedPic*>?

I don't really need to sort the objects, except that that probably makes
retrieval faster, I'd assume. Which is why I'm thinking map. Usually I use
vectors instead of arrays, but I thought maybe since I need to retrieve
specific items based on an id, a map might be faster.
I'd start with a std::map<short, CachedPic>. If you encapsulate your
container choice with typedefs or an enclosing class, then you can
always easily optimize later (choose another container).

If you go with map<short,CachedPic*>, you still have to store your
CachedPic somewhere and (copy it there), and you have the added
complication of managing that extra storage space and making sure
pointers into it are stable. That's not to say that
map<short,CachedPic*> wouldn't be better, just that it is more
complicated, and you can always change to it if profiling (or some other
constraint) suggests it. But I'd start simple.

If the one copy of the CachedPic into the map is really killing you, you
could give CachedPic a default ctor and:

CachedPic& cp = my_map[id];
infile >> cp;

That is, enter a default constructed CachedPic into the map, and then
read your data directly into the map.

Other possibilities include:

vector<pair<short, CachedPic> > // manually sort and lookup with
lower_bound
hash_map<short, CachedPic> // unsorted but fast lookup

The hash_map is likely to have the fastest lookup time, but you pay a
price in the risk of the hash_map changing out from under you (as it is
commonly supplied but non-standard). Still, easy to change if properly
encapsulated though.

If your id's are simply sequential numbers starting with 0, you can
easily create a perfect hash simply with:

vector<CachedPic>

i.e. the id is the index into the vector.
I think I need to get a better book on the stl, since the one I have (the
C++ Programming Lanugage, Stroustrup) is technically complete but short on
examples (at least ones I find useful, in this case). But in the meantime,
if someone could give me their opinion on what container I should use, and
whether to store the object or a pointer there, I'd appreciate it.


My personal favorite is http://www.josuttis.com/libbook/ but there are
several good ones out there.

-Howard
Jul 23 '05 #3


Thanks, guys... looks like map<short,CachedPic*> is working out fine.

-Howard
Jul 23 '05 #4
* Howard:


Thanks, guys... looks like map<short,CachedPic*> is working out fine.


For efficiency consider a std::vector. :-)

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #5

"Alf P. Steinbach" <al***@start.no> wrote in message
news:42****************@news.individual.net...
* Howard:


Thanks, guys... looks like map<short,CachedPic*> is working out fine.


For efficiency consider a std::vector. :-)


I did, but since my ids are not in order from 0..n-1, and may be inserted in
somewhat random order, I think map will be better. Agreed? It's mainly the
"find" time I want to reduce, and get rid of the current looping through an
array of ids to locate the index for an array of handles that was previously
in place.

-Howard


Jul 23 '05 #6
* Howard:

"Alf P. Steinbach" <al***@start.no> wrote in message
news:42****************@news.individual.net...
* Howard:


Thanks, guys... looks like map<short,CachedPic*> is working out fine.

For efficiency consider a std::vector. :-)


I did, but since my ids are not in order from 0..n-1, and may be inserted in
somewhat random order, I think map will be better. Agreed?


Two ways.

Use consecutive id's.

Or use the vector indices instead of id's (you only need the id's to retrieve
the resources -- presumably you're not updating the resources).

It's mainly the
"find" time I want to reduce, and get rid of the current looping through an
array of ids to locate the index for an array of handles that was previously
in place.


"find" time is minimum when you use a direct index or pointer.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #7

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

Similar topics

2
by: Maitre Bart | last post by:
What I want to perform is calling a member function of container 1 (CRunner), using as argument the value of a container 2 (CNames). The purpose is to transfer values from C2 into C1 using a...
3
by: jignesh shah | last post by:
Hi all, Is there a way to recover a single container if its been corrupted or mark bad without restoring whole tablespace? environment: db28.1/aix5.1/tsm/rs-6000. Regards Jignesh
6
by: Réal Forté | last post by:
Hi, I have a puzzling case here: using System; namespace TestOverrides { class Class1
53
by: Alan Silver | last post by:
Hello, I understand the issue that tables should be used for tabular data and not for layout, but I would like some clarification as to exactly what constitutes tabular data. For example, if...
7
by: toton | last post by:
Hi, I want a circular buffer or queue like container (queue with array implementation). Moreover I want random access over the elements. And addition at tail and remove from head need to be low...
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,...
2
by: Daniel Lipovetsky | last post by:
I would like for an object to "report" to a container object when a new instance is created or deleted. I could have a container object that is called when a new instance is created, as below. ...
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...
36
by: Peter Olcott | last post by:
So far the only way that I found to do this was by making a single global instance of the container class and providing access to the contained class, through this single global instance. Are...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.