std::ptrdiff_t
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
在头文件 <cstddef> 中定义
|
||
typedef /*implementation-defined*/ ptrdiff_t; |
||
std::ptrdiff_t两个指针相减的结果是有符号整数类型.
原文:
std::ptrdiff_t is the signed integer type of the result of subtracting two pointers.
[编辑] 注释
std::ptrdiff_t用于指针的算术运算和数组下标,如果负值是可能的。使用其他类型,如int的程序,可能会失败,例如:当该指数超过64位系统INT_MAX,或者如果它依赖于32位模数.
原文:
std::ptrdiff_t is used for pointer arithmetic and array indexing, if negative values are possible. Programs that use other types, such as int, may fail on, e.g. 64-bit systems when the index exceeds INT_MAX or if it relies on 32-bit modular arithmetic.
当使用C + +容器库的正确类型,迭代器之间的差异是的成员typedefdifference_type,这是经常的代名词std::ptrdiff_t.
原文:
When working with the C++ container library, the proper type for the difference between iterators is the member typedef difference_type, which is often synonymous with std::ptrdiff_t.
只有相同的数组元素(包括过去数组结尾的指针1)的指针,也可以彼此相减.
原文:
Only pointers to elements of the same array (including the pointer one past the end of the array) may be subtracted from each other.
如果数组是如此之大(大于PTRDIFF_MAX元素,但小于SIZE_MAX字节),两个指针之间的差可能并不表示为std::ptrdiff_t减去两个这样的指针,结果是未定义.
原文:
If an array is so large (greater than PTRDIFF_MAX elements, but less than SIZE_MAX bytes), that the difference between two pointers may not be representable as std::ptrdiff_t, the result of subtracting two such pointers is undefined.
对于字符数组短比PTRDIFF_MAX,std::ptrdiff_t的签名对应的std::size_t作为:它可以存储任何类型的数组的大小,并是在大多数平台上,代名词std::intptr_t).
原文:
For char arrays shorter than PTRDIFF_MAX, std::ptrdiff_t acts as the signed counterpart of std::size_t: it can store the size of the array of any type and is, on most platforms, synonymous with std::intptr_t).
[编辑] 示例
#include <cstddef> int main() { const std::size_t N = 100; int* a = new int[N]; int* end = a + N; for(std::ptrdiff_t i = N; i>0; --i) *(end - i) = i; delete[] a; }
[编辑] 另请参阅
sizeof运算符返回的无符号整数类型 (typedef) | |
标准布局类型的成员相对类型起始地址的字符偏移量 (函数宏) |