C++Builder里面有動態(tài)數(shù)組,使用如下:
- //==============================================  
-   
- //數(shù)組長度  
-   
- DynamicArray<int> arrayOfInt;  
- arrayOfInt.Length = 10;  
- cout << "ArrayLength: " << arrayOfInt.Length << endl;  
-   
- //==============================================  
釋放數(shù)組,只有把長度設置為0即可,不可使用delete刪去:
 
- //==============================================  
-   
- arrayOfInt.Length = 0;  
-   
- //==============================================  
訪問數(shù)據:
- void InitArray(DynamicArray<char> &c_array)  
- {  
-   c_array[0] = 'A';  
-   c_array[1] = 'B';  
-   cout << "Third char is: " << c_array[2];  
- }  
-   
- //==============================================  
 Low和 High 屬性表示數(shù)組的上下邊界
例如數(shù)組求和的函數(shù):
- int TotalArray(const DynamicArray<int>& arrayOfInt)  
- {  
-   int total=0;  
-   for (int i=arrayOfInt.Low; i<=arrayOfInt.High; i++)  
-     total += arrayOfInt[i];  
-   return total;  
- }  
- 當然也可以這樣:  
-   
- for (int i=0; i<arrayOfInt.Length; i++)  
-   
- {  
-   
- total += arrayOfInt[i];  
-   
- }  
-   
- //==================================================  
賦值,比較和復制動態(tài)數(shù)組:
Dynamic Arrays are reference counted. When a 
DynamicArray is assigned to another, only the reference is assigned (and the reference count adjusted), the content of the source is not copied. Similarly, when two Dynamic Arrays are compared, only the references are compared, not the contents. To copy
 the contents of a DynamicArray, you must use the Copy (or CopyRange) methods.
- //C++ example  
-   
- void foo(DynamicArray<int> &i_array)  
- {  
-   DynamicArray<int> temp = i_array;//此處是引用賦值,不復制數(shù)據  
-   assert(temp == i_array); // temp 和i_array 指向同一內存數(shù)據塊  
-   i_array[0] = 20;//此處改變,實際上就是Temp的值  
-   assert(temp[0] == 20); // Temp的值同樣也會改變  
-   temp = i_array.Copy(); // 賦值數(shù)組,此時temp 和i_array 指向不同內存塊,  
- //但是這兩個內存所含數(shù)據相同  
-   temp[0] = 10;//此時改變temp的值不會改變i_array的值  
-   assert(temp[0] != i_array[0]); // Above assignment did not  
-                                  // modify i_array.  
- }  
- //=====================================================  
Multidimensional dynamic arrays(多維數(shù)組)
例如2維數(shù)組,注意該2維數(shù)組可以不是方陣數(shù)組,
即數(shù)組可以由10行,但每一行的元素個數(shù)可以不相同- typedef DynamicArray< DynamicArray < AnsiString > > T2DStringArray;  
- void foo(T2DStringArray &s_array)  
- {  
-   SetLength(s_array, 10);  
-   for (int i=0; i<s_array.Length; i++)  
-   { // Set lengths of second dimensions.(NOTE: non-rectangular)  
-     SetLength(s_array[i], i+1);  
-     for (int j=0; j<s_array[i].Length; j++)  
-       s_array[i][j] = itoa(i*10+j);  
-   }  
- }  
-   
- //=====================================================