这是地图的有效运算符吗? [英] Is this a valid less than operator for a map?

查看:52
本文介绍了这是地图的有效运算符吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个已经有==和!=运算符的类,我想在地图中使用

,它使用我的类(不是指向类的指针)作为键。 />
要做到这一点,我需要一个<运营商。在这种情况下,这是一个有效的小于运算符吗?

bool MyClass :: operator<(const MyClass& other)const

{

if(* this == other)return false;

return(this<& other);

}


我可以使用此运算符声明并使用地图变量


std :: map< MyClass,std :: string> m_MyClassCache;


我担心如果地图已经排序然后添加了某些内容并且

通过复制到新数组来重新分配地图

项目可能不再排序。这可能是个问题吗?


谢谢

I have a class that already has == and != operators that I want to use
in a map that uses my class (not a pointer to the class) as the key.
To do this I need a < operator. Is this a valid less than operator to
use in this case?
bool MyClass::operator<(const MyClass &other) const
{
if (*this == other) return false;
return (this < &other);
}

I can declare and use a map variable using this operator

std::map<MyClass,std::string> m_MyClassCache;

I am worried that if the map is sorted and then something is added and
the map is reallocated by copying to a new array the previosly sorted
items may no longer be sorted. Is this likely to be a problem?

Thanks

推荐答案

8月22日,12:51 ,dimsttho ... @ yahoo.com < dimsttho ... @ yahoo.com>

写道:
On 22 Aug., 12:51, "dimsttho...@yahoo.com" <dimsttho...@yahoo.com>
wrote:

我有一个已经有==和!=运算符的类我想在地图中使用

作为关键字使用我的类(不是指向类的指针)。

要做到这一点,我需要一个<运营商。在这种情况下,这是一个有效的小于运算符吗?


bool MyClass :: operator<(const MyClass& other)const

{

* * * * if(* this == other)return false;

* * * * return(this<& other) ;


}
I have a class that already has == and != operators that I want to use
in a map that uses my class (not a pointer to the class) as the key.
To do this I need a < operator. Is this a valid less than operator to
use in this case?

bool MyClass::operator<(const MyClass &other) const
{
* * * * if (*this == other) return false;
* * * * return (this < &other);

}



否 - 它无效。交换元素会改变

谓词,这样你就可以得到两个元素< b和b< a

不同的时间。


/ Peter

No - it is not valid. Swapping the elements around will change the
predicate so that you for two elements could have a < b and b < a at
different times.

/Peter





你的地图键对内存地址进行排序,即指向它的指针(通常是

这不是你想要的)。这是地图上的一个键,即地图[Key] =

数据; (地图只需要<操作符)。

示例:


class MMyClass

{

private:

std :: string Key1; //键的第一部分

std :: string Key2; //键的第二部分

std :: string Key3; //键的第三部分


public:

MMyClass(const std :: string& Key1,const std :: string& Key2,const

std :: string& Key3):

Key1(Key1),

Key2(Key2),

Key3( Key3)

{

}

bool运算符<(const MMyClass& MyClass)const

{

if(Key1< MyClass.Key1)返回true;

else if(Key1 == MyClass.Key1)

{

if(Key2< MyClass.Key2)返回true;

else if(Key2 == MyClass.Key2)

{

return Key3< MyClass.Key3;

}

}


返回false;

}

};


问候,Ron AF Greve

http://www.InformationSuperHighway.eu


< di ********* @ yahoo .comwrote in message

news:eb ********************************** @ y21g2000 hsf.googlegroups.com ...
Hi,

Your key to the map sorts on the memory address i.e. pointer to it (usually
this is not what you want). Here is a key from a map i.e. the map[ Key ] =
Data; (The map only needs the < operator).
Example:

class MMyClass
{
private:
std::string Key1; // Part one of the key
std::string Key2; // Part two of the key
std::string Key3; // Part three of the key

public:
MMyClass( const std::string& Key1, const std::string& Key2, const
std::string& Key3):
Key1( Key1 ),
Key2( Key2 ),
Key3( Key3 )
{
}
bool operator<( const MMyClass& MyClass ) const
{
if( Key1 < MyClass.Key1 ) return true;
else if( Key1 == MyClass.Key1 )
{
if( Key2 < MyClass.Key2 ) return true;
else if( Key2 == MyClass.Key2 )
{
return Key3 < MyClass.Key3;
}
}

return false;
}
};


Regards, Ron AF Greve

http://www.InformationSuperHighway.eu

<di*********@yahoo.comwrote in message
news:eb**********************************@y21g2000 hsf.googlegroups.com...

>我有一个类已经有==和!=我想要使用的运算符

在地图中使用我的类(不是指向类的指针)作为键。

要做到这一点,我需要一个<运营商。在这种情况下,这是否是一个有效的少于运算符




bool MyClass :: operator<(const MyClass& other)const

{

if(* this == other)return false;

return(this<& other);

}


我可以使用此运算符声明和使用地图变量


std :: map< MyClass,std :: stringm_MyClassCache;


我担心如果地图已经排序然后添加了某些内容并且

通过复制到新数组来重新分配地图>
项目可能不再排序。这可能是个问题吗?


谢谢
>I have a class that already has == and != operators that I want to use
in a map that uses my class (not a pointer to the class) as the key.
To do this I need a < operator. Is this a valid less than operator to
use in this case?
bool MyClass::operator<(const MyClass &other) const
{
if (*this == other) return false;
return (this < &other);
}

I can declare and use a map variable using this operator

std::map<MyClass,std::stringm_MyClassCache;

I am worried that if the map is sorted and then something is added and
the map is reallocated by copying to a new array the previosly sorted
items may no longer be sorted. Is this likely to be a problem?

Thanks



8月22日,12: 18,peter koch< peter.koch.lar ... @ gmail.comwrote:
On 22 Aug, 12:18, peter koch <peter.koch.lar...@gmail.comwrote:

8月22日,12:51,dimsttho ... @ yahoo.com" < dimsttho ... @ yahoo.com>

写道:
On 22 Aug., 12:51, "dimsttho...@yahoo.com" <dimsttho...@yahoo.com>
wrote:

我有一个已经有==和!=运算符的类我想在地图中使用

作为关键字使用我的类(不是指向类的指针)。

要做到这一点,我需要一个<运营商。在这种情况下,这是否是一个有效的运营商?


I have a class that already has == and != operators that I want to use
in a map that uses my class (not a pointer to the class) as the key.
To do this I need a < operator. Is this a valid less than operator to
use in this case?


bool MyClass :: operator<(const MyClass& other)const

{

* * * * if(* this == other)return false;

* * * * return(this<& other);
bool MyClass::operator<(const MyClass &other) const
{
* * * * if (*this == other) return false;
* * * * return (this < &other);


}
}



否 - 它无效。交换元素会改变

谓词,这样你就可以得到两个元素< b和b< a

不同时间。


No - it is not valid. Swapping the elements around will change the
predicate so that you for two elements could have a < b and b < a at
different times.



我认为这有点狡猾。我已经提出了一个更好的解决方案

使用实例变量(在==运算符中不进行比较):


class MyClass

{

...


私人:

unsigned long m_instance;

static unsigned long m_instanceCount;

};


unsigned long MyClass :: m_instanceCount = 0;


MyClass: :MyClass():m_instance(m_instanceCount ++)

{

}


MyClass :: MyClass(double x,double y) :m_instance(m_instanceCount ++)

{

}

MyClass :: MyClass(const MyClass& rhs):m_instance(rhs。 m_instance)

{

}


MyClass& MyClass :: operator =(const MyClass& rhs)

{

if(this!=& rhs)

{

...

m_instance = rhs.m_instance;

}

返回* this;

}


bool MyClass :: operator<(const MyClass& other)const

{

if(* this ==其他)返回false;

return(m_instance< other.m_instance);

}

I thought it was a bit dodgy. I have come up with a better solution
using an instance variable (which is not compared in the == operator):

class MyClass
{
...

private:
unsigned long m_instance;
static unsigned long m_instanceCount;
};

unsigned long MyClass::m_instanceCount = 0;

MyClass::MyClass() : m_instance(m_instanceCount++)
{
}

MyClass::MyClass(double x, double y) : m_instance(m_instanceCount++)
{
}

MyClass::MyClass(const MyClass& rhs) : m_instance(rhs.m_instance)
{
}

MyClass& MyClass::operator=(const MyClass& rhs)
{
if (this != &rhs)
{
...
m_instance = rhs.m_instance;
}
return *this;
}

bool MyClass::operator<(const MyClass &other) const
{
if (*this == other) return false;
return (m_instance < other.m_instance);
}


这篇关于这是地图的有效运算符吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆