The point of this exercise is to demonstrate that an object that behaves like an iterator is an iterator. More precisely, if an object accesses some container and behaves like an iterator of a particular strength, then that object can be used as an iterator to manipulate the container with any of the generic functions that require an iterator having that strength. However, while the generic algorithms can be used with this container, the member functions, such as begin and end, will (of course) not be present (for example, in an array) unless they have been explicitly coded for that container. We will restrict this exercise to arrays of double, but the same message would hold true for any base type.
a. Argue from the properties of random-access iterators that a pointer that points to an element of an array behaves exactly as a random-access iterator.
b. Argue further that
i) the name of the array is a pointer to double that points to the first
element and so the array name can serve as a “begin” iterator and
ii) (the array’s name) + (the size of the array) can serve as an “end”
pointer. (Of course, this points one past the end, as it should.)
c. Write a short program in which you declare an array of double of size 10, and populate this array with 10 double s. Then call the sort generic algorithm with these pointer values as arguments and display the results.
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'.