fastest way to resize dynamic arrray

Dear all,
I am a visual basic user, but I need to learn C++ to use the library of my colleague and reduce the calculation time.
When I use visual basic I often use Redim to resize an array.
However, I found it not easy in C++.
My arrray (of a structure) is huge (occupy about 1-3 Gb in memory) and I do not know it's size. I calculate with all existing elements and know does it need a new element. If yes, I add new element to the array, and then continue calculating.

Could someone, please, tell me how to resize array in a fastest way ?
One of myfriend adviced me to use pointer, while other suggested to use list. But I do not know how.
I think copy memory is not a good choice.
In addition, while calculating for new element, the whole array is used. Therefore, I waver that pointer may take a lot of time. Is it right ?
Please, help me.
Last edited on
Arrays can't be resized in C++. What you have to do is allocate a bigger array and copy all the old elements to the new array.

std::vector is used similar to an array, but it will handle the reallocation for you.

If you want to avoid copying you could use std::deque. It works similar to an array but instead of allocating all elements in a contiguous array it allocates many smaller chunks when needed so it never has to copy old elements when the size changes.
use a c++ matrix library with a good documentation, so you save time and energy, download one from sourceforge
Last edited on
Topic archived. No new replies allowed.