铸造类型不仅表示 [英] casting the type not only the representation

查看:74
本文介绍了铸造类型不仅表示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好


我有一个返回time_t的方法,另外两个方法返回

双数据类型我不能改变它,因为图书馆是由Big Bucks Inc.提供的
。我觉得time_t很长但我不能

从time.h验证。


使用sizeof(type)和numeric_limits< type> :: max()告诉我int

和long给出相同的输出。


循环,我将把方法的输出保存到一个文件中,这个文件大小很大,因此如果可以的话,尽量减小它的大小。


文件格式为;

time_t double double \ n

....

每条记录的数量为432000代码运行一周。


减小其大小。我建议如下。

将time_t转换为int只是为了确保。

将其他2种方法的输出转换为float,因为那只是4

字节而非8.

不要使用" \ n"因此,在提取数据时需要额外的工作,但这是一个很好的工作。


问题:

施放只改变了数据而非实际的

类型。所以,如果我这样做,那么
cout_to_file<< static_cast< float(双重myMthd()的输出),

仍然需要8个字节,而不是预期的4个。


如何解决这个?


非常感谢。


Hi there

I have a method which returns time_t and another two methods return
double data types and I cann''t change that since the library is
provided by Big Bucks Inc. I think time_t is long but I could not
verify that from time.h

using sizeof(type) and numeric_limits<type>::max() tells me that int
and long give the same output.

in a loop, I will be saving the output of the methods to a file which
will be large in size and thus trying to minimize its size if I can.

the file will be in the format;
time_t double double \n
....
the number of records will be 432000 every week the code is run.

to reduce its size. I am suggesting the following.
cast time_t into int just to make sure.
cast the output of the other 2 methods into float since that is only 4
bytes and not 8.
don''t use "\n" thus extra work when extracting the data but that is
ok.

the problem:
cast only changes the representation on the data and not its actual
type. so if I do
cout_to_file << static_cast<float(the out put of double myMthd()),
that will still take 8 bytes and not 4 as expected.

how do I solve this?

many thanks.


推荐答案

* Gary Wessle:
* Gary Wessle:

你好


我有一个返回time_t的方法,另外两个方法返回

双数据类型,我不能改变,因为图书馆是由Big Bucks Inc.提供的
。我认为time_t很长但我不能

验证时间.h


使用sizeof(type)和numeric_limits< type> :: max()告诉我int

和long给出相同的输出。循环中$ b / b
我会将方法的输出保存到一个文件中,这个文件大小很大,因此如果可以的话我会尽量减小它的大小。


文件将采用格式;

time_t double double \\\


...

每周运行代码的记录数为432000.


减小其大小。我建议如下。

将time_t转换为int只是为了确保。

将其他2种方法的输出转换为float,因为那只是4

字节而非8.

不要使用" \ n"因此,在提取数据时需要额外的工作,但这是一个很好的工作。


问题:

施放只改变了数据而非实际的

类型。所以,如果我这样做,那么
cout_to_file<< static_cast< float(双重myMthd()的输出),

仍然需要8个字节,而不是预期的4个。


如何解决这个?
Hi there

I have a method which returns time_t and another two methods return
double data types and I cann''t change that since the library is
provided by Big Bucks Inc. I think time_t is long but I could not
verify that from time.h

using sizeof(type) and numeric_limits<type>::max() tells me that int
and long give the same output.

in a loop, I will be saving the output of the methods to a file which
will be large in size and thus trying to minimize its size if I can.

the file will be in the format;
time_t double double \n
...
the number of records will be 432000 every week the code is run.

to reduce its size. I am suggesting the following.
cast time_t into int just to make sure.
cast the output of the other 2 methods into float since that is only 4
bytes and not 8.
don''t use "\n" thus extra work when extracting the data but that is
ok.

the problem:
cast only changes the representation on the data and not its actual
type. so if I do
cout_to_file << static_cast<float(the out put of double myMthd()),
that will still take 8 bytes and not 4 as expected.

how do I solve this?



首先,你混淆了几件事。你将二进制

表示与文本表示混淆。而且你把C ++实现的

属性与标准保证混淆了。


其次,你过早优化,这就是邪恶(TM)。


想一想:一个GiB中有1024个MiB。每周,未优化,

你生产一个大约1.5 Mib的文件。好吧,即使只有一个1 GiB磁盘

,可以持续至少十年。目前的磁盘大小通常为
不小于100 GiB。这意味着至少一千年(磁盘

不会持续那么久,但有能力):/你为什么要优化/?


-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet和电子邮件中最烦人的事情是什么?

First, you''re confusing several things. You''re confusing binary
representation with textual representation. And you''re confusing the
properties of a C++ implementation with what the standard guarantees.

Second, you''re optimizing prematurely, which is Evil(TM).

Think about it: there are 1024 MiBs in one GiB. Each week, unoptimized,
you produce a file about 1.5 Mib. Well, even with just a 1 GiB disk
that can go on for at least ten years. Current disk sizes are typically
not less than 100 GiB. Which means at least a thousand years (the disk
won''t last that long, but has the capacity): /why are you optimizing/?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


Gary Wessle写道:
Gary Wessle wrote:

你好


使用sizeof(type)和numeric_limits< type> :: max()告诉我int

和long给出相同的输出。
Hi there

using sizeof(type) and numeric_limits<type>::max() tells me that int
and long give the same output.



不是一个可移植的假设。

Not a portable assumption.

$ b循环中的$ b,我将把方法的输出保存到一个文件中,这个文件大小很大,因此如果可以的话,尽量减小它的大小。

文件将采用格式;

time_t double double \ n

....

每周运行代码的记录数量为432000.


减小其大小。我建议如下。

将time_t转换为int只是为了确保。

将其他2种方法的输出转换为float,因为那只是4

字节而非8.

不要使用" \ n"因此,在提取数据时需要额外的工作,但这是确定的。
in a loop, I will be saving the output of the methods to a file which
will be large in size and thus trying to minimize its size if I can.

the file will be in the format;
time_t double double \n
....
the number of records will be 432000 every week the code is run.

to reduce its size. I am suggesting the following.
cast time_t into int just to make sure.
cast the output of the other 2 methods into float since that is only 4
bytes and not 8.
don''t use "\n" thus extra work when extracting the data but that is
ok.



为什么不按原样保存数据呢?如果尺寸困扰你,只需压缩

文件。

Why not just save the data as is? If the size bothers you, just compress
the file.


问题:

施放只改变了表示数据而不是其实际的

类型。所以,如果我这样做,那么
cout_to_file<< static_cast< float(双重myMthd()的输出),

仍然需要8个字节,而不是预期的4个。


如何解决这个?
the problem:
cast only changes the representation on the data and not its actual
type. so if I do
cout_to_file << static_cast<float(the out put of double myMthd()),
that will still take 8 bytes and not 4 as expected.

how do I solve this?



你想要一个文本文件还是一个二进制文件?


-

Ian Collins。

Do you want a text file or a binary file?

--
Ian Collins.


如果我这样做

浮动a;

浮动b;

a = static_cast< float(此处为double方法的输出);

b = static_cast< float(此处为double方法的输出);

cout<< a<< " " << b<< " ;


我得到

dat_col.cpp:171:警告:名字查找?? ba ??改变了

dat_col.cpp:137:警告:匹配这个?? ??根据ISO标准规则

dat_col.cpp:141:警告:匹配这个a ?? ba ??根据旧规则


嗯,是不是没有警告,为什么它没有捕获它和

它只是咳嗽b,我有吗认真对待这个警告?


谢谢
if I do
float a;
float b;
a = static_cast<float(output of the double method here);
b = static_cast<float(output of the double method here);
cout << a << " " << b << " ";

I get
dat_col.cpp:171: warning: name lookup of a??ba?? changed
dat_col.cpp:137: warning: matches this a??ba?? under ISO standard rules
dat_col.cpp:141: warning: matches this a??ba?? under old rules

well, is there not a warning with a, then why it did not catch it and
it only cough b, and do I have to take this warning seriously?

thanks


这篇关于铸造类型不仅表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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