Problem

Write a program that illustrates how to use the functions replace, replace_if, replace_cop...

Write a program that illustrates how to use the functions replace, replace_if, replace_copy, and replace_copy_if. Your program must use the function lessThanEqualTo50, as shown in Example.

EXAMPLE

Consider the following statements:

char cList[10] = {‘A’, ‘a’, ‘A’, ‘B’, ’A’,‘c’, ‘D’, ‘e’, ‘F’, ‘A’}; //Line 1vector charList(cList, cList + 10); //Line 2

After the statement in Line 2 executes, the vector container charList is as follows:

charList = {‘A’, ‘a’, ‘A’, ‘B’, ‘A’, ‘c’,‘D’, ‘e’, ‘F’, ‘A’} //Line 3

Now consider the following statement:

replace(charList.begin(), charList.end(), ‘A’, ‘Z’); //Line 4

This statement uses the function replaceto replace all the occurrences of ‘A’ with  ‘Z’ in charList. After this statement executes, charListis as follows:

charList ={‘Z’, ‘a’, ‘Z’, ‘B’, ‘Z’, ‘c’, ‘D’,‘e’, ‘F’, ‘Z’} //Line 5

Now consider the following statement:

replace_if(charList.begin(), charList.end(), isupper, ‘*’); //Line 6

This statement uses the function replace_ifto replace the uppercase letters with  ‘*’ in the list charList. After this statement executes, charListis as follows:

charList ={‘*’, ‘a’, ‘*’, ‘*’, ‘*’, ‘c’, ‘*’,‘e’, ‘*’, ‘*’} //Line 7

Next suppose that you have the following statements:

int list[10] = {12, 34, 56, 21, 34, 78, 34, 55, 12, 25}; //Line 8 vector intList (list, list + 10); //Line 9vector temp(10); //Line 10

The statement in Line 9 creates a vector, intList, of type intand initializes intListusing the array list, created in Line 8. After the statement in Line 9 executes, intListis as follows:

intList = {12, 34, 56, 21, 34, 78, 34, 55, 12, 25}

The statement in Line 10 declares a vector temp of type int. Next consider the following statement:

replace_copy(intList.begin(), intList.end(),temp1.begin(), 34, 0); //Line 11

This statement copies all the elements of intListand replaces 34with 0. The list intListis not modified. After this statement executes, tempis as follows:

temp = {12, 0, 56, 21, 0, 78, 0, 55, 12, 25}

Next, suppose that you have the following function definition:

bool lessThanEqualTo50(int num) //Line 12{return (num<=50); }

The function lessThanEqualTo50 returns true if num is less than or equal to 50, otherwise it returns false. Consider the following statement:

replace_copy_if(intList.begin(), intList.end(),temp.begin(), lessThanEqualTo50, 50); //Line 13

This statement uses the function replace_copy_if to copy the elements of intList into temp and replaces all the elements less than 50 with 50. Notice that the fourth parameter of the function replace_copy_if is the function lessThanEqualTo50. After the statement in Line 13 executes, temp is as follows:

temp = {50, 50, 56, 50, 50, 78, 50, 55, 50, 50}

We leave it as an exercise for you to write a program that further illustrates how to use the functions replace, replace_if, replace_copy, and replace_copy_if; see Programming Exercise 3 at the end of this chapter.

Step-by-Step Solution

Request Professional Solution

Request Solution!

We need at least 10 more requests to produce the solution.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the solution will be notified once they are available.
Add your Solution
Textbook Solutions and Answers Search
Solutions For Problems in Chapter 13
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT