Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <unordered_map>
- #include <boost/utility/string_view.hpp>
- #include <boost/functional/hash.hpp>
- using namespace std;
- // custom specialization of boost::hash
- namespace boost
- {
- template<>
- struct hash<boost::string_view>
- {
- std::size_t operator()(boost::string_view str) const
- {
- return boost::hash_range<const char*>(str.begin(), str.end());
- }
- };
- }
- int main() {
- std::vector<std::string> v(3);
- v[0] = "jeffin";
- v[1] = "sam";
- v[2] = "good";
- boost::string_view strview;
- std::vector<std::string> temp;
- std::unordered_map<boost::string_view, std::size_t, boost::hash<boost::string_view>> m;
- for (auto& str :v)
- {
- strview = str;
- temp.push_back(std::string(strview));
- std::cout<<"I will insert a pointer pointing to "<<&temp[temp.size()-1]<<std::endl;
- m[temp[temp.size()-1]]=1;
- for(auto &i : temp)
- {
- std::cout<<i<<" "<<&i<<std::endl;
- }
- std::cout<<std::endl;
- for (auto& elem : m)
- std::cout << elem.first << ":" << elem.second << std::endl;
- }
- std::cout<<"printing using index "<<temp[2]<<" and now address "<<&temp[2]<<std::endl;
- v.clear();
- return 0;
- }
Add Comment
Please, Sign In to add comment