Yes you can ... but be careful. operator< defined in terms of the pointer, not in terms of the specified.
#include <memory> #include <map> #include <string> #include <iostream> int main() { std::map<std::shared_ptr<std::string>,std::string> m; std::shared_ptr<std::string> keyRef=std::make_shared<std::string>("Hello"); std::shared_ptr<std::string> key2Ref=std::make_shared<std::string>("Hello"); m[keyRef]="World"; std::cout << *keyRef << "=" << m[keyRef] << std::endl; std::cout << *key2Ref << "=" << m[key2Ref] << std::endl; }
prints
Hello=World Hello=
jsantander
source share