Archived

This topic is now archived and is closed to further replies.

jollyjeffers

Another trivial C++ Syntax Question...

Recommended Posts

jollyjeffers    1570
hi people, You guys were all really helpful with my other question on C++ syntax (a couple of days ago), so hopefully you''ll be able to help me out here Redimensioning arrays. I know (using pointers) that to create an array I can do: int *pArray = new int[10000] However, once I''ve created this array, how could I add new elements and/or delete elements (the latter isn''t so important). I''ve been using VB for so long now, that I''m used to using (in VB!) Dim Array() as long redim Array(10000) as long redim preserve Array(10001) as long ''add another element I don''t believe the same is not possible in C++ but bjarne Stroustrup''s book doesn''t seem to mention how to do it (or if it does, I missed it!). Any quick hints/suggestions? many thanks in advance, Jack;

Share this post


Link to post
Share on other sites
Cybertron    122
arrays were one of the problems I encountered when moving from VB to C++

to resize them, use malloc() and free() to create/delete and realloc() to resize. The extra memory added isn''t initialized though, but the contents of the original array is kept. Look in MSND for description of these functions

Most programs don''t need this feature, usually a maximum limit is good enough. Most of the array will remain unused though, unless it fills up

Share this post


Link to post
Share on other sites
origil    134
I'f suggest using a linked list for what you're trying to do.

Or... you could create a new array of the new desired size and move the data using a for loop from the last array to the new array... It'd be pretty slow when dealing with large arrays.
Of course you'd have to delete the old array.

[edited by - origil on June 7, 2002 3:12:47 PM]

Share this post


Link to post
Share on other sites
Kippesoep    892

    
#include <vector>

std::vector <int> array;
array.resize (1000); //Resize to 1000 elements

array [0] = 15; //First element

array [999] = 20; //Last element

array.resize (1001); //Add one extra

array [1000] = 25; //New last element

if (array [999] != 20) PANIC!




[edited by - Kippesoep on June 7, 2002 3:18:18 PM]

Share this post


Link to post
Share on other sites
jollyjeffers    1570
hi,

I''m trying to stick to C++ standards - new/delete rather than malloc/free...

Kippesoep - I''ve seen some stuff on those vectors things before (STL right?), but does that slow things down at all? I assumed that using additional helper libraries/interfaces would add an additional overhead to the operations...

or does it literally just template what origil suggested, and create a new temporary array and copy it across etc...?

thanks for the help!

Jack;

Share this post


Link to post
Share on other sites
Oluseyi    2103
quote:
Original post by jollyjeffers
Kippesoep - I''ve seen some stuff on those vectors things before (STL right?), but does that slow things down at all?

a.) No.
b.) Do you think using new and delete would be any faster, either for you (in terms of typing out the extra statements while preserving your data) or for your code (execution time)?

Using resize isn''t particularly efficient anyways. What you should do is call reserve when you first construct your vector (a design flaw, but that''s another discussion) to limit unecessary resizing early on, then simply use push_back and/or push_front to add additional elements as needed. The vector will take care of resizing itself, and in a more efficient way (when it reaches capacity, it roughly doubles its size so it doesn''t have to resize so frequently).

Finally, if the time taken to resize the vector is a major bottleneck in your algorithm, you don''t want a vector; you want a constant insertion time structure like a list or unsorted associative array. vector''s attraction is the same as that of arrays - constant time random access.

Share this post


Link to post
Share on other sites
zbarrier1    122
JollyJeffers,
The Standard Template Libraries (STL) are very efficient. I would suggest learning to use them and spending the time saved (writing your own would take longer) on other topics. From my experience in the Software Industry, most professionals using C++ today use STL. Hope this helps.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
not that it really matters, but neither of your questions has been about syntax. The first was about OS specific functions, this one is about the standard library (and yes the correct answer is to use vector).

Share this post


Link to post
Share on other sites
jollyjeffers    1570
so a Vector it is then thanks for the explanations people...

AP: oops! As far as I saw it, they were fairly basic/fundamental parts of programming C++ (I thought there was a standard/non-os specific answer to the first question).

Jack;

Share this post


Link to post
Share on other sites