How do you add or subtract elements from an array dynamically while preserving the other elements?
(In VB it was Redim Preserve)
thanks
Printable View
How do you add or subtract elements from an array dynamically while preserving the other elements?
(In VB it was Redim Preserve)
thanks
You can't really. There are workarounds however.
The first and easiest is to use a dynamic array class in the first place. This only works in C++ as C doesn't have classes (duh!). A very good one comes with every C++ compiler and is called vector.
Look up your compiler's reference to see what member functions vector offers.Code:// Usage of vector:
#include <vector>
using namespace std;
// declare an initially empty dynamic array of ints:
vector<int> arInts;
// declare a dynamic array of floats with an initial size of 5:
vector<float> arFloats(5);
// general:
// vector<type> name[(initial size)];
vector grows as necessary to accomodate more elements.
There's another way, but it is more complicated. You can allocate memory on the heap to get an array:
C++:
C:Code:int *array = new int[size];
To enlargen or shrink this array without losing your values you must first save them, allocate a new array and copy the values.Code:int *array = (int*)malloc(size*sizeof(int));
oldsize is the current size of the array, newsize is the new wanted size of the array.
C++:
C:Code:int *temp = array;
array = new int[newsize];
memcpy(array, temp, _min(oldsize, newsize)*sizeof(int));
delete[] temp;
_min might not be defined, it can be defined like this:Code:int *temp = array;
array = (int*)malloc(newsize*sizeof(int));
memcpy(array, temp, _min(oldsize, newsize)*sizeof(int));
free(temp);
C++:
C:Code:template <typename T>
inline T _min(T a, T b)
{
return a < b ? a : b;
}
Code:#define min(a,b) \
(((a) < (b)) ? (a) : (b))
Simple One:
Code:#include "stdafx.h"
#include <malloc.h>
int main(int argc, char* argv[])
{
int* z = (int*)calloc(10, sizeof(int));
z[1] = 10;
z = (int*)realloc(z, sizeof(int) * 12);
z[11] = 15;
printf("Z[1] = %d\nZ[11] = %d\n", z[1], z[11]);
free(z);
return 0;
}
Oh yeah, forgot realloc. realloc is simply a wrapper for this part:
int *temp = array;
array = (int*)malloc(newsize*sizeof(int));
memcpy(array, temp, _min(oldsize, newsize)*sizeof(int));
free(temp);
except that it determines oldsize programmatically.
i don't think your methods would work with me, though, because my arrays are arrays of a custom class.
Use vector<> then.
Your classes each need to support various things in order to use them with vector<>.
You need to define operator=, operator==, and the constructor, default constructor, and copy-constructor.
== for vector? Why?
Maybe. That depends on what the default constructor does.Quote:
i don't think your methods would work with me, though, because my arrays are arrays of a custom class.
Oh, ignore that one. If you try and do much *with* the vector afterwards, it'll probably try and use it.
And VC6 has some bugs RE operator use.
And quite a lot of bugs RE templates...
They're the same ones. It has difficulty deciding which implicit operators are needed for which template functions.
For example, with vector, you don't need operator< unless you actually use any of the sorting functionality.
i'm unfamiliar with vector. could someone point to a good tutorial?
thanks,
jmiller