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 the std::hash
specialization for std::u16string
provided by the standard library, which will work for the std::unordered_*
containers, but not Boost's ones.
One solution might be to define the hashing function in the following way:
std::size_t hash_value(std::u16string const &s) {
return std::hash<std::u16string>{}(s);
}
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_set
container in the C++11 standard library, in case you didn't know about it.