何时使用`short` over`int`? [英] When to use `short` over `int`?

查看:157
本文介绍了何时使用`short` over`int`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有许多问题要求C ++中 short int 整数类型之间存在差异,但实际上,你什么时候选择超过 int

There are many questions that asks for difference between the short and int integer types in C++, but practically, when do you choose short over int?

推荐答案

(有关详细说明,请参阅 Eric的回答

(See Eric's answer for more detailed explanation)

注意:


  • 一般来说, int 设置为'自然大小' - 硬件最有效处理的整数形式

  • 当使用 short 时一个数组或算术运算, short 整数转换为 int ,所以这可以引入一个命中处理速度整数

  • 使用可以节省内存(如果是比 int 更窄,这在使用大型数组时很重要

  • 您的程序将使用更多内存32位 int 系统与16位 int 系统

  • Generally, int is set to the 'natural size' - the integer form that the hardware handles most efficiently
  • When using short in an array or in arithmetic operations, the short integer is converted into int, and so this can introduce a hit on the speed in processing short integers
  • Using short can conserve memory if it is narrower than int, which can be important when using a large array
  • Your program will use more memory in a 32-bit int system compared to a 16-bit int system

结论:


  • 使用 int 除非你节省内存是至关重要的,否则你的程序会占用大量内存(例如许多阵列)。在这种情况下,请使用

  • Use int unless you conserving memory is critical, or your program uses a lot of memory (e.g. many arrays). In that case, use short.

这篇关于何时使用`short` over`int`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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