Another trivial C++ Syntax Question...
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;
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
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
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]
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]
#include <vector>std::vector <int> array;array.resize (1000); //Resize to 1000 elementsarray [0] = 15; //First elementarray [999] = 20; //Last elementarray.resize (1001); //Add one extraarray [1000] = 25; //New last elementif (array [999] != 20) PANIC!
[edited by - Kippesoep on June 7, 2002 3:18:18 PM]
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;
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;
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.
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.
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.
STL is designed for efficiency. The vector class is for exactly this sort of problem, and would suit your needs.
[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files ]
[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files ]
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).
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement