8

I have two options to create a std map. I can work with both the types of map.

1.    std::map<A, std::string>    
2.    std::map<A*, std::string>

where A is a class object

Later in the code I will have to perform a find operation.

1.    std::map<A, std::string>   myMap1;
          if(myMap1.find(A_obj) != myMap1.end())
          {
          }

2.    std::map<A*, std::string>   myMap2;
          if(myMap2.find(A_obj_ptr) != myMap2.end())
          {
          }

I want to know which one is recommend to create. In which of these two, would I not have to overload any operators in class A for find operation to work. Which of these would have problems on insert operation when any operators are not overloaded.

If it helps, this is class A

class A
{
    private:
        std::vector<std::string> m_member;

    public:
        A(std::vector<std::string> input);  
};
10
  • Depends on what your object represents. For such a decision one needs more information about A. Feb 11, 2014 at 18:18
  • @Paranaix I have added class A info
    – ontherocks
    Feb 11, 2014 at 18:24
  • Also consider using (1) a std::unordered_map for faster lookup times (given a suitable hash function and perhaps a cached hash), or (2) a sorted std::vector<std::pair<A,std::string>> for better cache locality.
    – metal
    Feb 11, 2014 at 18:27
  • @metal. Thanks for the std::unordered_map advice
    – ontherocks
    Feb 11, 2014 at 18:37
  • 1
    @Whoz I don't understand your remark about contiguous ranges then. A map with arbitrary pointers as keys is legal, precisely because less must give a total order. Feb 11, 2014 at 19:36

2 Answers 2

8

Note that these two samples are only functionally equivalent if A instances are singletons. Otherwise it's very possible that two A values which are equal in value but different in address. This would lead to different semantics.

Personally I prefer the std::map<A, std::string> version because the semantics of it are crystal clear. The keys have equality semantics and there is no potentially for a dangling or nullptr value. The std::map<A*, std::string> version comes with a host of questions for the developer looking through the code

  • Who owns the key values?
  • Are all instances of A singletons? If not how do I ensure the A I'm looking for is the A* value that is stored?
  • When are the keys freed?
4
  • 4
    Good questions. If one follow's Herb Sutter's sage advice, raw pointers are only non-owning observers. For ownership, one would use a suitable smart pointer (usually, std::unique_ptr for unique ownership or std::shared_ptr for shared ownership).
    – metal
    Feb 11, 2014 at 18:31
  • You are right. std::map<A, std::string> is cleaner. But then I will have to overload operators like map::key_comp for find to work and other operator for insert to work. Am I right? Could you please provide an example what do I need to do for insert and find to work in this case.
    – ontherocks
    Feb 11, 2014 at 18:31
  • 1
    @ontherocks in order to be a key in std::map there only needs to exist a valid implementation of std::less<A>. That can be done by having a template specialization or adding operator < to the type
    – JaredPar
    Feb 11, 2014 at 18:34
  • Why does it work with std::map<A*, std::string> without any operator overloading? Because pointers are numbers?
    – ontherocks
    Feb 11, 2014 at 19:54
0

First option is preferable. For second option, we need to make sure that keys (pointers here) are protected. May be shared pointers will help. Other issue is that the map will be shorted w.r.t. the address of the A objects and that might not be very useful. Below sample demonstrates how the comparator can be defined or the default comparator can be overridden:

class A 
{
public:
    int a;  
};

namespace std 
{
   template<> 
   struct less<A*>
   {
    bool operator()(const A* const a, const A*  const b) const{
        return a->a < b->a;

   }
};

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.