CSc 220 Assignment 4

Goes On Forever

Assigned
Due

Apr 15
40 pts
May 1

This is a simple assignment to create a template class unbound_arr<T> which represents an unbounded array of type T. Your template class may have a default a constructor if needed, and a whopping two methods:

T a.get(i)
For an unbound_arr<T> a, return the last value stored at integer subscript i. If no such value is stored, return T(). This method should be a const method; in particular, it should not retain any information after the call.
T a.put(i, x)
The value x of type T is stored in the structure, and the value previously stored there is returned: The put method returns the same value that get would if called immediately before with i. Whenever possible, it should not store or retain the value T(). Since get will return T() for values of i when nothing is stored at i, it is more efficient to avoid storing it whenever possible.

The template assumes that type T has a default constructor and can be compared with ==.

How To Do This?

The simplest way is to store your data in a private map, int to T. The map itself already behaves in a similar way, except that it is not careful to avoid storing unneeded data. In particular, get must not use bracket subscripting, since that will always store an entry if the subscript does not exist. Either use the find method on your map, or use the at method and catch the exception when the item is not present.

Your put method should only add the item to the map if x is not equal to T(). If a position with a different value is replaced with T(), it should be deleted rather than stored.

There is a small test driver here.

Submission

When your program works and is properly formatted and commented, submit it online here.