存储整数:为什么“int”? [英] Storing an integer: why "int"?

查看:81
本文介绍了存储整数:为什么“int”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当你想在C ++中存储一个整数时,你使用整数类型,例如。


int main()

{

unsigned char amount_legs_dog = 4;

}


在编写可移植的C ++代码时,应该只有两个因素

影响您选择的整数类型:


A)签名。你想要只有正值吗?或者你想要两个

正值和负值?


B)C ++标准规定的该类型的最小范围。

短的最小范围和int是相同的。以下

语句在所有实现中始终为真:


sizeof(短)< = sizeof(int)


因为这样,为什么要使用类型int?什么?它好像没有任何价值。我总是会用短的在它的位置。


对此有什么想法?


-JKop

解决方案

2004年8月3日星期二19:59:03 GMT,JKop< NU ** @ NULL.NULL>写道:

当你想在C ++中存储一个整数时,你使用整数类型,例如。

int main()
{
unsigned char amount_legs_dog = 4;


在编写可移植的C ++代码时,应该只有两个因素会影响您选择的整数类型:

A)签名。你想要只有正值吗?或者你想要两个
正值和负值?

B)C ++标准规定的该类型的最小范围。

&的最小范围;短"和int是相同的。以下
语句在所有实现中始终如一:

sizeof(短)< = sizeof(int)

因为这样,为什么会有一个使用类型int什么?它似乎没有任何优点。我总是会用短的取而代之的是

对此有什么看法?

-JKop




标准说了些什么''int应该是所用架构的自然

大小''。换句话说,如果你使用的是32位
机器,你可以合理地预期一个整数为32位,而不是标准保证的

16。


另外(虽然我不是专家)如果你所处的架构中int和

short是不同的大小,那么期望int更多是非常合理的$>
效率比短,因为它使用了体系结构的自然大小。

换句话说如果你想节省空间则使用short,否则使用int,

它可能会更好但不会更糟(除了节省空间)。


john


John Harrison发布:

2004年8月3日星期二19:59:03 GMT,JKop< NU ** @ NULL.NULL>
写道:

当你想在C ++中存储一个整数时,你使用
整数类型,例如。

int main()
{
unsigned char amount_legs_dog = 4;在编写可移植的C ++代码时,应该只有两个
因子会影响您选择的整数类型:

A)签名。你想要只有正值吗?或者你想要
你想要正值和负值吗?

B)
C ++标准规定的那种类型的最小范围。

短的最小范围和int是相同的。
以下陈述在所有实现中始终如一:

sizeof(短)< = sizeof(int)

因为这样,为什么会有一个使用类型int在
全部?它似乎毫无价值。我会一直使用
short在它的位置。
对此有何想法?

-JKop
标准说''int的效果



是所用建筑的自然尺寸''。换句话说,如果
在32位机器上,你可以合理地期望一个整数为32
位而不是标准保证的16位。

另外(尽管我不是专家如果你使用
架构,其中int和short是不同的大小,它非常合理到
期望int比short更高效,因为它使用了自然的
大小架构。换句话说,如果你想要
节省空间,则使用short,否则使用int,它可能会更好但是它不会是
更糟糕(除了节省空间)。
john




但我再次强调,我们正在编写便携式的

代码。在编写便携式代码时,我们决定使用哪种

整体类型应该完全基于:


a)签名


b)最小范围


从此,(再次编写可移植代码),int似乎

没有任何优点,似乎应该

总是使用短在它的位置。

-JKop


JKop写道:

但我想再次强调我们正在编写便携式代码。在编写可移植代码时,我们决定使用哪种
整体类型应该完全基于:
a)签名
b)最小范围




为什么单独?如果你将其他东西带入

账户,代码也不会那么便携。


-

Salu2


When you want to store an integer in C++, you use an integral type, eg.

int main()
{
unsigned char amount_legs_dog = 4;
}

In writing portable C++ code, there should be only two factors that
influence which integral type you choose:

A) Signedness. Do you want only positive values? Or do you want both
positive and negative values?

B) The minimum range for that type as specified by the C++ Standard.
The minimum range for "short" and "int" are identical. The following
statement is always true on all implementations:

sizeof(short) <= sizeof(int)

As this is so, why would one ever use the type "int" at all? It seem to have
no merit whatsoever. I will always use "short" in its place.

Any thoughts on this?

-JKop

解决方案

On Tue, 03 Aug 2004 19:59:03 GMT, JKop <NU**@NULL.NULL> wrote:

When you want to store an integer in C++, you use an integral type, eg.

int main()
{
unsigned char amount_legs_dog = 4;
}

In writing portable C++ code, there should be only two factors that
influence which integral type you choose:

A) Signedness. Do you want only positive values? Or do you want both
positive and negative values?

B) The minimum range for that type as specified by the C++ Standard.
The minimum range for "short" and "int" are identical. The following
statement is always true on all implementations:

sizeof(short) <= sizeof(int)

As this is so, why would one ever use the type "int" at all? It seem to
have
no merit whatsoever. I will always use "short" in its place.

Any thoughts on this?

-JKop



The standard says something to the effect of ''int shall be the natural
size for the architecture used''. In other words if you are on a 32 bit
machine you can reasonably expect an integer to be 32 bits instead of the
16 guaranteed by the standard.

Also (although I''m no expert) if you are on an architecture where int and
short are different sizes its quite reasonable to expect int to be more
efficient than short since it uses the natural size of the architecture.
In other words use short if you want to save space but otherwise use int,
it might be better but it won''t be worse (except at saving space).

john


John Harrison posted:

On Tue, 03 Aug 2004 19:59:03 GMT, JKop <NU**@NULL.NULL> wrote:

When you want to store an integer in C++, you use an integral type, eg.

int main()
{
unsigned char amount_legs_dog = 4; }

In writing portable C++ code, there should be only two factors that influence which integral type you choose:

A) Signedness. Do you want only positive values? Or do you want both positive and negative values?

B) The minimum range for that type as specified by the C++ Standard.

The minimum range for "short" and "int" are identical. The following statement is always true on all implementations:

sizeof(short) <= sizeof(int)

As this is so, why would one ever use the type "int" at all? It seem to have no merit whatsoever. I will always use "short" in its place.
Any thoughts on this?

-JKop
The standard says something to the effect of ''int shall


be the natural size for the architecture used''. In other words if you are on a 32 bit machine you can reasonably expect an integer to be 32 bits instead of the 16 guaranteed by the standard.

Also (although I''m no expert) if you are on an architecture where int and short are different sizes its quite reasonable to expect int to be more efficient than short since it uses the natural size of the architecture. In other words use short if you want to save space but otherwise use int, it might be better but it won''t be worse (except at saving space).

john



But again I want to stress that we''re writing portable
code. In writing portable code, one''s decision on which
integral type to use should be based solely upon:

a) Signedness

b) The minimum range

From this, (again writing portable code), "int" appears to
have no merit whatsoever, and it seems that one should
always use "short" in its place.
-JKop


JKop wrote:

But again I want to stress that we''re writing portable
code. In writing portable code, one''s decision on which
integral type to use should be based solely upon:
a) Signedness
b) The minimum range



Why solely? The code is not less portable if you take other things into
account.

--
Salu2


这篇关于存储整数:为什么“int”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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