std::size_t
来自cppreference.com
| Defined in header <cstddef>
|
||
| Defined in header <cstdio>
|
||
| Defined in header <cstring>
|
||
| Defined in header <ctime>
|
||
| typedef /*implementation-defined*/ size_t; |
||
std::size_t 是 sizeof 和 alignof 操作符返回的一种无符号整数类型。
[编辑] 注解
size_t 可以存放下理论上可能存在的对象的最大大小,该对象可以是任何类型,包括数组。在许多平台上(使用分段寻址的系统除外),std::size_t 可以存放下任何非成员的指针,此时可以视作其与 std::uintptr_t 同义。
std::size_t 通常被用于数组索引和循环计数。使用其它类型来进行数组索引操作的程序可能会在某些情况下出错,例如在 64 位系统中使用 unsigned int 进行索引时,如果索引号超过 UINT_MAX 或者依赖于 32 位取模运算的话,程序就会出错。
在对诸如 std::string、std::vector 等 C++ 容器进行索引操作时,正确的类型是该容器的成员 typedef size_type,而该类型通常被定义为与 std::size_t 相同。
[编辑] 示例
#include <cstddef> int main() { const std::size_t N = 100; int* a = new int[N]; for(std::size_t n = 0; n<N; ++n) a[n] = n; delete[] a; }
[编辑] 另请参阅
| 符号的整数类型时,返回两个指针相减 Original: signed integer type returned when subtracting two pointers The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (的typedef) | |
| 一个标准的布局类型指定的成员从一开始的字节偏移量 Original: byte offset from the beginning of a standard-layout type to specified member The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (函数宏) | |