Archived

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

Dwiel

changeing size of array

Recommended Posts

Dwiel    365
I was writing a class that could read a file based on templates, and instances of those templates. Anyway... I was wondering if there was a way to have an array that has a variable number of elements. I know you can do it in VB, but I have been tring to do most of my programming in vc++ because, well, IT''s BETTER! Anyway if anyone knows how to do this I would be very happy if they explain it to me. thanx Tazzel3D ~ Zach

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
The way to do it is that you would have a pointer to the first entry of the array of elements in your class. Which when you want to resize the array you create a new pointer to a new array that is allocated to whatever the new size of the array is, then from there you copy the data from the old size array to the new size array, then delete the old array. It might sound complicated but its actually quite simple - below i illustrate.

template

class CArray
{
public:
ResizeArray(int iNewSize, BOOL bCopyOldData){
ArrayType *pNewArray = new ArrayType[m_iNewSize];

for(int i = 0;i < iNewSize;i++)
*(pNewArray + i) = *(m_pArray + i);

delete m_pArray;
m_pArray = pNewArray;
}

SetArrayEntry(int iArrayEntryNum, ArrayType Entry)
{
*(m_pArray + iArrayEntryNum) = Entry;
}
private:
ArrayType *m_pArray;
int m_iArraySize;
};

Im pretty sure that should be a working C++ teplate dynamic array class thingy. (Plz excuze me if there were errors - havn''t worked with C++ in a while) I reccomend you make one with more functionality though. Hope I helped.

-MaNiAk

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
o ya one more thing the code i gave you is rather prone to errors. Ill leave it to you to write code to check for that - if the code seems rather complex for you i reccomend reading C++ in ten minutes by Jesse Liberty. Its a real good small book and only 13 bucks.

-MaNiAk

Share this post


Link to post
Share on other sites
Gorg    248
vector is your solution, because vectors have been designed to work and be used like a C array. Yes, yes, you can use it instead of a real array!

You can do things like that and it is valid :

  

#include <vector>


void fillArray( int array[], int size )
{
for( int i =0; i < size; ++i )
{
array[i] = i;
}
}


int main()
{
std::vector<int> myArray;

myArray.resize(10); //make vector size 10


//yes this works and is valid by standard and

//supported by all flavor of STL libraries

fillArray( &myArray[0], myArray.size() );
}



Share this post


Link to post
Share on other sites
69with7of9    122
Unfortunately standard c++ will not allow you to resize an array. The only way is to create another array of the size you want and copy the old array into it. If you need variable memory allocaton, you might use linked lists instead of an array. Another option is to use an apvector. Apvectors were devolped as an alternate way of using c++ arrays by the AP Computer Science board. It contains a member function named .resize(x), which will change the size of your array to x. However, I personaly dislike using it because the decleration is somewhat odd. If you wan to find out more about it try http://www.collegeboard.org/ap/computer-science/html/classes.html for the source code. It might give you some ideas.

-David

Share this post


Link to post
Share on other sites