How to use hash_set

P

Paulo da Silva

Hi.

I have a class Foo with a string. I want to store its elements in a
hash_set beeing the hash executed over the string. How do I do that?

class Foo
{
...
string s;
...
};

....
hash_set<Foo> mc;
Foo mc_el("a_string_key");

What do I have to do in Foo and how do I insert mc_el in mc?

BTW, I never used hash_set before. So please forgive me if I am
misunderstanding something basic.
So far I used a hash_map<string,Foo>, mc["a_string_key"]=mc_el. I think
a better solution for this case should be a hash_set because the key is
inside the object. Am I wrong?

Thanks for any help.
 
S

Sarath

Hi.

I have a class Foo with a string. I want to store its elements in a
hash_set beeing the hash executed over the string. How do I do that?

class Foo
{
...
string s;
...

};

...
hash_set<Foo> mc;
Foo mc_el("a_string_key");

What do I have to do in Foo and how do I insert mc_el in mc?

BTW, I never used hash_set before. So please forgive me if I am
misunderstanding something basic.
So far I used a hash_map<string,Foo>, mc["a_string_key"]=mc_el. I think
a better solution for this case should be a hash_set because the key is
inside the object. Am I wrong?

Thanks for any help.

You can see some samples at
http://msdn2.microsoft.com/en-us/library/h6f04085(VS.80).aspx
 
P

Paulo da Silva

Paulo da Silva escreveu:
Hi.

I have a class Foo with a string. I want to store its elements in a
hash_set beeing the hash executed over the string. How do I do that?

class Foo
{
...
string s;
...
};

...
hash_set<Foo> mc;
Foo mc_el("a_string_key");

What do I have to do in Foo and how do I insert mc_el in mc?

....

I found a possible solution. I'm posting it here for others who may need
it but also for comments from experts before I implement it in a real
program.

So, thank you very much for any comments.
Paulo.
__________________________________________________________________________
#include <memory>
#include <ext/hash_set>
#include <iostream>

using namespace std;

class Foo
{public:
string s; // public for simplicity
Foo(char *sc): s(sc) {}
};

class eqf
{public:
bool operator()(Foo const &s1,Foo const &s2) const
{ return (s1.s==s2.s);
}
};

class hf
{public:
size_t operator()(Foo const &x) const
{ return __gnu_cxx::hash<char const *>()(x.s.c_str());
}
};

typedef __gnu_cxx::hash_set<Foo,hf,eqf> MSet;

int main()
{ MSet mc;
mc.insert(Foo("xxxx"));
mc.insert(Foo("zzzz"));
MSet::const_iterator it=mc.find(Foo("xxxx"));
cout << "xxxx: " << (it!=mc.end()? "present":"not present")
<< endl;
it=mc.find(Foo("yyyy"));
cout << "yyyy: " << (it!=mc.end()? "present":"not present")
<< endl;
for (it=mc.begin();it!=mc.end();++it)
cout << it->s << endl;
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,104
Latest member
LesliVqm09
Top