user1061293
user1061293

Reputation: 167

how to create generic hashmap in c++?

How can I create a generic hashmap in vc++? I am using visual studio and vc++ as language option. I want to seperate my implementation into header(interface) and cpp files. header:

template<class T1,class T2>
class Generic
{
  map<T1,T2> m1;

  public:

  // Standard constructors and destructors
  // -------------------------------------
     Generic ();
     virtual ~Generic ();

  // Copy constructor and equal operator
  // -----------------------------------
  Generic (Generic &);
  Generic& operator=(Generic&);

  void insert(const T1& key,const T2& value);
  T2 lookup(const T1&key);


};

and my cpp file:

MyGeneric::Generic()
{
}

//-----------------------------------------------------------------------------
// Generic : destructor
//-----------------------------------------------------------------------------
Generic::~Generic()
{
}

//-----------------------------------------------------------------------------
// Generic : copy constructor
//-----------------------------------------------------------------------------
Generic::Generic(Generic& original)
{
}

//-----------------------------------------------------------------------------
// Generic : equal operator
//-----------------------------------------------------------------------------
Generic& Generic::operator=(Generic& original)
{
  return *this;
}
void Generic::insert(const T1& key,const T2& value)
{
}
T2 Generic::lookup(const T1&key)
{
}

I am wondering whether I am making a mistake here. I am also being confused about its usage becuase I am not defining the template in my cpp file. Is that the problem?. How can I delete a single value for a corresponding key in hashmap?

Upvotes: 2

Views: 1760

Answers (1)

Constantinius
Constantinius

Reputation: 35039

It looks like you need a variant type. Have a look at the boost variant. It allows you to store and retrieve any type you want. You can of course use it in combination with sequences or maps like std::unordered_map.

Implementing it your own is quite messy itself. Believe me, I tried :)

Upvotes: 2

Related Questions