BIG C++ Cay Horstmann & Timothy Budd
Laboratory Notebook Chapter 24 – The Standard Template Library – Iterators and Algorithms
Your name:
Your email address:
Your student ID number:
Once this form has been customized for your institution, you can use this button to send your lab work. Be sure to read the instructions before starting your work.
To gain experience with
· understanding how iterators generalize the idea of pointers
· learning the various categories of iterators and the operations they provide
· understanding functions, generators, and predicates and how these can be generalized using function objects
· exploring the library of generic algorithms
· learning how generic algorithms can be extended through the use of binders, negators, and adapters
· exploring how stream iterators can be used to permit generic algorithms to work with external files
P1. One for All
Many functions in the STL require other functions as parameters. Practice in writing such code will help to ensure your understanding of the topic. Assume you work for a credit agency whose policy has always been to require bi-monthly payments from your customers. You have observed that customers are forgetting to make their payments. Therefore, you have been charged with the task of converting all payments over to an equivalent monthly payment format. You already have code that supplies you with a vector of all customer bi-monthly payment amounts. Consequently, you are instructed to write two small functions. The first simply divides the number passed to it by two. The definition is provided below. The second is a template function named forAll(). This function is also defined below and should perform the specified function, represented by action, on every element within the vector.
void divideBy2(double& number)
{
// Insert code here
}
template<typename Action>
forAll(vector<double>, Action action)
Submit all of the code for the divideBy2 function in the space below.
Submit all of the code for the forAll() function in the space below.
R1. Common Error
The following code illustrates one of the most common types of errors associated with iterators. Debug the code and explain the error.
list<float> debits;
list<float> credits;
list<float>::iterator i1 = debits.begin();
list<float>::iterator i2 = credits.end();
float total = 0;
while (i1 != i2)
total += credits;
++p;
i1 = debits.begin();
total = total - debits;
R2. Predicates
Assume that the credit card company evaluates a person’s credit worthiness based in part on their monthly income. Write a predicate function that accepts two parameters. One is the annual income of the customer and the other is his/her monthly payments to your credit agency. The predicate should return true if the monthly income of the customer is at least four times higher than the monthly payment. Otherwise, return false.
P3. Output Stream Iterators
Suppose that your United States based company is encountering illegal aliens trying to apply for credit. You decide that your business now requires applicants to supply a valid social security number (nine digits). Write a program that:
Use a simple cin statement to read the number from the standard input.
Insert all code in the space below.
R3. Function Objects
Modify the program in Problem P3 such that the social security number is read using an input stream iterator instead of with the simple cin statement used previously.
R4. Generators
Assume you have lost so much money with your business that you now have to file for bankruptcy. Your employees are furious and are demanding your telephone number so that their lawyers can contact you. You are planning to flee the country and would like to stall the legal process until you are out safely. Write a generator function for generating a random seven-digit phone number. This function will be used to provide unique phone numbers to each of the former employees who request it.
Provide your answers in the space below.
Do not forget to send your answers when you are finished.