How to make the operator < ( ) work ? | | |
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!! | | | | re: How to make the operator < ( ) work ?
ddh wrote:[color=blue]
> 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();[/color]
STL uses Value Semantics only (Don't ask why, C++ is not a reasonable
computer language). What you do probably compiles but is against the
design principles of STL. If not semantically wrong do
typedef std::map<CA, CB *> CMyMap;
or even better:
typedef std::map<CA, CB> CMyMap;
[color=blue]
> 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)[/color]
friend bool operator < (const CA& left, const CA& right) {
return left.m_val < right.m_val;
}
[color=blue]
> {
> 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?[/color]
If you really want it (although it's a bad idea):
struct deref_compare {
bool operator< (const CA* left, const CA* right) const {
return *left < *right;
}
}
std::map<CA*, CB*, deref_compare> mymap; | | | | re: How to make the operator < ( ) work ?
ddh wrote:
[color=blue]
> 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;[/color]
Why pointers?
[color=blue]
> 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.[/color]
Of course not. That operator compares a CA with a pointer to CA, not two
pointers. And before you try: No, you cannot make one that compares two
pointers.
[color=blue]
> How can I get to use a user defined compare function instead the comparion
> of the raw pointer?[/color]
Use the map's 3rd template argument. | | | | re: How to make the operator < ( ) work ?
"Rapscallion" <raps72583m@spambob.com> skrev i en meddelelse
news:1117823023.747881.296950@g49g2000cwa.googlegr oups.com...[color=blue]
> ddh wrote:[color=green]
>> 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();[/color]
>
> STL uses Value Semantics only (Don't ask why, C++ is not a reasonable
> computer language).[/color]
Why? Do you find it more reasonable to use values semantics sometimes and
pointer semantics at other times? With the developer having no choice but to
use the choices made by the language implementor.
[color=blue]
> What you do probably compiles but is against the
> design principles of STL. If not semantically wrong do
>
> typedef std::map<CA, CB *> CMyMap;
>
> or even better:
>
> typedef std::map<CA, CB> CMyMap;[/color]
That entirely depends on the purpose of the OP. Both are correct, of course.[color=blue]
>[color=green]
>> 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)[/color]
>
> friend bool operator < (const CA& left, const CA& right) {
> return left.m_val < right.m_val;
> }
>
>[color=green]
>> {
>> 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?[/color]
>
> If you really want it (although it's a bad idea):[/color]
Why is it a bad idea? If the user believes that pointers were the correct
solution but wants his sort to be made according to value-semantics, this is
a perfectly reasonable way to go.[color=blue]
>
>
> struct deref_compare {
> bool operator< (const CA* left, const CA* right) const {
> return *left < *right;
> }
> }
>
> std::map<CA*, CB*, deref_compare> mymap;
>[/color]
/Peter |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,510 network members.
|