A major advantage of C++ over `traditional' programming languages is the ability to define array variables whose sizes are chosen at run time. The classes described here implement general array objects.
Extensive use is made of template classes, which are class definitions with arguments. For example, the generic array class can be used for arrays of any type of object. When an instance of the class is declared in a program, the type of object in an array is defined. This is illustrated in the examples below.
templateclass Array
int n; // number of elements in array
void Create(unsigned int nreq)(nreq is the requested number of elements)
void Recreate(unsigned int nreq)(nreq is the requested number of elements)
void Read_elements(istream &stream)
void Write_elements(ostream &stream)
void Fill(const type& t)(t is the value to copy into each element of the array)
Array& operator=(const Array& a)e.g.
Array<double> a, b; cin >> a; b = a;
type& operator[](const int& i) conste.g.
Array<double> a; cin >> a; int i = 3; cout << a[i];
void add_element(const type& newobj)
void add_elements(const array& newobj)
void remove_element(const int& rem)
friend void scatter(const array& gathered, const Array<int>& mask, array& full)
mask
is an array of zeroes and ones, one for each element
of array full
.
Elements of gathered
are copied into the elements of
full
corresponding to ones in mask
.
friend void gather(const Array& full, const Array<int>& mask, Array& gathered)Inverse of
scatter
.
Array map(const Array<int>& idx) constreturns an array whose element
i
is element idx[i]
in the present array.
friend void map(const Array& a, const Array<int>& idx, Array& b)maps
a
to b
such that
b[i] = a[idx[i]]
.
friend Array concat(const Array& a, const Array& b)e.g.
Array<double> a, b, c; cin >> a >> b; c = concat(a,b);
void reverse()e.g.
Array<double> a; cin >> a; a.reverse();
friend Array reverse(const Array& a)e.g.
Array<double> a, b; cin >> a; b = reverse(a);
static void spanlimit( const int ntot, // elements in array const int span, // elements in range int& start // suggested start of range in; corrected value out )
void spanlimit( const int span, // elements in range int& start // suggested start of range in; corrected value out ) const