So for my homework i needa write a code that requires me to look up on a T key and adding stuff to T values. Could someone help me plz? (ie can i set both the key and value to template type)
p.s. sorry not being able to expain the question too well! If you don't understand what i mean u can hv a look at the question:
In this question, you are required to implement a template class for a container data structure that supports hashing.
It should satisfy the following requirements.
a. The complete implementation (including the class definition and the implementation of the member functions) should be contained in a file HashBin.h which can be included by other client programs.
b. The template class should be named HashBin. A container storing items of type T can be created by a statement HashBin<T> hashbin;.
For example, HashBin<string> hashbin; should define a container that is capable of storing strings in bins.
c. It should consist of the following functions.
i. void add(T item): The structure will store the items into bins according to the hash code of the item. To use this template, the item must provide the hashcode() function that returns a hash code of type int. For example, we can define a class like this:
`
class MyInt {
private:
int value;
public:
MyInt(int v) { value = v; }
int hashcode() { return v%10; }
friend bool operator==(const MyInt & a,
const MyInt & b) {
return a.value == b.value;
};
friend ostream& operator<<(ostream& cout,
const MyInt & a) {
cout << a.value; return cout;
};
};
`
Then we can do this:
HashBin<MyInt> hashbin;
MyInt a(11);
MyInt b(37);
hashbin.add(a);
hashbin.add(b);
In this case, a will be put to bin 1, and b will be put to bin 7.
ii. int count(T key): Searches the container for elements with the specific key and returns the number of matches. For example the following code will print the count of 3.
HashBin<MyInt> hashbin;
MyInt a(11);
hashbin.add(a);
hashbin.add(a);
hashbin.add(a);
cout << hashbin.count(a) << endl;
You can assume that the == operator is defined for type T.
iii. void printBins(): It will print out items in all the bins in an ascending order of the hash code. For each bin, items are then printed in the same order as it is being added.
You can assume that the << operator is defined for printing of type T.
For example, the following code:
`
HashBin<MyInt> hashbin;
MyInt a(11);
MyInt b(51);
MyInt c(37);
MyInt d(127);
hashbin.add(a);
hashbin.add(b);
hashbin.add(c);
hashbin.add(d);
hashbin.add(a);
hashbin.add(c);
hashbin.add(a);
hashbin.printBits();
`
Will output:
1: 11 51 11 11
7: 37 127 37
The output may consist of trailing space.