Why does following
#include <string>
#include <boost/unordered_set.hpp>
int main()
{
typedef boost::unordered_set<std::string> unordered_set;
unordered_set animals;
animals.emplace("cat");
animals.emplace("shark");
animals.emplace("spider");
return 0;
}
work and following results in too many compilation errors.
#include <string>
#include <boost/unordered_set.hpp>
int main()
{
typedef boost::unordered_set<std::u16string> unordered_set;
unordered_set animals;
animals.emplace("cat");
animals.emplace("shark");
animals.emplace("spider");
return 0;
}
Also, what's the solution for this ? Do I need to write my own hash_function and operator== in function objects as mentioned here ?
The
operator==is not a concern, because it is already defined in the standard library. However, the hash function has to be adapted from thestd::hashspecialization forstd::u16stringprovided by the standard library, which will work for thestd::unordered_*containers, but not Boost's ones.One solution might be to define the hashing function in the following way:
This wrapper will get you an already written logic wrapped to work nicely with Boost.
Lastly, let me remind you of the availability of the equivalent
std::unordered_setcontainer in the C++11 standard library, in case you didn't know about it.