cout 的输出根据类型转换方式而变化

发布于 2025-01-04 16:00:51 字数 562 浏览 3 评论 0原文

我正在开发一个程序,遇到一个奇怪的与 cout 相关的问题。由于程序有点大,而且代码最好讲,我将粘贴相关片段。

首先,我有一个迭代器,在 for 中定义为

for(vector::iterator it=facets_to_dump->begin(); it;end(); it++)

在此 for 中,如果我使用表达式,则

facet* facet_to_work_on = *it;
cout << facet_to_work_on->facet_id << "\t";

可以很好地打印出整数。

但是,如果我使用符号

cout << (facet*)(*it)->facet_id << "\t";

此代码会打印出十六进制值。十六进制值等于整数值。知道为什么会发生这种情况吗?

提前致谢。

I'm working on a program and have a strange, cout related problem. Since the program is a bit big and the code talks best, I'll paste the relevant snippets.

First, I have an iterator, *it defined in a for as

for(vector<facet*>::iterator it=facets_to_dump->begin(); it<facets_to_dump->end(); it++)

In this for, if I use the expression

facet* facet_to_work_on = *it;
cout << facet_to_work_on->facet_id << "\t";

Nicely prints out integers.

But, if I use the notation

cout << (facet*)(*it)->facet_id << "\t";

This code prints out hex values. Hex values are equal to the integer values. Any idea why this is happening?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

德意的啸 2025-01-11 16:00:51

打印出十六进制值的原因

cout << (facet*)(*it)->facet_id << "\t";

是 ->绑定比 (facet*) 转换更难,即它评估

(*it)->facet_id

并将结果转换为facet*。指针以十六进制输出。

The reason

cout << (facet*)(*it)->facet_id << "\t";

prints out a hex value is that -> binds harder than the (facet*) cast, that is it evaluates

(*it)->facet_id

and casts the result to a facet*. Pointers are output in hex.

三五鸿雁 2025-01-11 16:00:51

尝试包含 和:

 cout << dec << (facet*)(*it)->facet_id << "\t";

表示您想要十进制形式的数字。

Try including <iomanip> and:

 cout << dec << (facet*)(*it)->facet_id << "\t";

To say you want numbers in decimal form.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文