首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::ptrdiff_t

Defined in header <cstddef>

typedef /*implementation-defined*/ ptrdiff_t;

std::ptrdiff_t减除两个指针的结果的有符号整数类型。

注记

std::ptrdiff_t用于指针算法和数组索引,如果可能出现负值。使用其他类型的程序,例如int,例如,当索引超过64位时,系统可能会发生故障。INT_MAX或者它是否依赖于32位模块运算。

在使用C++容器库时,迭代器之间的区别的适当类型是difference_type,这通常是与std::ptrdiff_t...

只有指向相同数组%28的元素(包括指针)的指针(在数组结束后的指针)(%5月29日)相互减除。

如果数组如此大,%28大于PTRDIFF_MAX元素,但小于SIZE_MAX字节%29,即两个指针之间的差异可能不能表示为std::ptrdiff_t,减去两个这样的指针的结果是未知的。

对于短于PTRDIFF_MAX,,,std::ptrdiff_t作为签署的对应方std::size_t*它可以存储任意类型数组的大小,并且在大多数平台上都是std::intptr_t...

二次

代码语言:javascript
复制
#include <cstddef>
#include <iostream>
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)
        std::cout << (*(end - i) = i) << ' ';
    delete[] a;
}

二次

另见

size_t

unsigned integer type returned by the sizeof operator (typedef)

offsetof

byte offset from the beginning of a standard-layout type to specified member (function macro)

c ptrdiff文档[医]T型

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券