std::ptrdiff_t
来自cppreference.com
|
|
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
| Defined in header <cstddef>
|
||
| typedef /*implementation-defined*/ ptrdiff_t; |
||
std::ptrdiff_t两个指针相减的结果是有符号整数类型.
Original:
std::ptrdiff_t is the signed integer type of the result of 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.
You can help to correct and verify the translation. Click here for instructions.
[编辑] 注释
std::ptrdiff_t用于指针的算术运算和数组下标,如果负值是可能的。使用其他类型,如int的程序,可能会失败,例如:当该指数超过64位系统INT_MAX,或者如果它依赖于32位模数.
Original:
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.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
当使用C + +容器库的正确类型,迭代器之间的差异是的成员typedefdifference_type,这是经常的代名词std::ptrdiff_t.
Original:
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.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
只有相同的数组元素(包括过去数组结尾的指针1)的指针,也可以彼此相减.
Original:
Only pointers to elements of the same array (including the pointer one past the end of the array) may be subtracted from each other.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
如果数组是如此之大(大于PTRDIFF_MAX元素,但小于SIZE_MAX字节),两个指针之间的差可能并不表示为std::ptrdiff_t减去两个这样的指针,结果是未定义.
Original:
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.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
对于字符数组短比PTRDIFF_MAX,std::ptrdiff_t的签名对应的std::size_t作为:它可以存储任何类型的数组的大小,并是在大多数平台上,代名词std::intptr_t).
Original:
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).
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[编辑] 为例
#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运营商 Original: unsigned integer type returned by the sizeof operator 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. (函数宏) | |