Pointers as keys in map C++ STL

后端 未结 5 755
误落风尘
误落风尘 2020-12-08 06:17

I have a question on how pointers to a custom object are handled when used as Keys in an map. More specifically if I define

std::map< CustomClass*, int &         


        
5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-08 06:56

    Pointers will be handled but compared as pointers (memory order). You have to pass custom less functor if you wish to compare the objects:

    template struct ptr_less {
        bool operator()(T* lhs, T* rhs) {
            return *lhs < *rhs; }};
    map> mymap;
    

提交回复
热议问题