![Page 1: 12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022062305/5697bfe81a28abf838cb6657/html5/thumbnails/1.jpg)
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
1
Engineering Problem Solving with C++, Etter/Ingber
Chapter 9
An Introduction to Pointers
![Page 2: 12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022062305/5697bfe81a28abf838cb6657/html5/thumbnails/2.jpg)
An Introduction to Pointers
Addresses and Pointers Pointers to Array Elements Dynamic Memory Allocation Data Structures and the STL.
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
2
![Page 3: 12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022062305/5697bfe81a28abf838cb6657/html5/thumbnails/3.jpg)
ADDRESSES AND POINTERS
address operator
pointer assignment
pointer arithmetic
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
3
![Page 4: 12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022062305/5697bfe81a28abf838cb6657/html5/thumbnails/4.jpg)
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
4
Addresses and Pointers A pointer is an object that holds the memory
address of another object. If a variable p contains the address of
another variable q, then p is said to point to q.
If q is a variable at location 100 in memory, then p would have the value 100 (q’s address).– Memory snapshot:
qp
![Page 5: 12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022062305/5697bfe81a28abf838cb6657/html5/thumbnails/5.jpg)
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
5
Address Operator The operator & is called the address operator. When the & operator is applied to an object, the
result is the address of the object. Example: int x=75;cout << "x is " << x;cout << "\nthe addres of x is " << &x;
75x [0x7fff8164]
OUTPUT:x is 75the address of x is 0x7fff8164
![Page 6: 12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022062305/5697bfe81a28abf838cb6657/html5/thumbnails/6.jpg)
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
6
Pointer Assignment
Pointer types are declared using the pointer operator *, also called the dereferencing operator.
Syntax - – type *variable_name, *variable_name;– type* variable_name;
When declaring more than one pointer variable, the * operator must precede each identifier.
![Page 7: 12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022062305/5697bfe81a28abf838cb6657/html5/thumbnails/7.jpg)
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
7
Example
int *iPtr;double* dPtr;
The variable iPtr is declared to be of type pointer to int.
The variable dPtr is declared to be of type pointer to double.
Neither variable in this example has been initialized.
iPtr dPtr? ?
![Page 8: 12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022062305/5697bfe81a28abf838cb6657/html5/thumbnails/8.jpg)
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
8
Example
int *iPtr, i=6;char* s, str[] = "example";double *dPtr, d=1.25;
iPtr
s
dPtr
6
"example"
1.25
i
str
d
![Page 9: 12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022062305/5697bfe81a28abf838cb6657/html5/thumbnails/9.jpg)
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
9
Initialization and Assignment
Pointer types may be initialized at the time they are declared.
Pointer types may be assigned new values using the assignment operator.
![Page 10: 12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022062305/5697bfe81a28abf838cb6657/html5/thumbnails/10.jpg)
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
10
Example – Initial Pointers to NULL
int *iPtr=0;
char *s=NULL; //predefined constant in iostream
double *dPtr=NULL;
iPtr
s
dPtr
![Page 11: 12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022062305/5697bfe81a28abf838cb6657/html5/thumbnails/11.jpg)
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
11
Assignment
The assignment operator (=) is defined for pointers of the same base type.
The right operand of the assignment operator can be any expression that evaluates to the same type as the left operand.
Example:int x, *xp, *ip;xp = &x;ip = xp;
x
xp
ip
![Page 12: 12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022062305/5697bfe81a28abf838cb6657/html5/thumbnails/12.jpg)
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
12
The Base Type
The base type of a pointer refers to the type of object the pointer is referencing.
The base type of a pointer defines the size of the object the pointer is referencing.
The size of a pointer is independent of its base type. – p and q are the same ( 4 bytes**), but p points to 4
bytes** and q points to 8 bytes** **compiler dependent
![Page 13: 12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022062305/5697bfe81a28abf838cb6657/html5/thumbnails/13.jpg)
Example:
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
13
int p(5), *iPtr=&p;double q, *dPtr=&q;
dPtr
p
q
5
??
iPtr
sizeof p is 4sizeof q is 8sizeof iPtr is 4sizeof dPtr is 4
![Page 14: 12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022062305/5697bfe81a28abf838cb6657/html5/thumbnails/14.jpg)
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
14
Base Type
A pointers base type determines how the object referenced by the pointer will be interpreted.
The declaration:int *p;declares p to be a pointer to int. What ever p points to will be interpreted as an int, ie 4 bytes.
Base type also defines pointer arithmetic.
![Page 15: 12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022062305/5697bfe81a28abf838cb6657/html5/thumbnails/15.jpg)
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
15
Pointer Arithmetic
Four arithmetic operations are supported:+, -, ++, --
Arithmetic is performed relative to the base type of the pointer.
When applied to pointers, ++ means increment pointer to point to next object.
Example: p++;– if p is defined as int *p, p will be incremented by
4 (bytes).– if p is defined as double *p, p will be
incremented by 8(bytes).
![Page 16: 12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022062305/5697bfe81a28abf838cb6657/html5/thumbnails/16.jpg)
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
16
Exampleint *p;cout << "size of char is " << sizeof(char) << endl;cout << "size of int is " << sizeof(int) << endl;cout << "size of double is " << sizeof(double) << endl;cout << "size of float is " << sizeof(float) << endl;cout << "the size of p is " << sizeof(p) << endl;
Output:size of char is 1size of int is 4size of double is 8size of float is 4the size of p is 4
![Page 17: 12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022062305/5697bfe81a28abf838cb6657/html5/thumbnails/17.jpg)
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
17
Practice! int q=6;int *iPtr = &q;cout << "iPtr is " << iPtr << endl;cout << "*iPtr is " << *iPtr << endl;cout << "++*iPtr, is " << ++*iPtr << endl;cout << "q is " << q << endl;cout << "iPtr is " << iPtr << endl;cout << "*iPtr++ is " << *iPtr++ << endl;cout << "iPtr is " << iPtr << endl;cout << "q is " << q << endl;
Complete the output:iPtr is 0x7fff2f14
![Page 18: 12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022062305/5697bfe81a28abf838cb6657/html5/thumbnails/18.jpg)
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
18
Result of Practice
iPtr is 0x7fff2f14*iPtr is 6++*iPtr is 7q is 7iPtr is 0x7fff2f14*iPtr++ is 7iPtr is 0x7fff2f18q is 7
![Page 19: 12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022062305/5697bfe81a28abf838cb6657/html5/thumbnails/19.jpg)
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
19
Comparing Pointers
You may compare pointers using relational operators
Common comparisons are:– check for null pointer (p == NULL)– Note: since NULL evaluates as false, and any
other pointer evaluates as true, checking for a null pointer can be done as (!p)
– check if two pointers are pointing to the same object
(p == q) – Note: (*p == *q) means they are pointing to
equivalent, but not necessarily the same data.
![Page 20: 12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022062305/5697bfe81a28abf838cb6657/html5/thumbnails/20.jpg)
POINTERS TO ARRAY ELEMENTS
one-dimensional arrays
pointers as arguments to functions
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
20
![Page 21: 12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022062305/5697bfe81a28abf838cb6657/html5/thumbnails/21.jpg)
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
21
One Dimensional Arrays The name of an array is the address of the first
element (i.e. a pointer to the first element). Arrays and pointers may often be used
interchangeably.Example
int num[4] = {1,2,3,4}, *p;p = num; //same as p = &num[0];cout << *p <<endl;++p;cout << *p;
output:12
![Page 22: 12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022062305/5697bfe81a28abf838cb6657/html5/thumbnails/22.jpg)
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
22
Arrays and Pointers You can index a pointer using [] operator.Example:
char myString[] = "This is a string"; char *str;str = myString;for(int i =0; str[i]; i++) //look for nullcout << str[i];
What does this segment print?
This is a string
![Page 23: 12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022062305/5697bfe81a28abf838cb6657/html5/thumbnails/23.jpg)
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
23
Arrays and Pointers When an array is defined, memory is allocated
according to the specified size of the array. The name of an array is a pointer to the first
element. However, the value of the pointer can not be changed. It will always point to the same memory location.
When a pointer is defined, 4 bytes* are allocated to store a memory address.
The value assigned to a pointer can be modified (reassigned to point to a different object).
![Page 24: 12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022062305/5697bfe81a28abf838cb6657/html5/thumbnails/24.jpg)
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
24
Practice! What does this print?char myString[ ] = "This is a string";char *strPtr;strPtr = myString;cout << *myString << endl; cout<<myString << endl; cout << *(myString + 1) << endl;strPtr++;cout << *++strPtr << endl;myString++; //not legal
0xfff4c252strPtr
TThis is a stringhi
0xfff4c252myString T h i s i s a s t r i n g \0
![Page 25: 12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022062305/5697bfe81a28abf838cb6657/html5/thumbnails/25.jpg)
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
25
Arrays of Pointers
You may define arrays of pointers like any other data type in C++
int num=8;//declare an array of 10 pointers to intint *iPtrs[10];//first element is assigned a valueiPtrs[0] = # //output the value of numcout << *iPtrs[0];
![Page 26: 12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022062305/5697bfe81a28abf838cb6657/html5/thumbnails/26.jpg)
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
26
Pointers As Arguments to Functions
Pointers may be passed either "by value" or "by reference".
In either case, the pointer can be used to modify the object to which it is pointing.
Only when passed by reference can the pointer argument itself be modified.
![Page 27: 12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022062305/5697bfe81a28abf838cb6657/html5/thumbnails/27.jpg)
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
27
Common Pointer Problems
Using uninitialized pointersint *iPtr;*iPtr = 100;iPtr has not been initialized. The value 100 will
be assigned to some memory location. Which one determines the error.
Failing to reset a pointer after altering it’s value.
Incorrect/unintended syntax.
![Page 28: 12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022062305/5697bfe81a28abf838cb6657/html5/thumbnails/28.jpg)
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
28
#include <iostream>int main(){ char *aString = "What happens here?"; int len=0; while(*aString++ != '\0')
len++;std::cout << len << ": " << aString;return 0;
}
Does this compile? If not, why?If it does, what is the output? Explain?
Practice!
Yes18:
![Page 29: 12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022062305/5697bfe81a28abf838cb6657/html5/thumbnails/29.jpg)
DYNAMIC MEMORY ALLOCATION
new
delete
dynamically allocated arrays
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
29
![Page 30: 12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022062305/5697bfe81a28abf838cb6657/html5/thumbnails/30.jpg)
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
30
Dynamic Allocation Using new and delete
Storage for data is allocated as needed from the free memory area known as the heap.
Run-time stack– Local variables– Formal parameters– Managed by the compiler
Heap– Dynamic storage– Managed by storage allocator
Stack
Heap
Global data
Program code
![Page 31: 12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022062305/5697bfe81a28abf838cb6657/html5/thumbnails/31.jpg)
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
31
Dynamic Memory Allocation
Dynamically allocated memory is defined at runtime.
Dynamic allocation of memory allows for more efficient use of a finite resource.
Dynamic allocation is often used to support dynamic data structures such as stacks, queues, linked lists and binary trees.
Dynamically allocated memory should be freed during execution when it is no longer needed.
![Page 32: 12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022062305/5697bfe81a28abf838cb6657/html5/thumbnails/32.jpg)
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
32
Operator new Replaces malloc() used in C Allocates a block of memory from the heap. Returns the address of the first byte. If allocation fails, new throws and exception that
terminates the program (or returns NULL on older compilers).
![Page 33: 12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022062305/5697bfe81a28abf838cb6657/html5/thumbnails/33.jpg)
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
33
Exampleint *iPtr;iPtr = new int; // 4 bytes are allocated
// iPtr points to 1st bytedouble *dPtr;dPtr = new double[20]; // 160 bytes allocated
// dPtr points to 1st byte ?
?
?
?
?
…
?
?
heap
iPtr
dPtr
![Page 34: 12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022062305/5697bfe81a28abf838cb6657/html5/thumbnails/34.jpg)
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
34
Initializing Dynamically Allocated Memory To initialize a dynamically allocated object, the
initial value is provided inside parentheses following the type.
Example:
int *ptr;ptr = new int(100); //4 bytes allocated
//initial value: 100
![Page 35: 12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022062305/5697bfe81a28abf838cb6657/html5/thumbnails/35.jpg)
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
35
int main(){
int *iPtr, *jPtr, i;iPtr = new int;jPtr = new int(3);double *dPtr;dPtr = new double[6];*iPtr = 7;cout << *iPtr << ',' << *jPtr << endl;for(i=0; i<6; i++)
dPtr[i] = 5;for(i=0; i<6; i++)
cout << (*dPtr)++ << ' ';cout << endl;for(i=0; i<6; i++)
cout << dPtr[i] << ' ';return 0;
}
OUTPUT7 , 35 6 7 8 9 1011 5 5 5 5 5
EXPLAIN
Practice:
![Page 36: 12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022062305/5697bfe81a28abf838cb6657/html5/thumbnails/36.jpg)
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
36
Operator delete
Replaces free() used in C. The delete operator frees memory
allocated by new. Using delete to attempt to free any other
type of address will result in errors.
![Page 37: 12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022062305/5697bfe81a28abf838cb6657/html5/thumbnails/37.jpg)
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
37
delete Exampleint *ptr;
ptr = new int (100);
cout << *ptr; //see if it worked
value of ptr is now undefined
100ptr
delete ptr; //free the memory
?ptr
![Page 38: 12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022062305/5697bfe81a28abf838cb6657/html5/thumbnails/38.jpg)
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
38
Example: Dynamically Allocated Arrays
double *dptr;
const int SIZE = 10;
dptr = new double[SIZE]; //80 bytes
for(int i=0; i<SIZE; ++i)
cin >> dptr[i];
fun1(dptr, SIZE); // pass array to fun1
delete [] dptr; //free all 10 elements
![Page 39: 12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022062305/5697bfe81a28abf838cb6657/html5/thumbnails/39.jpg)
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
39
Example using deleteint main(){
int *iPtr, *jPtr;iPtr = new int(2);jPtr = new int(3);cout << *iPtr << ',' << *jPtr << endl;delete iPtr;delete jPtr;cout << *iPtr << ',' << *jPtr << endl;int *dPtr;dPtr = new int(5);cout << iPtr << ',' << jPtr << endl;cout << *iPtr << ',' << *jPtr << endl;cout << dPtr << endl;return;
} What output would you expect from this code?
![Page 40: 12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022062305/5697bfe81a28abf838cb6657/html5/thumbnails/40.jpg)
DATA STRUCTURES AND THE STL
list
stack
queue
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
40
![Page 41: 12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022062305/5697bfe81a28abf838cb6657/html5/thumbnails/41.jpg)
List A list is a data structure organized as a
collection of elements, or nodes, that are linked by pointers.
Elements can be added at any position in a list in constant time by reassigning pointers.
List have many useful applications in the organization large amounts of data.
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
41
![Page 42: 12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022062305/5697bfe81a28abf838cb6657/html5/thumbnails/42.jpg)
The list class
list is a class template defined in the header file list.
#include<list>
list<string> word; //list of strings
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
42
![Page 43: 12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022062305/5697bfe81a28abf838cb6657/html5/thumbnails/43.jpg)
list Methodso Elements can be added to and removed from any position in
a list using the appropriate method and an iterator. o An iterator is similar to a pointer, but is usually
implemented as a class object.o Common Methods:
bool empty()
iterator insert()
iterator remove()
iterator begin()
iterator end()
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
43
![Page 44: 12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022062305/5697bfe81a28abf838cb6657/html5/thumbnails/44.jpg)
Examplelist<string> wordList;
list<string>::iterator iter =
wordList.begin();
iter = wordList.insert(iter,"hello");
wordList.insert(iter,"world");
for(iter=wordList.begin(); iter!=
wordList.end(); iter++)
cout << *iter << " ";
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
44
output:hello world
![Page 45: 12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022062305/5697bfe81a28abf838cb6657/html5/thumbnails/45.jpg)
Queue
A queue is known as a first-in-first-out (FIFO) data structure.
Items are added to the end of a queue and removed from the front of the queue.
Queues have many useful applications, including processing requests.
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
45
![Page 46: 12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022062305/5697bfe81a28abf838cb6657/html5/thumbnails/46.jpg)
The queue class
queue is a class template defined in the header file queue.
#include<queue>
queue<int> printIDs; //queue of ints
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
46
![Page 47: 12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022062305/5697bfe81a28abf838cb6657/html5/thumbnails/47.jpg)
queue Methods
bool empty()
void pop()//remove from front
void push()//add to end
data-type front()//return front
data-type back()//return end
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
47
![Page 48: 12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022062305/5697bfe81a28abf838cb6657/html5/thumbnails/48.jpg)
Example
#include<queue>
…
queue<int> theQueue;
theQueue.push(10);
theQueue.push(20);
cout << theQueue.front(); //output 10
cout << theQueue.back(); //output 20
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
48
![Page 49: 12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022062305/5697bfe81a28abf838cb6657/html5/thumbnails/49.jpg)
Stack
A stack is known as a last-in-first-out (LIFO) data structure.
Items are added to and removed from the top of the stack.
Stacks are essential in the design of compilers and for implementing recursion.
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
49
![Page 50: 12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022062305/5697bfe81a28abf838cb6657/html5/thumbnails/50.jpg)
The stack class
stack is a class template defined in the header file stack.
#include<stack>
stack<int> theStack; //stack of ints
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
50
![Page 51: 12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022062305/5697bfe81a28abf838cb6657/html5/thumbnails/51.jpg)
stack Methods
bool empty()
void pop() //remove from top
void push() //add to top
data-type top()//return top
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
51
![Page 52: 12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction](https://reader035.vdocuments.mx/reader035/viewer/2022062305/5697bfe81a28abf838cb6657/html5/thumbnails/52.jpg)
Example
#include<stack>
…
stack<int> theStack;
theStack.push(10);
theStack.push(20);
cout << theStack.top(); //output 20
theStack.pop();
cout << theStack.top(); //output 10
04/21/23 Engineering Problem Solving with C++, second edition, J. Ingber
52