System.DynamicArray

From RAD Studio API Documentation
Jump to: navigation, search

C++

template<class T> class RTL_DELPHIRETURN DynamicArray

Properties

Type Visibility Source Unit Parent
class public sysdyn.h System System

Description

DynamicArray is the C++ analog for the Delphi dynamic array type.

A dynamic array is a resizable collection implemented as an array. The DynamicArray template is a C++ implementation of Delphi dynamic array types. The DynamicArray template uses a binary layout compatible with the Delphi dynamic array implementation.

template <class T> class DELPHIRETURN DynamicArray;

You must specify one template parameter:

Parameter Usage

type

The type of the elements

Length of dynamic arrays

The size of a DynamicArray can be set or obtained from the Length property:

//C++ example

DynamicArray<int> arrayOfInt;
arrayOfInt.Length = 10;
cout << "ArrayLength: " << arrayOfInt.Length << endl;

To free a DynamicArray, assign it a Length of zero.

Accessing data

Access elements of a Dynamic Array using the subscript [] operator:

//C++ example

void InitArray(DynamicArray<char> &c_array)
{
  c_array[0] = 'A';
  c_array[1] = 'B';
  cout << "Third char is: " << c_array[2];
}

Bounds of dynamic arrays

The Low and High properties of DynamicArrays return the low and high bounds of the array, respectively.

//C++ example

int TotalArray(const DynamicArray<int>& arrayOfInt)
{
  int total=0;
  for (int i=arrayOfInt.Low; i<=arrayOfInt.High; i++)
    total += arrayOfInt[i];
  return total;
}

Note: The Low property always returns 0. The High property returns Length-1. So you may also traverse a dynamic array using the following construct:

for (int i=0; i<arrayOfInt.Length; i++)

Assigning, comparing and copying dynamic arrays

Dynamic arrays are reference counted. When a DynamicArray is assigned to another, only the reference is assigned (and the reference count adjusted) while the content of the source is not copied. Similarly, when two dynamic arrays are compared, only the references are compared and not the contents. To copy the contents of a DynamicArray, you must use the Copy (or CopyRange) method.

//C++ example

void foo(DynamicArray<int> &i_array)
{
  DynamicArray<int> temp = i_array;
  assert(temp == i_array); // Temp and i_array point to same chunk
                           // of data.
  i_array[0] = 20;
  assert(temp[0] == 20); // Temp 'sees' changes to i_array.
  temp = i_array.Copy(); // Temp gets its own copy of data.
  temp[0] = 10;
  assert(temp[0] != i_array[0]); // Above assignment did not
                                 // modify i_array.
}

Multidimensional dynamic arrays

Dynamic arrays can be multidimensional. Multidimensional dynamic arrays don't have to be rectangular. The Length property can be used to set each dimension. The following example illustrates this:

//C++ example

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);
  }
}

Code Examples