CSCI 330 Lab: Map

Complete the implementation of the my_map template class as defined below, so that it runs correctly with the test code test_my_map.cpp.

#ifndef MY_MAP_H
#define MY_MAP_H

// my_map.h

#include "my_set.h"
#include "my_pair.h"

using namespace std;

template <class key, class value>
class my_map : public my_set< my_pair<key, value> >
{
public:
   typedef my_set< my_pair<key, value> > parent;
   typedef my_set_iterator< my_pair<key, value> > iterator;

   my_map();
   iterator find(const key & k);
   value & operator[](const key & k);
      // If the given key is not found, insert it into the map.
   void erase(const key & k);
};

#endif


#ifndef MY_PAIR_H
#define MY_PAIR_H

// my_pair.h

template <class T1, class T2>
class my_pair
{
public:
   my_pair();
   my_pair(const T1 & first, const T2 & second);

   bool operator==(const my_pair & rhs) const;
   bool operator<(const my_pair & rhs) const;
   bool operator>(const my_pair & rhs) const;

   T1 first;
   T2 second;
};

#endif


// test_my_map.cpp

#include <iostream>
#include <string>
#include <map>
#include <cassert>
#include "my_map.h"

using namespace std;

int main(char *args[])
{
   //#define MAP map
   #define MAP my_map
   MAP<string, int> m;

   assert(m.size() == 0);
   assert(m.empty());

   m["abby"] = 9;
   assert(m["abby"] == 9);

   m["billy"] = 6;
   assert(m["billy"] == 6);
   assert(m["abby"] == 9);

   m["katie"] = -2;
   assert(m["katie"] == -2);
   assert(m["billy"] == 6);
   assert(m["abby"] == 9);

   m["bobby"] = -4;
   assert(m["bobby"] == -4);
   assert(m["katie"] == -2);
   assert(m["billy"] == 6);
   assert(m["abby"] == 9);

   m["binky"] = 100;
   m["abby"] = -100;
   assert(m["binky"] == 100);
   assert(m["bobby"] == -4);
   assert(m["katie"] == -2);
   assert(m["billy"] == 6);
   assert(m["abby"] == -100);

   assert(m["charles"] == 0);

   assert((*m.find("katie")).second == -2);

   assert(m.find("katie") != m.end());

   m.erase("katie");
   assert(m.find("katie") == m.end());

   cout << "All tests passed." << endl;
   cin.get();
}