Expanding Array

Hello,

how can I expand array?

for example let's say I have array with 3 elements

int a[3] = {1, 5, 10};

how can I expand him to 5 elements but make him still contain his parameters?

same with 2 dimensional array (I hope it is called "dimension")

and array that looks like this:

int a[3][2] = {1 {1,2}, 2 {2,3}, 3{3,4}}

or simply, how can I make array that will have 3 elements, and each of those elements will have 2 elements, and how can I define values of those elements when creating array
You must create a new array of the size you want and copy the elements of the old array into it.
An "expanding array" is called a vector, and an implementation is available in the STL (include the <vector> header).

A vector allows array-like accessing (myVec[3], just like you would do myArray[3]) as well as "expanding", either in a controlled way (myVec.resize(newSize)) and in an incremental way (myVec.push_back(newItem), which adds an extra item to the end of the vector, effectively increasing its size by 1).

Do keep in mind that while a vector supports this behaviour, it's not cheaper than your own implementation [maybe somewhat, because the implementation is done by people much smarter than you and I]. Changing the size of a vector still means a new array has to be created and all elements have to be copied into it. Just because it's hidden doesn't mean you don't pay the cost.
Topic archived. No new replies allowed.