473,386 Members | 1,908 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.

Dynamic array

Hi all.

I have a virtual "world" with different objects which are "born" and
which "die" sometimes. This means I have a variable amount of objects in
this world which I need to store in some structure.

Sounds like a perfectly good job for vectors (STL).

Another method would be to create a pre-defined, empty array. Now if an
object is born, the next free slot in this array is searched and the new
object is stored at that position. When an object dies, the
corresponding slot is simply "freed" again.

Now my question:

Which of this methods (STL, array) will be faster if my world has a huge
number of objects (5000-10000)?

Maybe any other ideas?

Regards,

Jan
Jul 22 '05 #1
7 2966
In article <MP************************@news.t-online.com>,
Jan Krumsiek <ja**********@gmx.de> wrote:
Hi all.

I have a virtual "world" with different objects which are "born" and
which "die" sometimes. This means I have a variable amount of objects in
this world which I need to store in some structure.

Sounds like a perfectly good job for vectors (STL).


If most of your insertions and deletions are at the end of the
sequence, then yes, vector sounds reasonable. However, if it is likely
that objects anywhere in your "world" can "die", (i.e. if you will
frequently be removing items from the middle of the sequence), another
container may be better (like list or set).

Jul 22 '05 #2
Clark Cox <cl*******@mac.com> writes:
In article <MP************************@news.t-online.com>,
Jan Krumsiek <ja**********@gmx.de> wrote:
Hi all.

I have a virtual "world" with different objects which are "born" and
which "die" sometimes. This means I have a variable amount of objects in
this world which I need to store in some structure.

Sounds like a perfectly good job for vectors (STL).


If most of your insertions and deletions are at the end of the
sequence, then yes, vector sounds reasonable. However, if it is likely
that objects anywhere in your "world" can "die", (i.e. if you will
frequently be removing items from the middle of the sequence), another
container may be better (like list or set).


You may also want to look into using some sort of small object
allocator routine, depending on the size of these objects, since they
are so many.
Jul 22 '05 #3

"Jan Krumsiek" <ja**********@gmx.de> wrote in message
news:MP************************@news.t-online.com...
Hi all.

I have a virtual "world" with different objects which are "born" and
which "die" sometimes. This means I have a variable amount of objects in
this world which I need to store in some structure.

Sounds like a perfectly good job for vectors (STL).

Another method would be to create a pre-defined, empty array. Now if an
object is born, the next free slot in this array is searched and the new
object is stored at that position. When an object dies, the
corresponding slot is simply "freed" again.

Now my question:

Which of this methods (STL, array) will be faster if my world has a huge
number of objects (5000-10000)?
I would expect them to be almost exactly the same. You should choose on the
basis of which results in simpler code.

Maybe any other ideas?


Actaully sounds much more like a task for STL list or map depending on your
requirements. I would expect either of those to be faster than vector or
array.

The most important thing is that, whatever data structure you choose, you
create a class that hides the details of that representation from the rest
of your program. That way you can change the representation in the future
without affecting the rest of the program. This seems especally imoprtant in
this case because you are unsure of what is the best, and you seem
interested in expereimenting to find out.

john
Jul 22 '05 #4
John Harrison wrote:

The most important thing is that, whatever data structure you choose, you
create a class that hides the details of that representation from the rest
of your program. That way you can change the representation in the future
without affecting the rest of the program. This seems especally imoprtant in
this case because you are unsure of what is the best, and you seem
interested in expereimenting to find out.


Yes, i get what you mean. I will create some CWorld class with "add",
"remove" and "getobject" methods.

Jan
Jul 22 '05 #5
Clark Cox wrote:
If most of your insertions and deletions are at the end of the
sequence, then yes, vector sounds reasonable. However, if it is likely
that objects anywhere in your "world" can "die", (i.e. if you will
frequently be removing items from the middle of the sequence), another
container may be better (like list or set).


Yes, actually objects can spawn and be removed all the time during the
main loop. Why is list or set better then? What are the differences?

Regards,
Jan
Jul 22 '05 #6
Billy O'Connor wrote:
You may also want to look into using some sort of small object
allocator routine, depending on the size of these objects, since they
are so many.


Thanks for the tip, but what do you mean with "small object allocater
routine"? Wouldn't that be something like the array I described in my
original posting?

Regards,

Jan
Jul 22 '05 #7
Jan Krumsiek wrote:
Clark Cox wrote:
If most of your insertions and deletions are at the end of the
sequence, then yes, vector sounds reasonable. However, if it is likely
that objects anywhere in your "world" can "die", (i.e. if you will
frequently be removing items from the middle of the sequence), another
container may be better (like list or set).

Yes, actually objects can spawn and be removed all the time during the
main loop. Why is list or set better then? What are the differences?

Regards,
Jan


because removing from the middle of a vector involves copying everything
after the removal point. It's O(n), whereas removing from a list is
O(1), and (i think) a set is O(log(n))
Jul 22 '05 #8

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

Similar topics

6
by: Vasileios Zografos | last post by:
Hello, I have a function that generates some values (e.g. vertices in 2d space) the number of which I dont know. So, it could generate 20 vertices, 100 vertices, or even 1 vertex. void...
4
by: Scott Lyons | last post by:
Hey all, Can someone help me figure out how to pass a dynamic array into a function? Its been giving me some trouble, and my textbook of course doesnt cover the issue. Its probably something...
5
by: meyousikmann | last post by:
I am having a little trouble with dynamic memory allocation. I am trying to read a text file and put the contents into a dynamic array. I know I can use vectors to make this easier, but it has to...
8
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
6
by: Materialised | last post by:
Hi Everyone, I apologise if this is covered in the FAQ, I did look, but nothing actually stood out to me as being relative to my subject. I want to create a 2 dimensional array, a 'array of...
1
by: lemonade | last post by:
Hello! Can someone explain to me the difference between dynamic array of pointers vs dynamic array of objects by giving a real life example. Following is the code that I am using for dynamic...
2
by: assgar | last post by:
Hi Developemnt on win2003 server. Final server will be linux Apache,Mysql and PHP is being used. I use 2 scripts(form and process). The form displays multiple dynamic rows with chechboxs,...
0
by: pjr | last post by:
Using VS2005, I dynamically create an event delegate. Code follows question. My method gets the event's parameters and passes them onto a common event handler. My delegate gets called when expected...
11
by: C C++ C++ | last post by:
Hi all, got this interview question please respond. How can you quickly find the number of elements stored in a a) static array b) dynamic array ? Rgrds MA
13
by: kwikius | last post by:
Does anyone know what a C99 dynamic array is, and if it will be useable in C++? regards Andy Little
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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.