How to pass a pointer to insert function of unordered_set in c++?

71 Views Asked by At

The following code doesnt work:

    template<typename T>
    struct Hashf {
       size_t operator()(T* ptr) const {
           return (size_t)*ptr;
      }
    };



    template<typename Container>
    auto Func(Container const &a_container)
    {
        auto a_begin = std::rbegin(a_container);
        auto a_end = std::rend(a_container);

        using T = typename Container::iterator::value_type;
        std::unordered_set<T*,  Hashf<T>> mytest;

        auto it_temp = a_end;

        mytest.insert(a_begin);  //cant insert the pointer 
        return it_temp;
    }

The question is how can I insert pointers(or the iterator) to unordered_set? The insert function fails.

I tried to make a hash set that stores the pointer

1

There are 1 best solutions below

1
lorro On

Iterators and pointers are different types. Thus you cannot insert an iterator to a set of T*; however, you can dereference and take the address of it:

mytest.insert(&*a_begin);