Professional Documents
Culture Documents
Fall 2013
1/40
......
ch
name
"All"
......
......
231 2
231 1
Fall 2013
2/40
Pointers
P (pointer)
12316
12316 (address)
Typed object
Pointer properties:
A pointer value is the address of the first byte of the pointed
object in the memory.
A pointer does not know about how many bytes it points to.
Fall 2013
3/40
Pointers (cont)
Fall 2013
4/40
Arrays
An array of the fix size represents a contiguous sequence of
objects of the same type allocated on the stack computer
memory. It is a very simple data structure often used in sorting
and searching operations.
The position of an element in the array is called the index. In
C++ arrays always begin with the index 0:
0
12
-3
24
65
92
11
(indices)
(array values)
Fall 2013
5/40
Arrays (cont.)
The total number of items inserted into an array is called the
logical size of the array. A special variable must be used to
keep track of the current number of items.
The logical size should be not greater than the physical size of
an array (= maximum size of an array).
Operations on arrays
An array provides a random access to its elements:
array[index] where 0 <= index < max_size.
C++ does not check the index-out-of-bounds condition and
it is a programmers responsibility to ensure that the index
range is valid. It is easy to go out of range, e.g., there is no
array item such as array[max_size].
There is no possibility of resizing this type of arrays.
Fall 2013
6/40
Arrays (cont.)
Fall 2013
7/40
Arrays (cont.)
Copying:
for (int i = 0; i < 100; i++)
other_array[i] = array[i];
// assigning other_array = array; is illegal
Fall 2013
8/40
Arrays (cont.)
Fall 2013
9/40
Enumeration Type
Fall 2013
10/40
Fall 2013
11/40
Example
// we want to copy elements of the array A to B
int A[10], B[10];
... // insert values to A
copy_elems(A, 10, B, 10); // copy A to B
Fall 2013
12/40
Dynamic Arrays
int* a = new int[3];// with elements 10, 20, 30
int* aptr = a; // aptr is an alias to a
It allocates 3 contiguous memory locations on the heap (free store)
part of memory, each of type int and stores the address of the first
memory location in a. Note that the pointer variable a is kept on the
stack part of memory.
heap
stack
...
a=1000
10
20
30
200
1000
1001
1002
...
(addresses)
Fall 2013
13/40
Memory Deallocation
Deallocation of memory allocated by the operator new:
delete [] a; // now a is a dangling pointer
a = nullptr;
Fall 2013
14/40
When an array cannot hold more data, increase its size by creating a
new larger array (usually by doubling its size).
Copy data from the old array to the new larger one.
Set the array variable to point to the new array. Deallocate the old
array.
Fall 2013
15/40
Resizing Array
Resizing takes several steps
// resize an array
int* resize_array(int *array, int capacity)
{
int new_capacity = 2*capacity;
int new_array[] = new int[new_capacity];
for (int i = 0; i < capacity; i++)
new_array[i] = array[i];
delete [] array; // remove old contents
return new_array;
}
... // in main()
int *A = new int[10];
... // populate A
A = resize_array(A, 10); // A is overwritten
Fall 2013
16/40
Advantages of arrays
Direct mapping to hardware
Efficient for low-level operations
Direct language support
Fall 2013
17/40
Fall 2013
18/40
elements
heap
0 1 r
Fall 2013
19/40
v_rep
v_rep
heap
elements
0 1
v
r
Fall 2013
20/40
v_rep
v_rep
heap
elements
v
r
0 1
n
n
r
r
Fall 2013
21/40
Applications of Vectors
Direct applications
Sorted collection of objects (elementary database)
Indirect applications
Auxiliary data structure for algorithms
Building blocks for other data structures
Fall 2013
22/40
Fall 2013
23/40
Fall 2013
24/40
Iterators
Fall 2013
25/40
Iterators (cont)
Iterator ADT
bool hasNext() { return p!=end(); }
iterator next() { return ++p; }
void reset() { p=begin(); }
Fall 2013
26/40
Iterators (cont)
Fall 2013
27/40
Two-Dimensional Arrays
A multidimensional array is called a matrix.
One-dimensional array can be considered as a 1 by n matrix.
Two-dimensional array m n consists of m rows and n
columns. Notice that every row is itself an array of n elements.
Example
A 2 3 matrix
a00
a10
a01
a11
a02
a12
Fall 2013
28/40
Fall 2013
29/40
Fall 2013
30/40
Initializing an array
for (int row = 0; row < max_rows; row++) {
for (int col = 0; col < max_cols; col++) {
array[row][col] = row+col;
}
}
Fall 2013
31/40
A Class Matrix
A Matrix is not a built-in type, but a user-defined class in
C++.
It is possible to define a matrix class as vector<vector<T>>
where T is any type in C++ but typically T=int or T=double.
There is a Matrix class written in the textbook Programming
Principles and Pratice Using C++, chapter 24.
#include "Matrix.h"
using namespace Numeric_lib;
int main()
{ // initialization is available only in C++11
Matrix<double,2> m2 { // 2-dim matrix (3x4)
{0,1,2,3}, //row 0
{4,5,6,7}, //row 1
{8,9,10,11} //row 2
};
cout << matrix elem(1,2)= << m2(1,2) << endl;
}
Teresa Leyk (CSCE 221)
Fall 2013
32/40
Position ADT
The Position ADT models the notion of a place within a data
structure where a single element is stored.
A special null position refers to no element.
The Position ADT provides a unified view of diverse ways of
storing data, such as
a cell of an array
a node of a linked list
Fall 2013
33/40
List ADT
The List ADT models a sequence of positions storing arbitrary
elements.
It establishes a before/after relation between positions.
Generic methods: size(), is_empty()
Query methods (p = position): is_first(p), is_last(p)
Accessor methods:
first(), last(),
before(p), after(p)
Fall 2013
34/40
List Implementations
APPLE
Stack
Memory
BANANA
ORANGE
PEACH
Heap Memory
trailer
APPLE
BANANA
ORANGE
PEACH
Fall 2013
35/40
Fall 2013
36/40
Sequence ADT
The Sequence ADT is the union of the Vector and List ADTs
Elements accessed by
rank, or
position
List-based methods:
first(), last(), before(p), after(p),
replace_element(p, e), swap_elements(p, q),
insert_before(p, e), insert_after(p, e),
insert_first(e), insert_last(e), remove(p)
Fall 2013
37/40
Applications of Sequences
Indirect applications:
Building block of more complex data structures
Fall 2013
38/40
Array-Based Implementation
We use a circular array for storing positions
A position object stores:
Element
Rank
Fall 2013
39/40
Sequence Implementations
Operation
size, is_empty
at_rank, rank_of, elem_at_rank
first, last, before, after
replace_element, swap_elements
replace_at_rank
insert_at_rank, remove_at_rank
insert_first, insert_last
insert_after, insert_before
remove
Array
1
1
1
1
1
n
1
n
n
List
1
n
1
1
n
n
1
1
1
Fall 2013
40/40