Pointers as keys in map C++ STL

后端 未结 5 756
误落风尘
误落风尘 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:57

    Leaving aside from the legality of this and any possible semantic misunderstandings, addressed already, I cannot think of any reason to use std::map here rather than std::unordered_map. There are early intercepts of this in Boost and Visual C++ if you are on a pre-C++11 compiler.

    Since you appear to be using a pointer to represent a unique object, something like boost::flyweight could be applicable.

提交回复
热议问题