MAP和比较类别的C 问题

C ++ Issue with map and the Compare Class

本文关键字:问题 比较 MAP      更新时间:2023-10-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;

感谢您的帮助!

首先,不要这样做,因为它会拧紧您的地图:您需要地图的订购功能,而不是相等性测试。

无论如何,我想您的问题是您的密钥类型std :: string*,但是您的功能测试了字符串&amp;。我没有尝试过,但这应该解决:

bool operator()( std::string * str1, const std::string * str2 ) const
{
    return *str1 < *str2;
}