7. Arrays and Vectors

Objectives

In this chapter you'll learn:

  • To use the array data structure to represent a set of related data items.

  • To use arrays to store, sort and search lists and tables of values.

  • To declare arrays, initialize arrays and refer to the individual elements of arrays.

  • To pass arrays to functions.

  • Basic searching and sorting techniques.

  • To declare and manipulate multidimensional arrays.

  • To use C++ Standard Library class template vector.

Now go, write it before them in a table, and note it in a book.

Isaiah 30:8

Begin at the beginning, . . . and go on till you come to the end: then stop.

Lewis Carroll

To go beyond is as wrong as to fall short.

Confucius

Outline

7.1 Introduction
7.2 Arrays
7.3 Declaring Arrays
7.4 Examples Using Arrays
  7.4.1 Declaring an Array and Using a Loop to Initialize the Array's Elements
  7.4.2 Initializing an Array in a Declaration with an Initializer List
  7.4.3 Specifying an Array's Size with a Constant Variable and Setting Array Elements with Calculations
  7.4.4 Summing the Elements of an Array
  7.4.5 Using Bar Charts to Display Array Data Graphically
  7.4.6 Using the Elements of an Array as Counters
  7.4.7 Using Arrays to Summarize Survey Results
  7.4.8 Using Character Arrays to Store and Manipulate Strings
  7.4.9 Static Local Arrays and Automatic Local Arrays
7.5 Passing Arrays to Functions
7.6 Case Study: Class GradeBook Using an Array to Store Grades
7.7 Searching Arrays with Linear Search
7.8 Sorting Arrays with Insertion Sort
7.9 Multidimensional Arrays
7.10 Case Study: Class GradeBook Using a Two-Dimensional Array
7.11 Introduction to C++ Standard Library Class Template vector
7.12 (Optional) Software Engineering Case Study: Collaboration Among Objects in the ATM System
7.13 Wrap-Up