Dynamic Arrays
Hi!
I need a way for a engine to create a dynamic array with a variable number of elements. It should be possible to append elements to the array, thus I need some sort of list. Since I''m still a C++ newbie, I don''t know how to do that. Could someone please give me some hints? What''s the best way to achieve that?
Thanks in advance
LaBasX2
A dynamic array can be done like so:
int* myDynamicArray = new int[numberOfItems];
But if you want to be able to easily append elements (and hence, resize the array), you might want to look into std::vector.
~~~~~~~~~~
Martee
int* myDynamicArray = new int[numberOfItems];
But if you want to be able to easily append elements (and hence, resize the array), you might want to look into std::vector.
~~~~~~~~~~
Martee
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
A far better way to implement a dynamic array is to use a linked list. Look this up on the net for for details and some tutorials.
The basics are that the list allows you to add, remove and insert values into the list at runtime very easily.
If you are stuck, let me know and I''ll point you in the right direction and show you some source code.
The basics are that the list allows you to add, remove and insert values into the list at runtime very easily.
If you are stuck, let me know and I''ll point you in the right direction and show you some source code.
Linked lists are absolutely terrible for producing cache misses though... heavy dependance on them can become a big bottleneck in performance. Of course, only if performance is one of your goals... if it is not, then they do make life easier.
I agree with using std::vector. You can append elements to the end of vectors using std::vector.pushback(); I believe.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement