C++11——整型

参考:http://www.learncpp.com/



C ++基本数据类型的大小

基本数据类型取值范围

1Byte=8bit

1bit 存储 2^1=2个值

#include <iostream>

int main()
{
    std::cout << "bool:\t\t" << sizeof(bool) << " bytes" << std::endl;
    std::cout << "char:\t\t" << sizeof(char) << " bytes" << std::endl;
    std::cout << "wchar_t:\t" << sizeof(wchar_t) << " bytes" << std::endl;
    //std::cout << "char16_t:\t" << sizeof(char16_t) << " bytes" << std::endl; // C++11, may not be supported by your compiler
    //std::cout << "char32_t:\t" << sizeof(char32_t) << " bytes" << std::endl; // C++11, may not be supported by your compiler
    std::cout << "unsigned short:\t\t" << sizeof(unsigned short) << " bytes" << std::endl;
    std::cout << "signed short:\t\t" << sizeof(signed short) << " bytes" << std::endl;
    std::cout << "short:\t\t" << sizeof(short) << " bytes" << std::endl;
    std::cout << "unsigned int:\t\t" << sizeof(unsigned int) << " bytes" << std::endl;
    std::cout << "signed int:\t\t" << sizeof(signed int) << " bytes" << std::endl;
    std::cout << "int:\t\t" << sizeof(int) << " bytes" << std::endl;
    std::cout << "long:\t\t" << sizeof(long) << " bytes" << std::endl;
    std::cout << "long long:\t" << sizeof(long long) << " bytes" << std::endl; // C++11, may not be supported by your compiler
    std::cout << "float:\t\t" << sizeof(float) << " bytes" << std::endl;
    std::cout << "double:\t\t" << sizeof(double) << " bytes" << std::endl;
    std::cout << "long double:\t" << sizeof(long double) << " bytes" << std::endl;
    return 0;
}

这里写图片描述

int x;
std::cout << "x is " << sizeof(x) << " bytes" << std::endl;

这里写图片描述

这里写图片描述


#include <iostream>

int main()
{
    unsigned short x = 65535; // largest 16-bit unsigned value possible
    std::cout << "x was: " << x << std::endl;
    x = x + 1; // 65536 is out of our range -- we get overflow because x can't hold 17 bits
    std::cout << "x is now: " << x << std::endl;
    return 0;
}

/*
x was: 65535
x is now: 0
*/

对于高级读者,下面是幕后实际发生的情况:数字65,535由二进制的位模式1111 1111 1111 1111表示。 65,535是无符号2字节(16位)整数可容纳的最大数字,因为它使用全部16位。 当我们给该值加1时,新值应该是65,536。 然而,65,536的位模式以二进制表示为1 0000 0000 0000 0000,它是17位! 因此,最高位(即1)丢失,低16位全部剩余(只能存储16位,多出的一位自动被丢弃掉)。 位模式0000 0000 0000 0000对应于数字0,这是我们的结果。

当用两个整数进行除法时,C ++会产生一个整数结果。由于整数不能保持小数值,因此任何小数部分都会被丢弃(而不是圆整!)。

仔细看看上面的例子,8/5产生值1.6。小数部分(0.6)被丢弃,1的结果依然存在。

  • 规则:使用整数除法时要小心,因为您将丢失结果的任何小数部分

Fixed-width integers

C99定义了一组固定宽度的整数(在stdint.h头文件中)
这里写图片描述

如果您的编译器不包含cstdint或stdint.h,好消息是您可以下载Paul Hsieh的stdint.h头文件的pstdint.h跨平台兼容版本。只需将pstdint.h文件包含在您的项目中,它将为您的平台定义适当大小的固定宽度整数类型。

由于C ++规范的疏漏,大多数编译器都将int8_t和uint8_t分别定义为类型signed charunsigned char,但这不是必需的。因此,std :: cin和std :: cout的工作方式可能与您所期望的不同。这是一个示例程序,显示了这一点:

#include <cstdint>
#include <iostream>

int main()
{
    int8_t myint = 65;
    std::cout << myint;

    return 0;
}

在大多数系统中,该程序将打印’A’(将myint视为字符)。但是,在某些系统上,这可能会按预期打印65。

为了简单起见,最好避免int8_t和uint8_t(改用int16_t或uint16_t)。但是,如果使用int8_t或uint8_t,则应该小心任何将int8_t或uint8_t解释为char而不是整数(包括std :: cout和std:cin)的函数。

  • 规则:避免int8_t和uint8_t。如果你确实使用它们,请注意它们通常被视为字符。

Fast and least

为了解决上述缺点,C ++ 11还定义了两组固定宽度的整数。

快速类型 (int_fast#_t) (where # = 8, 16, 32, or 64),int_fast32_t将为您提供至少32位的最快整数类型

最小类型(int_least#_t)为您提供了宽度至少为#位(其中#= 8,16,32或64)的最小整数类型。例如,int_least32_t会给你至少32位的最小整数类型。

整数最佳实践

C ++中整数的最佳实践如下所示:

  • 当整数的大小无关紧要时,int应该是首选。例如,如果你要求用户输入他们的年龄,或者从1到10的数字,那么int是16位还是32位(这些数字将适合任何一种)并不重要。这将涵盖您可能遇到的绝大多数情况。
  • 如果你需要一个变量保证是一个特定的大小,并希望性能,使用int_fast#_t
  • 如果你需要一个变量保证是一个特定的大小,并且希望在性能上优先考虑内存,使用int_least#_t。这在分配大量变量时最常使用。
  • 如果您有令人信服的理由,请仅使用无符号类型。int_least#_t
发布了96 篇原创文章 · 获赞 179 · 访问量 64万+

猜你喜欢

转载自blog.csdn.net/wc781708249/article/details/80289253