SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Standard Template Library
Exercise:
Programming Projects
Question:2 | ISBN:9780132846813 | Edition: 5

Question

This problem intends to illustrate removal of several instances of a particular item from a container with the remove generic function. A side effect is to examine the behavior of the generic function remove that comes with your compiler. (We have observed some variation in the behavior of remove from one compiler to another.) Before you start, look up the behavior of the remove generic algorithm as described by your favorite STL document or Web site. (For example, point your browser at http://www.sgi.com/tech/stl/remove.html . This worked as of the publication date.)

a. Modify the array declaration in Programming Project 19.1 to include several elements with the same value (say, 4.0, but you can use any value for this exercise). Make sure that some of these are not all together. Use the modifying generic function remove (see Display 19.19 ) to remove all elements 4.0 (that is, the value you duplicated in building the array). Test.

b. Use the array of double from part a to build a list and a vector that have the same contents as the array. Do this using the container constructor that takes two iterators as arguments. The vector and list classes each have a constructor that takes two iterators as arguments and initializes the vector or list to the items in the iterator interval. The iterators can be located in any container, including in an array. Build the vector and list using the array name for the begin iterator and the name + array length as the end iterator for the two constructor arguments. Use the modifying algorithm remove ( Display 19.19 ) to remove from the list and from the vector all elements equal to 4.0 (or the value you duplicated in building the array). Display the contents of the vector and list and explain the results.

c. Modify the code from part b to assign to an iterator variable of appropriate type the iterator value returned by the call to the remove generic function. Review the documentation for remove to be certain you know what these iterator values mean. Output the contents of the array, the vector and the list , begin( ) to end( ) , using the “begin” and “end” we described previously for the array. Output the contents of the two containers starting at the iterator returned from remove to the end() of each container. Explain your results.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Sorry the answer is not available at the moment…

If you are able to find the answer, please make sure to post it here. So that your Juniors have smile on their lips and feel happy.

Spread the 'tradition of sharing'.