Title: Arrays, Pointers and Structures
1Arrays, Pointers and Structures
2Pointers
- allow us to access something indirectly
- index of a book points to some topic in the body
of a book - street addresses in a telephone book point to
where some lives in the community - a forwarding address points to an address of
where some one can be reached (pointer to a
pointer)
3Arrays and Structures
- Arrays - ordered collections of objects, all of
the same type - int abc4
- Structure - a collection of objects of dissimilar
types - struct MyType int abc double def
4First Class Objects
- First Class Object
- can be manipulated in all of the usual ways
- are usually safer than Second Class Objects
- preserve value semantics
- obj2 obj1 (assignment)
- objtype a objtype b(a) (copy constructor)
- a b (overloaded assignment operators)
- usually implemented as a class
- can have bounds checking
5Second Class Objects
- Primitive types (arrays and structs)
- dont preserve value semantics
- are not as safe as First Class Objects
- no bounds checking on array indicies
6Standard template Library
- a standard set of templates and class templates
that provide a set of First Class types - was an extra in older compilers
- built into current generation of compilers
7vector
- vector is a template class that provides a First
Class version of the array primitive - vectorltintgt a(3)
- allocates a vector containing 3 integers
- vector methods
- size() - return of elements in the vector
- resize(n) - change the size of the vector to n
elements
8include ltstdlib.hgt include ltiostream.hgt include
ltvector.hgt using namespace std int main()
const int DIFFERENT_NUMBERS 100 int
totalNumbers cout ltlt How many numbers?
cin gtgt totalNumbers vectorltintgt
numbers(DIFFERENT_NUMBERS 1) for (int I0
I lt numbers.size() 1) numbersI 0
for (int j0 j lt totalNumbers j)
numbersrand DIFFERENT_NUMBERS 1
for (int k1 k lt DIFFERENT_NUMBERS k)
cout ltlt k ltlt occurs ltlt numbersk ltlt
times ltlt \n return 0
9(No Transcript)