Question: Write a Progam in C++. Using STL functions that take iterators as parameters: You will need the , , , and headers (plus maybe the

Write a Progam in C++.

Using STL functions that take iterators as parameters:

You will need the , , , and headers (plus maybe the and headers)

Declare an array of string and initialize it with some hard-coded values

Now declare a list and call the constructor that takes two iterators that define a range. Use the start and end addresses of the above array as arguments C++ can convert pointers to iterators automatically.

The value of an array variable is the start address

You can use pointer arithmetic to compute the address of the end of the array.

Use list's remove() method to remove one of the strings from the list

Use the STL for_each() function to print out the strings in the list

Write a void function that takes a string and prints it to the console output

Pass the begin() and end() methods of the list, plus this function (remember not to actually call the function just pass the name of the function)

Use the STL find_if() function to check if any of the strings in the list start with an 'E'

Write a function that has a string parameter and returns true if the string starts with an 'E', false if not

Use this as the 3rd parameter

To tell just whether there are any matches, compare the return value of find_if() with the end iterator as long as the two are not equal, the function found a match

Use the STL transform() function to fill a different list with the lengths of the strings

Declare a list of type size_t (for unsigned int, as is appropriate since the length of a string cannot be negative)

Important: allocate enough memory for this new list. Call its resize() method with the size of the list of strings.

Note that transform() requires not only the start and end iterators for the source (the list of string), but also the start iterator for the target (the list of size_t)

The 4th parameter is a function that takes a string and returns its length

Print out the lengths of the strings

Write your own function that uses iterators and functions. Write a function reduce() that takes the usual start and end iterators, an initial value and a function as parameters and produces a sum over the specified range of values

To get this to work, reduce() has to be a generic (templated) function. Look at the sample code on cplusplus.com and you can see examples

The function will need 3 type parameters inside the <>'s for the template part of the function header: the iterator type, the type of the values (for us, this will be int), and the type of the function parameter)

Make sure to use these identifiers for the parameter types and any appropriate local variables (you will presumably need an iterator for looping)

Remember to actually call the function parameter with the partial sum and the next value

Remember to dereference the iterator to get the actual next value

For computing the sum, initialize the result using the 3rd parameter (the initial value). Each time you iterate to the next item, add that in to the result using the function parameter.

Test your reduce() function for 2 cases:

Use it to add up the lengths in the result of the transform() function and print this sum. You can save a bit of time by calling the constructor of the STL plus class (in , though might already include it). The object that is created is appropriate for passing as the 4th parameter of the function call.

Use it to multiply the lengths and print this product. Yes, there is a multiplies class, too

Here is sample output:

Ben

Cam

Didi

Has names starting with E :false

Lengths of names:

3

3

4

Total # characters = 10

Product of lengths = 36

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!