【发布时间】:2012-11-06 21:57:16
【问题描述】:
我希望也许有人可以帮助我解决我的问题。我得到了一个可爱的
没有匹配函数调用类型为“const pCompare”的对象
struct pCompare
{
bool operator()( const std::string & str1, const std::string & str2 ) const
{
return str1.compare( str2 ) == 0;
}
};
std::string *t = new std::string ( "/test.html" );
std::map<std::string*, std::string, pCompare> test;
test.insert ( std::pair<std::string*, std::string> ( t, "héhé" ) );
std::cout << test.find(new std::string ("/test.html") )->second;
感谢您的帮助!
【问题讨论】:
-
您的地图有
(const) string *作为键,但您的比较器采用const string &... -
是否可以将您的实现更改为使用
std::map<std::string, std::string>。使用值而不是指针可以使事情变得更容易。 -
另请注意,比较器应测试排序不相等,否则您将进入 UB 领域。