D
ddh
Hi:
I have a class:
class CA
{
private:
int m_val;
};
and another
class CB
{
};
and then I use a std::map to contain the CA & CB pairs:
typedef std::map<CA *, CB *> CMyMap;
Then I get some CA *, CB * to insert into the map:
CA *pA1 = new CA()
CB *pB1 = new CB();
map.insert (std::make_pair(pA1, pB1));
CA *pA2 = new CA();
CB *pB2 = new CB();
map.insert (std::make_pair(pA2, pB2));
But the problem arise, when the second time i insert a pair, it use the
value of the point for comparing. That is to say, if pA2 < pA1, pA2
will be inserted in front of pA1 and vice verse. Which is not what I
want.
So I modify the class CA as:
class CA
{
public:
bool operator < (CA *pA)
{
return m_val < pA->m_val;
}
private:
m_val;
}
But it doesn't work. How can I get to use a user defined compare
function instead the comparion of the raw pointer?
Thank you for your help very much!!
I have a class:
class CA
{
private:
int m_val;
};
and another
class CB
{
};
and then I use a std::map to contain the CA & CB pairs:
typedef std::map<CA *, CB *> CMyMap;
Then I get some CA *, CB * to insert into the map:
CA *pA1 = new CA()
CB *pB1 = new CB();
map.insert (std::make_pair(pA1, pB1));
CA *pA2 = new CA();
CB *pB2 = new CB();
map.insert (std::make_pair(pA2, pB2));
But the problem arise, when the second time i insert a pair, it use the
value of the point for comparing. That is to say, if pA2 < pA1, pA2
will be inserted in front of pA1 and vice verse. Which is not what I
want.
So I modify the class CA as:
class CA
{
public:
bool operator < (CA *pA)
{
return m_val < pA->m_val;
}
private:
m_val;
}
But it doesn't work. How can I get to use a user defined compare
function instead the comparion of the raw pointer?
Thank you for your help very much!!