在这篇文章中,我们将学习C++中固定宽度的整数类型中最快和最小的类型,如int_least8_t、int_fast8_t等。
C或C++中的基本数据类型是char、short、int和long,每个类型在内存中分别允许1、2或4,8个字节。然而,这个字节可以根据所使用的编译器、操作系统或硬件而有所不同。
在32位编译器上,Long数据类型在内存中分配4比特,而在64位编译器上,它在内存中分配8字节。 换句话说,数据类型的大小取决于编译器。
为了克服这个问题,C提供了一个固定宽度的整数类型,它分配一定数量的内存字节。
固定宽度的整数类型。
具有固定位数的整数数据类型被称为固定宽度的整数。 顾名思义,在内存中分配固定数量的字节是很有用的。
要定义固定宽度的整数,程序员必须在std命名空间中添加cstdint>头文件。
对于像short、int和long这样的数据类型,C++规定了最小的字节数。而固定宽度的整数则确保了一定的大小,因此它们不能被移植,因为不是每个平台都支持它们。
**例如:**int32_t - 内存大小固定为32字节的整数。
有两种类型的固定类型的整数。
1.单一的:
intX t : 内存为X字节的单一整数。
2.2.无符号的
uintX t:内存中含有X字节的无符号整数。
其中x是存储在内存中的字节数(4、8、16、32)。
优势
在内存中提供了一个固定的字节大小。
缺点是
它不具有可移植性。
固定宽度的整数的类型
C++支持三种固定宽度的整数类型。
- 固定大小的整数:用户选择的大小
- 最小的固定尺寸的整数:其尺寸至少是用户指定的尺寸。
- 最快的固定大小的整数:其大小至少是你指定的大小。
整数的固定大小
它包含固定大小的比特。它们被进一步分为两组。
1.有符号的固定整数
有符号的整数通常由int(no_of_byte)_t类型定义.这里列出了固定有符号的整数类型。
- std::int8_t定义了1字节的有符号数据类型。
- std::int16_t定义16字节有符号数据类型。
- std::int32_t定义32字节有符号数据类型。
- std::int64_t定义64字节有符号数据类型。
2.无符号固定整数
无符号整数是由uint(no_of_byte)_t定义的。下面是固定无符号整数类型的列表。
- std::uint8_t定义了1字节的无符号数据类型。
- std::uint16_t定义了16字节的无符号数据类型。
- std::uint32_t定义32字节的无符号数据类型。
- std::uint64_t定义了64字节的无符号数据类型。
从上表中你可以了解到,有符号的整数通常由int(no_of_byte)_t类型定义,无符号的整数则由uint(no_of_byte)_t定义。
例子:
#include <cstdint>
#include <iostream>
int main()
{
std::int16_t i{16};
std::cout << "data :"<<i;
return 0;
}
输出 :
data : 5
最小的固定宽度整数
该类型提供最小的有符号或无符号整数类型,其宽度至少为比特,该类型包括8、16、32或64字节。
它们又被分为两组:
- 最小的有符号固定宽度整数
- 最小的无符号固定宽度的整数
最小的有符号整数
最小的有符号整数类型包括至少8、16、32和64位的宽度。
以下是最小的有符号类型列表:
- int_least8_t
- int_least16_t
- int_least32_t
- int_least64_t
最小的无符号整数
最小的无符号整数类型包括至少8、16、32和64位的宽度,
这里列出了最小的无符号类型。
- uint_least8_t
- uint_least16_t
- uint_least32_t
- uint_least64_t
例子 :
#include <cstdint>
#include <iostream>
//main code
int main()
{
std::cout << "Smallest signed integer in C++ ";
std::cout << '\n';
std::cout << "least 8 bit : " << sizeof(std::int_least8_t) * 8 << " bits\n";
std::cout << "least 16 bit: " << sizeof(std::int_least16_t) * 8 << " bits\n";
std::cout << "least 32 bit: " << sizeof(std::int_least32_t) * 8 << " bits\n";
std::cout << "least 64 bit: " << sizeof(std::int_least64_t) * 8 << " bits\n";
std::cout << '\n';
std::cout << "Smallest unsigned integer types in C++ ";
std::cout << '\n';
std::cout << "least 8 bit : " << sizeof(std::uint_least8_t) * 8 << " bits\n";
std::cout << "least 16 bit: " << sizeof(std::uint_least16_t) * 8 << " bits\n";
std::cout << "least 32 bit: " << sizeof(std::uint_least32_t) * 8 << " bits\n";
std::cout << "least 64 bit: " << sizeof(std::uint_least64_t) * 8 << " bits\n";
return 0;
}
输出 :
Smallest signed integer in C++
least 8 bit : 8 bits
least 16 bit: 16 bits
least 32 bit: 32 bits
least 64 bit: 64 bits
Smallest unsigned integer types in C++
least 8 bit : 8 bits
least 16 bit: 16 bits
least 32 bit: 32 bits
least 64 bit: 64 bits
最快的固定宽度整数
这种类型提供了最快的有符号或无符号的整数类型,其宽度至少是位,包括8、16、32或64位内存。
它们又被分为两组。
- 最快的有符号整数
- 最快的无符号整数
最快的有符号整数
这种最快的有符号整数类型包括至少8、16、32和64位的宽度。
最快的有符号类型的列表是:。
- int_fast8_t
- int_fast16_t
- int_fast32_t
- int_fast64_t
最快的无符号整数
最快无符号整数类型包括至少8、16、32和64位的宽度。
最快无符号类型的列表是。
- uint_fast8_t
- uint_fast16_t
- uint_fast32_t
- uint_fast64_t
例子 :
#include <cstdint>
#include <iostream>
//main code
int main()
{
std::cout << "Fastest Signed Integer of Fixed width integer types in C++ ";
std::cout << '\n';
std::cout << "Fastest 8: " << sizeof(std::int_fast8_t) * 8 << " bits\n";
std::cout << "Fastest 16: " << sizeof(std::int_fast16_t) * 8 << " bits\n";
std::cout << "Fastest 32: " << sizeof(std::int_fast32_t) * 8 << " bits\n";
std::cout << "Fastest 64: " << sizeof(std::int_fast64_t) * 8 << " bits\n";
std::cout << '\n';
std::cout << "Fastest Unsigned Integer of Fixed width integer types in C++ ";
std::cout << '\n';
std::cout << "Fastest 8: " << sizeof(std::uint_fast8_t) * 8 << " bits\n";
std::cout << "Fastest 16: " << sizeof(std::uint_fast16_t) * 8 << " bits\n";
std::cout << "Fastest 32: " << sizeof(std::uint_fast32_t) * 8 << " bits\n";
std::cout << "Fastest 64: " << sizeof(std::uint_fast64_t) * 8 << " bits\n";
return 0;
}
输出 :
Fastest Signed Integer of Fixed width integer types in C++
Fastest 8: 8 bits
Fastest 16: 64 bits
Fastest 32: 64 bits
Fastest 64: 64 bits
Fastest Unsigned Integer of Fixed width integer types in C++
Fastest 8: 8 bits
Fastest 16: 64 bits
Fastest 32: 64 bits
Fastest 64: 64 bits
让我们再举一个例子。
例子 :
#include <cstdint>
#include <iostream>
//main code
int main()
{
std::cout << "Smaller types of Fixed width integer types in C++ ";
std::cout << '\n';
std::cout << "least 8 bit : " << sizeof(std::int_least8_t) * 8 << " bits\n";
std::cout << "least 16 bit: " << sizeof(std::int_least16_t) * 8 << " bits\n";
std::cout << "least 32 bit: " << sizeof(std::int_least32_t) * 8 << " bits\n";
std::cout << "least 64 bit: " << sizeof(std::int_least64_t) * 8 << " bits\n";
std::cout << '\n';
std::cout << "Fastest types of Fixed width integer types in C++ ";
std::cout << '\n';
std::cout << "Fastest 8: " << sizeof(std::int_fast8_t) * 8 << " bits\n";
std::cout << "Fastest 16: " << sizeof(std::int_fast16_t) * 8 << " bits\n";
std::cout << "Fastest 32: " << sizeof(std::int_fast32_t) * 8 << " bits\n";
std::cout << "Fastest 64: " << sizeof(std::int_fast64_t) * 8 << " bits\n";
return 0;
}
输出。
Smaller types of Fixed width integer types in C++
least 8 bit : 8 bits
least 16 bit: 16 bits
least 32 bit: 32 bits
least 64 bit: 64 bits
Fastest types of Fixed width integer types in C++
Fastest 8: 8 bits
Fastest 16: 64 bits
Fastest 32: 64 bits
Fastest 64: 64 bits
澄清一下。
这个输出会因系统而异。std::fast16_t的值是16位,而std::int_fast32_t的值是64位。因为32位整数的处理速度比16位整数快。
结论:
在实际问题中,固定宽度的整数类型有助于编写可移植和高效的代码。
让我们检查一下你的知识水平。
问题
需要哪个头文件来声明固定宽度的整数类型?
编码
cstdint
stdio
stdlib
要定义固定宽度的整数,程序员必须 在std命名空间内添加 头文件。