In this post, we go about __gnu_cxx::hash_set. As we saw before, we need to define our own HashFcn. We'll also need to define EqualKey if we're using custom data types. Another workaround is to overload the == operator inside the std namespace, then you don't have to provide your own EqualKey. It should look like:
namespace std {However, I prefer the first solution, and we'll use it in the code below. We also define our own hasher and pass it to the hash_set declaration instead of specializing the hash template inside the __gnu_cxx namespace as we did in the previous post.
bool operator==(const dnsRecord& rec1, const dnsRecord& rec2) {
return rec1.ip == rec2.ip;
}
}
#include <iostream>
#include <ext/hash_set>
using namespace std;
using namespace __gnu_cxx;
class dnsRecord {
public:
unsigned long ip;
string domainName;
};
class MyHasher {
public:
size_t operator()(const dnsRecord &r) const
{
return h(r.domainName.c_str());
};
private:
__gnu_cxx::hash<char*> h;
};
class MyComparator
{
public:
bool operator()(const dnsRecord& rec1, const dnsRecord& rec2) const {
return rec1.ip == rec2.ip;
}
};
int main(int argc, char *argv[])
{
hash_set<dnsRecord, MyHasher, MyComparator> hset;
dnsRecord rec1;
dnsRecord rec2;
dnsRecord rec3;
rec1.ip = 989798l; rec1.domainName = "dname1";
rec2.ip = 112334l; rec2.domainName = "dname2";
rec2.ip = 808323l; rec3.domainName = "dname3";
hset.insert(rec1);
hset.insert(rec2);
hset.insert(rec3);
dnsRecord rec4;
rec4.ip = 0;
rec4.domainName = "dname1";
hash_set<dnsRecord, MyHasher, MyComparator>::iterator it;
for(it = hset.begin(); it != hset.end(); it++)
cout << (*it).ip << ", " << (*it).domainName << endl;
rec4.domainName = "dname3";
hset.erase(rec4);
rec4.domainName = "dname3";
if(hset.find(rec4) == hset.end())
cout << "Successfully removed dname3" << endl;
else
cout << "Error!" << endl;
return EXIT_SUCCESS;
}