Guest User

Untitled

a guest
May 30th, 2019
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.21 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. int main() {
  22.   std::vector<std::string> v(3);
  23.   v[0] = "jeffin";
  24.   v[1] = "sam";
  25.   v[2] = "good";
  26.   boost::string_view strview;
  27.   std::vector<std::string> temp;
  28.   std::unordered_map<boost::string_view, std::size_t, boost::hash<boost::string_view>> m;
  29.   for (auto& str :v)
  30.   {
  31.     strview = str;
  32.     temp.push_back(std::string(strview));
  33.     std::cout<<"I will insert a pointer pointing to "<<&temp[temp.size()-1]<<std::endl;
  34.  
  35.     m[temp[temp.size()-1]]=1;
  36.     for(auto &i : temp)
  37.     {
  38.       std::cout<<i<<" "<<&i<<std::endl;
  39.     }
  40.     std::cout<<std::endl;
  41.     for (auto& elem : m)
  42.       std::cout << elem.first << ":" << elem.second << std::endl;
  43.   }
  44.   std::cout<<"printing using index "<<temp[2]<<" and now address "<<&temp[2]<<std::endl;
  45.   v.clear();
  46.   return 0;
  47. }
Add Comment
Please, Sign In to add comment