Chapter 9 :
Chapter 9 :
List
Arrays and Vectors
Mechanism for representing lists

Key Concepts:
Key Concepts:
one-dimensional arrays
array subscripting
arrays as parameters
array element as parameters
character string
Standard Template Library (STL)
container class
template class
vector
vector subscripting
vector resizing
string subscripting
iterators
iterator dereferencing
vector of vector
sorting
function
InsertionSort()
function
QuickSort()
searching
function
BinarySearch()
algorithm library
function
find()
table
matrices
member initialization list
multidimensional arrays

Lists
Problem solving often requires information be viewed as a list
List may be one-dimensional or multidimensional
C++ provides two list mechanisms
Arrays
Traditional and important because of legacy libraries
Restrictions on its use
Container classes
First-class list representation
Common containers provided by STL
vector
,
queue
,
stack
,
map
, …
Preferred long-term programming practice
Analogies
Egg carton
Apartments
Cassette carrier

Array
Traditional and important because
of legacy libraries

Basic Array Definition
BaseType ArrayName[ SizeExp ];
Bracketed constant
Expression
indicating number
of elements in list
Type of
values in
list
Name
of list
double
X[100];

Array Terminology
List is composed of
elements
Elements in a list have a
common name
The list as a whole is referenced through the
common name
List elements are of the same type — the
base
type
Elements of a list are referenced by
subscripting
or
indexing
the common name
double
X[100];
double
a = X[10];

Array Terminology
Subscripts
are denoted as expressions within brackets: [ ]
Base type
can be any fundamental, library-defined, or
programmer-defined type
The
index
type is integer and the
index
range must be
0 ...
n
-1
where
n
is the size of array.
Parameter passing style
Always call by reference (no indication necessary)
double
X[100];
// Subscripts are 0 through 99
double
a = X[100]; // What happen?

// array of 10 ints
char
B[MaxStringSize];
// array of 80 chars
double
C[M*N];
// array of 800 floats
int
Values[MaxListSize];
// array of 1000 ints
Rational
D[N-15];
// array of 5 Rationals

Subscripting
Suppose
int
A[10];
// array of 10 ints A[0], … A[9]
To access individual element must apply a subscript to list name
A
A subscript is a bracketed expression also known as the index
First element of list has index 0
A[0]
Second element of list has index 1, and so on
A[1]
Last element has an index one less than the size of the list
A[9]
Incorrect indexing is a common error
A[10]
// does not exist

Array Elements
Suppose
int
A[10];
// array of 10 uninitialized ints
A
A[4]A[5] A[6]
A[3]
A[0]
A[2]
A[8] A[9]
A[7]
A[1]
--
--
--
--
--
--
--
--
--
--
To access an individual element we must apply a subscript to list
name
A

Array Element Manipulation
Consider
int i = 7, j = 2, k = 4;
A[0] = 1;
A[j] = A[i] + 3;
A[i] = 5;
A[j+1] = A[i] + A[0];
cin >> A[k];
// where suppose next input value is 3
A[A[j]] = 12;
A
A[4]A[5] A[6]
A[3]
A[0]
A[2]
A[8] A[9]
A[7]
A[1]
--
--
--
--
1
5
8
6
12
3

#### You've reached the end of your free preview.

Want to read all 105 pages?

- Winter '03
- Pro.Lei
- C++ Programming, Array, STL