Re: Help migrating hash_set to c++0x
Em 20-12-2010 22:59, Paulo da Silva escreveu:
I just saw (wikipedia for example) that unordered_set should implement
the same behaviour as that of hash_set. Neverthless it is not working in
this code!
#include <memory>
#include <unordered_set>
#include <iostream>
using namespace std;
class Foo
{public:
string s;
Foo(char const * const sc): s(sc) {}
};
class eqf
{public:
inline bool operator()(Foo const &s1,Foo const &s2) const
{ return (s1.s==s2.s);
}
};
class hf
{public:
inline size_t operator()(Foo const &x) const
{ return hash<char const *>()(x.s.c_str());
}
};
// typedef __gnu_cxx::hash_set<Foo,hf,eqf> MSet;
typedef unordered_set<Foo,hf,eqf> MSet;
int main()
{ MSet mc;
pair<MSet::iterator,bool> r;
r=mc.insert(Foo("xxxx"));
// OK expected and obtained
cout << "xxxx " << (r.second?"OK":"BAD") << endl;
mc.insert(Foo("zzzz"));
// Does not allow duplicates ...
r=mc.insert(Foo("zzzz"));
// BAD (duplicate) expected but OK obtained
cout << "zzzz " << (r.second?"OK":"BAD") << endl;
MSet::const_iterator it=mc.find(Foo("xxxx"));
for (it=mc.begin();it!=mc.end();++it)
cout << it->s << endl;
return 0;
}
Anything wrong? Better way to implement?
Thanks for any comments.
Please correct me if I am wrong ...
I replaced hash_set by unordered_set. It seems that hash_set does not
exist using c++0x.
I am having the following problem:
pair<some_container::iterator,bool> r=ct_dir->insert(de);
When "de" already exists, with hash_table r.second contained false, but
using unordered_set r.second contains true, i.e. it accepts duplicates.
How do I fix this to avoid duplicates in the set and also be aware of that?
Thanks