C++ 重载map::key_comp,以便在对象指针的映射上使用map::find

C++ 重载map::key_comp,以便在对象指针的映射上使用map::find,c++,map,C++,Map,map::find利用map::key\u comp搜索密钥。默认的map::key_comp对于内置的数据类型很好。 我有一个对象指针映射std::map您可以使用自定义比较器声明映射,在这里您可以添加比较逻辑 struct structMyObjectAComparer { bool operator ()(const A* pLeft, const A* pRight) { } }; std::map<A*, B*

map::find
利用
map::key\u comp
搜索密钥。默认的
map::key_comp
对于内置的数据类型很好。
我有一个对象指针映射
std::map您可以使用自定义比较器声明映射,在这里您可以添加比较逻辑

struct structMyObjectAComparer
    {
        bool operator ()(const A* pLeft, const A* pRight)
        { 

        }
    };

std::map<A*, B*, structMyObjectAComparer> myMap
structStructMyObject比较程序
{
布尔运算符()(常数A*pLeft,常数A*pRight)
{ 
}
};
std::map myMap

在定义映射时,请查看指定比较模板参数:

class CompareIntPtr
{
public:
    bool operator()( int* p1, int* p2 )
    {
        return *p1 < *p2;   
    }
};

int main() {

    typedef std::map< int*, int, CompareIntPtr > TestMapType;
    TestMapType m;

    int *i1 = new int( 1 );
    int *i2 = new int( 2 );

    m.insert( std::make_pair( i1, 1 ) );
    m.insert( std::make_pair( i2, 2 ) );

    TestMapType::const_iterator found = m.find( i1 );
    if( found != m.end() )
        std::cout << "Found i1";

    return 0;
}
类比较ptr
{
公众:
布尔运算符()(int*p1,int*p2)
{
返回*p1<*p2;
}
};
int main(){
typedef std::mapTestMapType;
testmapm型;
int*i1=新的int(1);
int*i2=新的int(2);
m、 插入(std::make_对(i1,1));
m、 插入(std::make_对(i2,2));
TestMapType::const_iterator found=m.find(i1);
如果(找到!=m.end())
标准::cout