I have the following code which implements a simple Hash/Dict in C++
Hash.h
using namespace std;
#include <string>
#include <vector>
class Hash
{
private:
vector<const char*> key_vector;
vector<const char*> value_vector;
public:
void set_attribute(const char*, const char*);
string get_attribute(const char*);
};
Hash.cpp
using namespace std;
#include "Hash.h"
void Hash::set_attribute(const char* key, const char* value)
{
key_vector.push_back(key);
value_vector.push_back(value);
}
string Hash::get_attribute(const char* key)
{
for (int i = 0; i < key_vector.size(); i++)
{
if (key_vector[i] == key)
{
return value_vector[i];
}
}
}
At the moment, the only type it can take as a key/value is a const char*
, but I want to extend it so that it can take any type (obviously only one type per hash). I was thinking about doing that by defining a constructor which takes a type as an argument, but I don't know at all how to do that in this situation. How would I do that, and how would I implement it so set_attribute is defined to take that type?
Compiler: Mono