Chapter Goals

Using Vectors to Collect Data Items

  • Vector — a collection of data items of the same type
  • Each element can be accessed separately
  • Here we define a vector of salaries vector<double> salaries(10);
  • Type is vector<double>
  • (10) indicates that the vector holds ten values
  • salaries stores a sequence of 10 double values

Figure 1 Figure of Salaries

Using Vectors to Collect Data Items

[] operator

  • To fill slot 4 with the value 35000: salaries[4] = 35000;
  • Slots numbers begin at zero
  • Slot 4 is the fifth slot in the vector
  • Indexed the same as the C-array

Figure 2 Vector Slot Filled with double Value

Using Vectors to Collect Data Items

Index values

Using Vectors to Collect Data Items

Syntax : Vector Variable Definition

vector<type_name> variable_name; vector<type_name> variable_name(initial_size);

Example:

vector<int> scores; vector<Employee> staff(20);

Purpose:

Define a new variable of vector type, and optionally supply an initial size.

Syntax : Vector Subscript

vector_Expression[integer_expression]

Example:

 salaries[i + 1] 

Purpose:

Access an element in a vector.

Working with Vectors

Working with Vectors

Working with Vectors

salvect.cpp

Common Error

Bounds Errors

Advanced Topic

Strings Are Vectors of Characters

Vector Parameters and Return Values

Vector Parameters and Return Values

Modifying a Vector

Vector Parameters and Return Values

Returning a Vector

Vector Parameters and Return Values

Returning a Vector of Positions

Advanced Topic

Passing Vectors by Constant Reference

Removing and Inserting Vector Elements

Removing an element — Where order is not important

  • Overwrite old element with last element
  • Shrink vector int last_pos = values.size() -1; values[pos] = values[last_pos]; values.pop_back();

Figure 5 Removing an Element in an Unordered Vector

Removing and Inserting Vector Elements

Removing an element — Where order is important

  • Move all elements following the element to be removed to a lower index
  • Shrink the size of the vector
for (int i = pos; i < values.size() - 1; i++) values[i] = values[i + 1]; values.pop_back();

See erase2.cpp

Figure 6 Removing an Element in an Ordered Vector

Removing and Inserting Vector Elements

Inserting, other than at the end

  • Create a new element at the end
  • Move all elements above the insertion location up (to a higher index) by one slot
int last = values.size() - 1; values.push_back(values[last]); for (int i = last; i > pos; i--) values[i] = values[i - 1]; values[pos] = s;

See insert.cpp

Figure 7 Inserting an Element in an Ordered Vector

Quality Tip

Make Parallel Vectors into Vectors of Objects

Quality Tip

Make Parallel Vectors into Vectors of Objects — Part 2

Figure 9 Eliminating Parallel Vectors

Arrays

Defining and Using Arrays

Syntax Array Variable Definition

type_name variable_name[size];

Example:

int scores[20];

Purpose:

Define a new variable of an array type.

Defining and Using Arrays

Companion Variable

Defining and Using Arrays

Companion Variable

Defining and Using Arrays

Array Parameters

Array Parameters

Array Parameters

Array Parameters — salarray.cpp

Character Arrays

  • Arrays that hold chars
  • How C and C++ store strings (before the STL)
  • char type denotes an individual character
    • Delimited using single quotes
    • Actually a 1-byte integer
  • 'y' is different from "y"
  • An array that holds a string: char greeting[] = "Hello"; // Same as char greeting[6] = "Hello"
  • Last item is a sentinel, the null-terminator character
  • String constants ("Hello") are always null-terminated

Figure 10 A Character Array

Character Arrays

Character Arrays — append.cpp

Character Arrays

Two-Dimensional Arrays

  • Use a two-dimensional array (or matrix) to store tabular data
  • C++ uses an array with two subscripts (literally, an array of arrays) to store a matrix: const int POWERS_ROWS = 11; const int POWERS_COLS = 6; double powers[POWERS_ROWS][POWERS_COLS];
  • Individual elements are accessed by double subscripts m[i][j] powers[3][4] = future_value(1,000 6.5, 25);
  • Use separate brackets!
  • powers[3,4] will compile, but isn't what you want

Figure 11 Accessing an Element in a Two-Dimensional Array

Two-Dimensional Arrays

Filling in Data

Figure 12 A Two-Dimensional Array Is Stored as a Sequence of Rows

Two-Dimensional Arrays

Passing to Functions

Syntax : Two-Dimensional Array Definition

type_name variable_name[size1][size2];

Example:

double monthly_sales[NREGIONS][12];

Purpose:

Define a new variable that is a two-dimensional array.

Common Error

Omitting the Column Size of a Two-Dimensional Array Parameter