Dim intArray(1 to 4) As Integer
Can this be done in C/C++ so that keeping track of things (especially objects) would be easier by starting with 1?
C/C++ Arrays
In VB you could declare an array like so
No ... although if you really want your arrays to start at one, I suppose you could just make them one bigger, and not use the 0th item. I don''t think that''s a good idea, though.
~~~~~~~~~~
Martee
~~~~~~~~~~
Martee
#include <stdio.h>int main(int argc, char *argv[]){ int* intarray = new int[4]; --intarray; for (int x=1; x<5; ++x) { intarray[x] = x*x; } printf("Array[1] %d\n", intarray[1]); printf("Array[2] %d\n", intarray[2]); printf("Array[3] %d\n", intarray[3]); printf("Array[4] %d\n", intarray[4]); return 0;}
Not at all recommended, by the way Bad programming practice, really. This example was just provided to further understanding of the language
You could always create a template array class and overload the [] operator to whatever you wanted it to. You could also include bounds checking for safety....
Then you can instantiate an array of any type like this...
etc...
// Template array class. Note... this was written off the top of// my head, may require debuggering.template <class T>class CArray{public: CArray(int size) { data = new T[size]; maxItems = size-1; } ~CArray() { delete[] data; } T& operator[] (int index) { //index--; // uncomment this line to make the array 1 based if(index > maxItems) return data[maxItems]; elseif (index < 0) return data[0]; else return data[index]; }private: T* data; int maxItems;};
Then you can instantiate an array of any type like this...
CArray<int> myIntegerArray;CArray<char> myCharArray;CArray<float> myFloatArray;
etc...
Is it possible to redimension an array like VB''s ReDim statement? Or must I manually store the data temporarily and delete and remake the array to what is needed?
1. You cannot resize a normal array at all. Ever.
2. You cant resize a dynamically allocated array* but you can make a new array and copy the contents across, then destroy the old one.
3. If you use something like the template class I wrote above, you could include methods that take care of this array copying resizing stuff internally.
4. Ideally, your arrays should be big enough to fit what you need to put in them. If your array is likely to change size a lot, and is likely to get very large, you may be better off using a linked list.
*ignoring functions like realloc() which are dangerous, and they just do the samething anyway under the hood.
2. You cant resize a dynamically allocated array* but you can make a new array and copy the contents across, then destroy the old one.
3. If you use something like the template class I wrote above, you could include methods that take care of this array copying resizing stuff internally.
4. Ideally, your arrays should be big enough to fit what you need to put in them. If your array is likely to change size a lot, and is likely to get very large, you may be better off using a linked list.
*ignoring functions like realloc() which are dangerous, and they just do the samething anyway under the hood.
Sandman:
How would i go about creating the same tamplate for a 2d array?
i need a place to store outside inputs and as you anderstand there can be any number of inputs so i can not limit the array size.
I''m just not sure how to overload the second []!
Thanks
How would i go about creating the same tamplate for a 2d array?
i need a place to store outside inputs and as you anderstand there can be any number of inputs so i can not limit the array size.
I''m just not sure how to overload the second []!
Thanks
quote:Original post by Daishi
Is it possible to redimension an array like VB''s ReDim statement? Or must I manually store the data temporarily and delete and remake the array to what is needed?
use a std::vector
[Questions (STFW) | GDNet Start Here | GDNet Search | Forum FAQ | Google | Asking Smart Questions ]
[Docs (RTFM) | MSDN | SGI''s STL | OpenGL | File formats]
[C++ Must Haves (RTFS) | MinGW | Boost | Loki | FLTK | SDL ]
Stolen from Magmai Kai Holmlor, who held it from Oluseyi, who was inspired by Kylotan...
quote:Original post by Daishiyou can use realloc(), or better yet, std::vector
Is it possible to redimension an array like VB''s ReDim statement? Or must I manually store the data temporarily and delete and remake the array to what is needed?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement