智能指针作为地图键

时间:2014-10-08 08:47:09

标签: c++ c++11 map shared-ptr smart-pointers

我有以下代码来测试智能指针作为std::map的关键,我在Mac和Linux上运行代码,但我观察到不同的输出,这是一个错误还是我做错了什么?

#include <iostream>
#include <memory>
#include <string>
#include <map>

using namespace std;

class Dog {
 public:
   typedef shared_ptr<Dog> sptr;

   Dog(const string &name) : name_(name) { }

   friend bool operator<(const Dog &lhs, const Dog &rhs) {
     cout << "Dog::operator< called" << endl;
     return lhs.name_ < rhs.name_;
   } 

   friend bool operator<(const sptr &lhs, const sptr &rhs) {
     cout << "Dog::operator< sptr called" << endl;
     return lhs->name_ < rhs->name_;
   } 

 private:
   string name_;
};

void test_raw_object_as_map_key() {
  cout << "raw object as map key ============== " << endl;
  map<Dog, int> m;
  m[Dog("A")] = 1;
  m[Dog("B")] = 2;
  m[Dog("C")] = 3;
  m[Dog("A")] = 4;

  cout << "map size: " << m.size() << endl;
}

void test_smart_pointer_as_map_key() {
  cout << "smart pointer as map key ============== " << endl;

  map<Dog::sptr, int> m;
  m[make_shared<Dog>("A")] = 1;
  m[make_shared<Dog>("B")] = 2;
  m[make_shared<Dog>("C")] = 3;
  m[make_shared<Dog>("A")] = 4;

  cout << "map size: " << m.size() << endl;
}

int main(int argc, const char *argv[]) {
  test_raw_object_as_map_key();
  test_smart_pointer_as_map_key();
  return 0;
}

在Mac上:

neevek@MAC$ g++ --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin13.1.0
Thread model: posix

neevek@MAC$ ./a.out
raw object as map key ============== 
Dog::operator< called
Dog::operator< called
Dog::operator< called
Dog::operator< called
Dog::operator< called
Dog::operator< called
Dog::operator< called
Dog::operator< called
Dog::operator< called
map size: 3
smart pointer as map key ============== 
Dog::operator< sptr called
Dog::operator< sptr called
Dog::operator< sptr called
Dog::operator< sptr called
Dog::operator< sptr called
Dog::operator< sptr called
Dog::operator< sptr called
Dog::operator< sptr called
Dog::operator< sptr called
map size: 3

在Linux上:

neevek@LINUX$ g++ --version
g++ (Ubuntu 4.8.2-19ubuntu1) 4.8.2
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

neevek@LINUX$ ./a.out
raw object as map key ============== 
Dog::operator< called
Dog::operator< called
Dog::operator< called
Dog::operator< called
Dog::operator< called
Dog::operator< called
Dog::operator< called
Dog::operator< called
Dog::operator< called
Dog::operator< called
map size: 3
smart pointer as map key ============== 
map size: 4

2 个答案:

答案 0 :(得分:8)

GCC是正确的(在Mac上,因为g++实际上是铿锵声):std::map使用std::less<T>比较密钥。这反过来在参数上调用operator <,但查找首先在namespace std中执行,因此它找到shared_ptr的默认实现,比较内部指针。要完成这项工作,您必须为std::less专门设置shared_ptr<Dog>

namespace std {
    template<>
    struct less<shared_ptr<Dog>> {
        bool operator() (const shared_ptr<Dog>& lhs, const shared_ptr<Dog>& rhs) {
            return *lhs < *rhs;
        }
    };
}

答案 1 :(得分:4)

Compare的默认std::map对象为std::less<Key>,在您的情况下为std::shared_ptr<Dog>。所以它查找std::less< std::shared_ptr<Dog> >实现,它只是比较指针地址。

要指定Compare对象,您可以自己定义并在map

中使用它
class MyCompare {
public:
  bool operator() (const sptr& l, const sptr& r) {
    return *l < *r; // Invokes your Dog's operator <
  }
};

然后

  map<sptr, int, MyCompare> m;
  m[make_shared<Dog>("A")] = 1;
  m[make_shared<Dog>("B")] = 2;
  m[make_shared<Dog>("C")] = 3;
  m[make_shared<Dog>("A")] = 4;

  cout << "map size: " << m.size() << endl;

输出map size: 3