[英]c++ stl map iterators outputting values a hex
I have the following code: 我有以下代码:
map<StatTypesEnum, ValueHandler*>::const_iterator itr;
for(itr=player1->Stats.begin(); itr!=player1->Stats.end(); itr++)
{
cout << "Stat: " << itr->first << " Value: " << (ValueHandler*)(itr->second)->getValue() << endl;
}
The getValue() method returns an int. getValue()方法返回一个int。 If I cout the value outside of the iterator, it displays in base10 decimal, however when i return the value using an iterator (as above) it displays in base16, hex. 如果我在迭代器外部查找该值,它将以base10十进制显示,但是当我使用迭代器(如上)返回该值时,它将以base16(十六进制)显示。
Just for completeness, the following line displays as base10: 为了完整起见,以下行显示为base10:
cout << player1->Stats[Power]->getValue() << endl;
I would like the iterator to display base10. 我希望迭代器显示base10。
Thanks. 谢谢。
When you print (ValueHandler*)(itr->second)->getValue()
you should be getting a hexadecimal value because that's how pointers are printed. 当您打印(ValueHandler*)(itr->second)->getValue()
您应该获取一个十六进制值,因为这是指针的打印方式。 You probably shouldn't be casting the return value of getValue()
to a ValueHandler*
. 您可能不应该将getValue()
的返回值转换为ValueHandler*
。 You probably intended to cast itr->second
to that pointer type (although it's not necessary) but just got the parentheses wrong. 您可能打算将itr->second
转换为该指针类型(尽管不是必须的),但括号却弄错了。 Here's what casting itr->second
would look like: 这是强制转换itr->second
样子:
((ValueHandler*) itr->second)->getValue()
And what you want is probably: 而您想要的可能是:
itr->second->getValue()
(ValueHandler*)(itr->second)->getValue()
is a pointer, not an int
. (ValueHandler*)(itr->second)->getValue()
是一个指针,而不是int
。 You're casting the return value of getValue
. 您正在转换getValue
的返回值。
Maybe you want ((ValueHandler*)(itr->second))->getValue()
? 也许你想要((ValueHandler*)(itr->second))->getValue()
? Which is redundant anyway. 无论如何,这是多余的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.