By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,587 Members | 1,079 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,587 IT Pros & Developers. It's quick & easy.

How to use hash_set

P: n/a
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<Foomc;
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.
Jun 18 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Jun 19, 8:43 am, Paulo da Silva <psdasil...@esotericaX.ptXwrote:
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<Foomc;
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/lib...85(VS.80).aspx
Jun 19 '07 #2

P: n/a
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<Foomc;
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,eqfMSet;

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;
}
Jun 19 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.