header

Data Structures

Data structures are used to store and manipulate sets of data. Typically a data structure contains multiple values of either similar or different types of data as opposed to a simple data type that stores a single value.

Arrays

Arrays are used to store a set of values of the same data type. Within reason, an array can be declared to contain as many values as necessary for the application but all of the those values must be of the same type. For example, you can have an array of integers or an array of floating point numbers but you cannot have an array that stores both integers and floating point numbers.

One-Dimensional Arrays

Conceptually, a one-dimensional array is like a list. It contains a sequence of values stored one after the other. While it is possible to have empty spaces between one item and the next, there are usually no gaps between items. Specific items are accessed based on their position within the list. The only difference is that in an array, you start counting at zero rather than at one. Here is a conceptual example of an array that has a capacity of 10 but currently has only 5 values stored in it.

One-Dimensional Array

The value of item 2 is 34, the value of item 4 is 55, and the value of item 0 is 28.

The one-dimensional array could also be represented as running from left to right (rather than top to bottom as it was above):

One-Dimensional Array

Two-Dimensional Arrays

A two-dimensional array is like a table of values; a rectangular grid with columns and rows. A specific element is accessed by using both its row and column position. In the example below, the item at row 0, column 2 is 34 and the item at row 3, column 4 is 99.

Two-Dimensional Array

A two-dimensional array can also be thought of as a one-dimensional array of one-dimensional arrays (i.e., a list each of whose elements is a one-dimensional array). Each row represents one of the arrays in the list. To access a specific element you first identify the location of the array in the list of arrays (the element's row position) and then the position within the specified array (the column position of the element).

Higher-Dimensional Arrays

In some cases, an application may require an array with even higher dimensions. For example, an Excel workbook represents a three-dimensional array of cells. Each worksheet (the third dimension) consists of a two-dimensional array of cells. In effect, the workbook contains a list (an array) of two-dimensional arrays.

Structures

In C++, a structure is used to store a collection of values which are usually (but not necessarily) of different data types. Such structures are also referred to as records. (The word "record" is usually used to describe this concept in an abstract sense while "structure" refers to the specific data structure used in C++.) Each component or field in a structure is given a name and specific values in a structure are accessed by using the corresponding field identifier as shown here:

structureId.fieldId

Suppose a structure is designed to store an inventory record consisting of the quantity on hand (an integer), the unit price (a double), and the name of the item (a string). We can represent such a structure as a block of memory containing three named components:

where qty is a 4-byte integer, price is an 8-byte double, and name is a string object. Suppose a structure-type variable named item is declared:

The following statements would initialize the values of the three fields that make up this structure:

item.qty = 20;
item.price = 1.25;
item.name = "Yellow Pencil";

The contents of this record could be viewed using the following output statements:

cout << item.qty << endl;
cout << item.price << endl;
cout << item.name << endl;