CMPU-235: Software Development Methodology

Homework #4

Due: Oct. 6

Make the following modifications/clarifications to your grid and grid iterator classes, as discussed on tuesday.

For your Grid class:

  1. begin() This method should return an iterator for the top left element of the grid.
  2. end() This method should return an iterator that represents the position below the bottom left element of the grid.
  3. row_begin(Grid_iterator) This method should return an iterator that represents the leftmost element in the row that the iterator argument is in.
  4. row_end(Grid_iterator) This method should return an iterator that represents the position after the rightmost element in the row the iterator argument is in.

For your Grid Iterator class:

  1. operator++ This operator must not "wrap" around. After the last element in a row, it should return the row_end iterator. It is an error to call this method on any end iterator.
  2. operator* This operator should dereference the iterator and return the data element. It is an error to call this method on an end iterator.
  3. next_row() This method should return an iterator for the element immediately below the implicit iterator argument.

In general, these functions should provide the ability for a user of your classes to iterate through the elements of a grid as follows:


  typedef Grid<int>::iterator GridItor;
  Grid<int>; g;
  GridItor i,j;

  for (i=g.begin(); i!=g.end(); i=i.next_row())
    for (j=i; j!=g.row_end(i); j++)
      //  *j is the current element
Finally, modify your grid class to work as an associative grid. You should use the standard library pair class as your element data, and provide the following access method:

  • operator[](key) Should find the element in your grid whose key is == to the key argument. Note that this operator is invoked on a grid as follows:
      
      Grid<String,int> g;
    
      ...
    
      g["Chris"] = 5;
    
       ...
    
    Note: The following is an example of how the << operator is supposed to be overloaded for templates:
    
    template <class C> Grid{
    public:
    
      ...
    
      friend ostream& operator<<(ostream&, Grid<C>);
    
      ...
    }
    
    template <class T> ostream& operator<<(ostream& s, Grid<T> g)
    {
      ...
    }