Arrays and the apvector class

  1. An array is a homogeneous collection. Each item stored in an array is the same type; for example, all integers, all doubles, or all strings. It is not possible to store both integers and strings in the same array.
  2. Items in an array are numbered, or ordered; that is, there is a first item, a fifteenth item, and so on. The number that refers to an item is the item's index, sometimes called the subscript.
  3. An array supports random access. The time to access the last item is the same as the time to access the first item is the same as the time to access the fifteenth item or the hundredth item.
  4. However, the built-in array type in C++ allows for any subscript, even if the value doesn't represent a valid array location. Thus, invalid subscripts are not checked before being used to index a built-in array..

Built-in arrays

There is a class Vector defined as part of the STL library in the C++ standard. The class Vector declared in vector.h is consistent with this standard class. There is also apvector, defined for use in the Advanced Placement computer science course. The apvector class is based on the class Vector, and all member function of the apvector class are also member functions of the Vector class

The apvector class is a template class in that you may specify the type of information that the homogeneous collection will hold.

  1. apvectors, like arrays, are indexed beginning at zero.
  2. apvectors can be initialized to hold the same value in every cell by providing a second argument to the constructor when the vector is defined.
  3. apvectors can be resized.
  4. apvectors should always be passed by reference to save memory and the time that would be required to copy if pass by value were used. There are occasions when a copy is needed, but in general pass by reference is preferred. Use const reference parameters to protect the parameter from being altered even when passed by reference.
  5. Built-in arrays are more cumbersome to use but are often more efficient. Nevertheless, you should use apvectors and switch to arrays only when you've determined that speed is essential and that the use of vectors is making your program slow (which is probably not the case).

APCS - the apvector class
Arrays are ordered collections of homogeneous elements. In this course we will use the apvector class to implement resizable one-dimensional arrays. Elements of apvectors are initialized beginning at zero and are accessed in the same way as built-in C++ arrays. But unlike built-in arrays, this class checks subscripts at runtime to ensure that they fall within the legal range. This eliminates subscript out-of-bounds bugs that often crash the system. In addition, the length of an apvector is stored within the vector, eliminating the need for an extra variable. One apvector can be copied to another with the overloaded assignment operator. When a local apvector is declared, the compiler only needs to allocate stack space for its length and a pointer to the actual array because the actual array is allocated dynamically from the much larger free store memory.

Here are examples of the four apvector constructors -
  1. apvector<char> letters; // empty (zero-length) array
  2. apvector<double> x(10); // array of 10 floating-point numbers
  3. apvector<int> counters(100,0); // array of 100 integers each initialized to zero
  4. apvector<int> newcounters (counters); // second array of 100 integers copied from counters

Here are some more examples of how to use apvectors in a program -

#include "apvector.h"			// include the header file; tell the compiler where it is
n = counters.length( ); 		// determine the current length of the array
counters.resize(2*n);			// double the size of the array
counters[0] = 42;			// store 42 in the first element of the array

// These prototype functions show how to always pass array objects by reference.

void fillArray(apvector<int> &counters);	
	// Note that the & must be explicitly used when an array object is passed by reference.

void displayArray(const apvector<int> &counters);
	// To avoid unnecessary copying, do not pass an array object by value .
	// Pass by reference using the keyword const to ensure the array cannot be changed.


Continue to:  Unit 1  / Prev  / Next