Professional Documents
Culture Documents
Week10 Ses12..
Week10 Ses12..
Vector - Pointer
VECTORS
n std::vector
n Vectors are like arrays that can change size as
your program runs
n Vectors, like arrays, have a base type
n To declare an empty vector with base type int:
vector<int> v;
Slide 8- 2
Accessing vector Elements
v[i] = 42;
Slide 8- 3
Initializing vector Elements
Slide 8- 4
The size Of A vector
Slide 8- 5
The Type unsigned int
changed to:
Slide 8- 6
Alternate vector Initialization
Slide 8- 7
The vector Library
#include <vector>
Slide 8- 8
vector Issues
independent copies
Slide 8- 9
vector Efficiency
n A vector's capacity is the number of elements
allocated in memory
n Accessible using the capacity( ) member function
vector
n More efficient than allocating smaller chunks of
memory
Slide 8- 10
Controlling vector Capacity
Slide 8- 11
Member functions
n Iterators:
n v.begin(), v.rbegin()
n v.end(), v.rend()
n Capacity:
n v.size(), v.max_size()
n v.resize(), v.empty()
n Modifiers:
n v.push_back(), v.pop_back()
Slide 8- 12
Overview
Pointers
Dynamic Arrays
Slide 9- 13
POINTER - Recap
n Operator *
n val = *ptr
n val = ptr
Slide 8- 14
Pointers
Slide 9- 15
Pointers Tell
Where To Find A Variable
Slide 9- 16
Declaring Pointers
double *p;
n The asterisk identifies p as a pointer variable
Slide 9- 17
Multiple Pointer Declarations
Slide 9- 18
The address of Operator
p1 is now a pointer to v1
v1 can be called v1 or "the variable pointed to
by p1"
Slide 9- 19
The Dereferencing Operator
n p is said to be dereferenced
Slide 9- 20
A Pointer Example
output:
42
42
Slide 9- 21
Pointer Assignment
Slide 9- 22
Caution! Pointer Assignments
Slide 9- 23
Slide 9- 24
The new Operator
p1 = new int;
n The new variable is referred to as *p1
Slide 9- 25
Dynamic Variables
is running
n Additional examples of pointers and dynamic
Slide 9- 26
Slide 9- 27
Slide 9- 28
new and Class Types
n Using operator new with class types calls
a constructor as well as allocating memory
n If MyType is a class type, then
Slide 9- 29
Basic Memory Management
Slide 9- 30
The delete Operator
delete p;
Slide 9- 31
Dangling Pointers
disasterous
Slide 9- 32
Automatic Variables
Slide 9- 33
Global Variables
Slide 9- 34
Type Definitions
Slide 9- 35
Defining Pointer Types
Slide 9- 36
Multiple Declarations Again
Slide 9- 37
Pointer Reference Parameters
Slide 9- 38
Dynamic Arrays
Slide 9- 39
Pointer Variables
and Array Variables
Slide 9- 40
Pointer Variables
As Array Variables
n Continuing the previous example:
Pointer variable p can be used as if it were an
array variable
n Example: p[0], p[1], …p[9]
are all legal ways to use p
n Variable a can be used as a pointer variable
except the pointer value in a cannot be changed
n This is not legal: IntPtr p2;
… // p2 is assigned a value
a = p2 // attempt to change a
Slide 9- 41
Slide 9- 42
Creating Dynamic Arrays
n Memory is wasted
n What if the programmer estimates too small?
n The program may not work in some situations
n Dynamic arrays can be created with just the
right size while the program is running
Slide 9- 43
Creating Dynamic Arrays
type double:
typedef double* DoublePtr;
DoublePtr d;
d = new double[10];
This could be an
integer variable!
n d can now be used as if it were an ordinary array!
Slide 9- 44
Dynamic Arrays (cont.)
n Pointer variable d is a pointer to d[0]
n When finished with the array, it should be
deleted to return memory to the freestore
n Example: delete [ ] d;
n The brackets tell C++ a dynamic array is being
deleted so it must check the size to know how many
indexed variables to remove
n Forgetting the brackets,
is not legal, but would tell
the computer to
remove only one variable
Slide 9- 45
Slide 9- 46
Slide 9- 47
Pointer Arithmetic (Optional)
Slide 9- 48
Pointer Arthmetic Operations
n You can add and subtract with pointers
n The ++ and - - operators can be used
IntArrayPtr *
int's
Slide 9- 51
Deleting
Multidimensional Arrays
on a previous slide:
for ( i = 0; i < 3; i++)
delete [ ] m[i]; //delete the arrays of 4 int's
delete [ ] m; // delete the array of IntArrayPtr's
Slide 9- 52
Slide 9- 53
Slide 9- 54
Declaring pointer
n char* ptr;
int main ()
{
int firstvalue = 5, secondvalue = 15;
n int* ptr;
int * p1, * p2;
n double* ptr;
*p1 = 10; // value pointed to by p1 = 10
*p2 = *p1; // value pointed to by p2 = value pointed to by p1
p1 = p2; // p1 = p2 (value of pointer is copied)
*p1 = 20; // value pointed to by p1 = 20
Slide 8- 55