Guest User

Untitled

a guest
May 29th, 2019
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.87 KB | None | 0 0
  1. #include <iostream>
  2. #include <unordered_map>
  3. #include <boost/utility/string_view.hpp>
  4. #include <boost/functional/hash.hpp>
  5.  
  6. using namespace std;
  7.  
  8. // custom specialization of boost::hash
  9. namespace boost
  10. {
  11.     template<>
  12.     struct hash<boost::string_view>
  13.     {
  14.       std::size_t operator()(boost::string_view str) const
  15.       {
  16.         return boost::hash_range<const char*>(str.begin(), str.end());
  17.       }
  18.     };
  19. }
  20.  
  21. namespace test
  22. {
  23.   class example
  24.   {
  25.    public:
  26.     void check()
  27.     {
  28.       m["abc"] = 123;
  29.       m["def"] = 456;
  30.     }
  31.     void print()
  32.     {
  33.       for (auto& elem : m)
  34.       std::cout << elem.first << ":" << elem.second << std::endl;
  35.     }
  36.    private:
  37.     std::unordered_map<boost::string_view, std::size_t, boost::hash<boost::string_view>> m;
  38.   };
  39. }
  40.  
  41. int main() {
  42.   test::example p;
  43.   p.check();
  44.   p.print();
  45.   return 0;
  46. }
Add Comment
Please, Sign In to add comment