How to work with stl with functor wrap of function pointer?

DreamLinuxer

I need to use unordered_map with function pointer passing in, is there any workaround which can make the following code work?


struct eq_fun
{
    bool operator()(void* s1, const void* s2) const
    {
        return ( _cmp_fn((void*)s1,(void*)s2) == 0 );
    }
    int (*_cmp_fn)(void*, void*);
    eq_fun(int (*fn)(void*, void*)):_cmp_fn(fn){}
};

struct hash_fun
{
    size_t operator()(const void *p) const
    {
        return _hash_fn(p);
    }
    int (*_hash_fn)(const void*);
    hash_fun(int (*fn)(const void*)):_hash_fn(fn){}
};

unordered_map<void*,void*> *create(int (*h)(const void*),int (*cmp)(void*,void*))
{
    return new unordered_map<void*,void*,hash_fun(h),eq_fun(cmp)>;
}

john

Sure

unordered_map<void*,void*,hash_fun, eq_fun> *create(int (*h)(const void*),int (*cmp)(void*,void*))
{
    return new unordered_map<void*,void*,hash_fun,eq_fun>(0, hash_fun(h), eq_fun(cmp));
}

Seems unordered_map does not have a constructor that takes only the hash function and equaility function, so I've added the minimum number of buckets parameter with a value of zero. In any case the important point is that you construct your function objects separately and pass those objects to the unordered_map constructor.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to assign functor to function pointer?

From Dev

How to call function pointer in STL

From Dev

Function pointer, Functor or Lambda?

From Dev

function pointer to functor

From Dev

function pointer to functor

From Dev

Functor and function pointer logic

From Dev

Function pointer, Functor or Lambda?

From Dev

How does `<$` function work in the Functor class?

From Dev

How to write a class that may accept a function pointer and/or functor just like a smart pointer does for custom deleter?

From Dev

Casting function pointer to functor in C++

From Dev

Deduce return type of function pointer or functor

From Dev

C++: how can I overload a function so that it can take any functor, including pointer to member functions?

From Dev

How does this code with a pointer function work?

From Dev

How to resize a STL list pointer

From Dev

Wrap c++ function that needs function pointer

From Dev

How to wrap a templated function to work on const and non-const data

From Dev

How to wrap a templated function to work on const and non-const data

From Dev

Passing dynamic functor to stl

From Dev

STL algorithm copy if with functor

From Dev

Why is C++ lambda implemented with functor instead of function pointer?

From Dev

C++: Function pointer as Template argument instead of functor

From Dev

C++: Function pointer as Template argument instead of functor

From Dev

Wrap a function pointer in C++ with variadic template

From Dev

wrap C ++ function in c # that returns pointer

From Dev

How do both of these function pointer calling syntax variations work?

From Dev

How does template converting operator to pointer to member function syntax work

From Dev

How to wrap the jQuery function

From Dev

How to wrap function in haskell?

From Dev

How to wrap function in haskell?

Related Related

  1. 1

    How to assign functor to function pointer?

  2. 2

    How to call function pointer in STL

  3. 3

    Function pointer, Functor or Lambda?

  4. 4

    function pointer to functor

  5. 5

    function pointer to functor

  6. 6

    Functor and function pointer logic

  7. 7

    Function pointer, Functor or Lambda?

  8. 8

    How does `<$` function work in the Functor class?

  9. 9

    How to write a class that may accept a function pointer and/or functor just like a smart pointer does for custom deleter?

  10. 10

    Casting function pointer to functor in C++

  11. 11

    Deduce return type of function pointer or functor

  12. 12

    C++: how can I overload a function so that it can take any functor, including pointer to member functions?

  13. 13

    How does this code with a pointer function work?

  14. 14

    How to resize a STL list pointer

  15. 15

    Wrap c++ function that needs function pointer

  16. 16

    How to wrap a templated function to work on const and non-const data

  17. 17

    How to wrap a templated function to work on const and non-const data

  18. 18

    Passing dynamic functor to stl

  19. 19

    STL algorithm copy if with functor

  20. 20

    Why is C++ lambda implemented with functor instead of function pointer?

  21. 21

    C++: Function pointer as Template argument instead of functor

  22. 22

    C++: Function pointer as Template argument instead of functor

  23. 23

    Wrap a function pointer in C++ with variadic template

  24. 24

    wrap C ++ function in c # that returns pointer

  25. 25

    How do both of these function pointer calling syntax variations work?

  26. 26

    How does template converting operator to pointer to member function syntax work

  27. 27

    How to wrap the jQuery function

  28. 28

    How to wrap function in haskell?

  29. 29

    How to wrap function in haskell?

HotTag

Archive