问题描述
我有一个有趣的作业,其中我有一个std::map
的CTurist
(上一个类)和unsigned
变量.这是代码:
I have this interesting assignment where I have a std::map
of CTurist
(previous class) and unsigned
variable. Here's the code:
class CTurist
{
protected:
string tName;
int age;
public:
CTurist() {};
CTurist(string name, int age2)
{
tName = name;
age = age2;
}
bool operator<(const CTurist& e) const
{
return age < e.age;
}
friend ostream& operator<<(ostream& os, const CTurist&& e);
friend ifstream& operator>>(ifstream& is, CTurist&& e);
};
class CHotel:public CTurist
{
protected:
string hName;
int stars;
int beds;
map<CTurist, unsigned> Turisti;
public:
unsigned Sum = 0;
CHotel(){};
CHotel(string hName2, int zvezdi, int legla)
{
hName = hName;
stars = zvezdi;
beds = legla;
}
int Compare()
{
list<CTurist*> list;
int br=0;
CTurist vyzrast;
map<CTurist, unsigned>::iterator it = Turisti.begin();
while (it != Turisti.end())
{
if (it->first < vyzrast)
{
br++;
}
else
{
list.push_back(std::move(&it->first));
}
}
}
};
我知道这很长,但是我认为最好向您提供所有信息.
I know it's quite a long one, but I thought it's best to give you all the information.
现在,底部的int Compare()
功能是导致我遇到问题的功能.
Now, the int Compare()
function AT THE BOTTOM is the one causing me problems.
我必须检查游客的年龄是否高于或低于我在这里称为vyzrast
的参数.我们正在比较age
.如果在下方,则非常简单.
I have to check if the age of the tourists is above or below a parameter which I've called vyzrast
here. We're comparing the age
. If it's below, it's quite straight forward.
如果在上面,我必须将那些游客添加到list<CTurist*>
中.指向指针列表.如果我从对象而不是指针创建列表.对此不走运,因此,为什么我要在此处寻求解决方法的建议.
If it's above though, I have to add those Tourists to a list<CTurist*>
, aka. to a list of pointers. If I make the list from objects, not pointers. No luck with this, hence why I'm looking for suggestions how to fix it here.
推荐答案
为什么在operator
重载中使用右值引用?您应该将const CTurist &
用于operator<<
,将CTurist&
用于operator>>
.而且您应该对operator>>
使用std::istream
:
Why are you using rvalue references for your operator
overloads? You should be using const CTurist &
for operator<<
and CTurist&
for operator>>
. And you should be using std::istream
for operator>>
:
friend ostream& operator<<(ostream& os, const CTurist &e);
friend istream& operator>>(istream& is, CTurist &e);
除此之外,Compare()
根本没有理由在填充std::list
时使用std::move()
,因为它添加了指针,而不移动实际对象.
Aside from that, there is no reason for Compare()
to use std::move()
at all when populating the std::list
, as it is adding pointers, not moving actual objects.
Compare()
由于以下几个原因而无法正确显示:
Compare()
doesn't won't correctly for several reasons:
-
it->first
是实际的const CTurist
对象,但是您的std::list
期望使用CTurist*
指针.std::map
键是const
,因此&it->first
是CTurist const *
指针(指向const对象的非const指针),而不是您期望的CTurist*
指针(指向非const对象的非const指针) .
it->first
is an actualconst CTurist
object, but yourstd::list
is expectingCTurist*
pointers.std::map
keys areconst
, so&it->first
is aCTurist const *
pointer (non-const pointer to const object), not aCTurist*
pointer (non-const pointer to non-const object) like you are expecting.
您的vyzrast
对象未初始化.您根本没有为其age
(和tName
)成员分配任何值,因此比较的结果不确定.
Your vyzrast
object is uninitialized. You are not assigning any value to its age
(and tName
) member at all, so the result of your comparisons is indeterminate.
您不会在每次循环迭代时增加您的it
迭代器,因此如果std::map
不为空,则会陷入无限循环.
You are not incrementing your it
iterator on each loop iteration, so you are stuck in an infinite loop if the std::map
is not empty.
请尝试以下类似操作:
int Compare()
{
std::list<const CTurist *> clist;
int br = 0;
CTurist vyzrast("", SomeAgeValueHere);
std::map<CTurist, unsigned>::iterator it = Turisti.begin();
while (it != Turisti.end())
{
if (it->first < vyzrast)
{
br++;
}
else
{
clist.push_back(&it->first);
}
++it;
}
// use clist as needed...
return SomeValueHere;
}
这篇关于从地图复制到指针列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!