|
-
Jan 5th, 2003, 05:17 PM
#1
Thread Starter
Addicted Member
dynamic arrays
How do you add or subtract elements from an array dynamically while preserving the other elements?
(In VB it was Redim Preserve)
thanks
-
Jan 5th, 2003, 07:29 PM
#2
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.
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)];
Look up your compiler's reference to see what member functions vector offers.
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++:
Code:
int *array = new int[size];
C:
Code:
int *array = (int*)malloc(size*sizeof(int));
To enlargen or shrink this array without losing your values you must first save them, allocate a new array and copy the values.
oldsize is the current size of the array, newsize is the new wanted size of the array.
C++:
Code:
int *temp = array;
array = new int[newsize];
memcpy(array, temp, _min(oldsize, newsize)*sizeof(int));
delete[] temp;
C:
Code:
int *temp = array;
array = (int*)malloc(newsize*sizeof(int));
memcpy(array, temp, _min(oldsize, newsize)*sizeof(int));
free(temp);
_min might not be defined, it can be defined like this:
C++:
Code:
template <typename T>
inline T _min(T a, T b)
{
return a < b ? a : b;
}
C:
Code:
#define min(a,b) \
(((a) < (b)) ? (a) : (b))
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 6th, 2003, 08:46 PM
#3
Hyperactive Member
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;
}
-
Jan 7th, 2003, 06:59 AM
#4
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 9th, 2003, 05:13 AM
#5
Thread Starter
Addicted Member
i don't think your methods would work with me, though, because my arrays are arrays of a custom class.
-
Jan 9th, 2003, 10:43 AM
#6
Monday Morning Lunatic
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.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jan 9th, 2003, 11:38 AM
#7
== for vector? Why?
i don't think your methods would work with me, though, because my arrays are arrays of a custom class.
Maybe. That depends on what the default constructor does.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 9th, 2003, 11:41 AM
#8
Monday Morning Lunatic
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.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jan 9th, 2003, 05:01 PM
#9
And quite a lot of bugs RE templates...
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 9th, 2003, 05:04 PM
#10
Monday Morning Lunatic
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 refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jan 10th, 2003, 01:34 PM
#11
Thread Starter
Addicted Member
i'm unfamiliar with vector. could someone point to a good tutorial?
thanks,
jmiller
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|