gpt4 book ai didi

c++ - size_t 和 std::size_t 的区别

转载 作者:行者123 更新时间:2023-12-01 14:58:11 32 4
gpt4 key购买 nike

size_t有什么区别和 std::size_t在声明它们的位置,何时应该使用它们以及任何其他差异化功能方面?

最佳答案

C的size_t和 C++ 的 std::size_t都是一样的。

在 C 中,它在 <stddef.h> 中定义在 C++ 中,它在 <cstddef> 中定义其内容与 C 头文件相同(参见下面的引文)。其定义为 无符号整数类型 的结果尺寸运算符(operator)。

C 标准在 §17.7/2 中说,

size_t which is the unsigned integer type of the result of the sizeof operator



C++ 标准在 §18.1/3 中说(关于 cstddef header ),

The contents are the same as the Standard C library header , with the following changes.



所以是的,两者都是一样的; 唯一的区别是 C++ 定义了 size_tstd命名空间。

另请注意,上面的行还说“有以下更改”,这不是指 size_t .它更确切地说是指 C++ 对语言(C 中不存在)所做的新添加(主要是),这些添加也定义在同一标题中。

维基百科有关于 size_t 的范围和存储大小的非常好的信息:

Range and storage size of size_t

The actual type of size_t is platform-dependent; a common mistake is to assume size_t is the same as unsigned int, which can lead to programming errors,[3][4] when moving from 32 to 64-bit architecture, for example.

According to the 1999 ISO C standard (C99), size_t is an unsigned integer type of at least 16 bits.



其余的你可以从 this page 阅读在维基百科。

关于c++ - size_t 和 std::size_t 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59825723/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com