卿本佳人19 2022-03-01 14:40 采纳率: 91.3%
浏览 15
已结题

lower_bound(a+j,a+n,temp)-a中为什么要“-a”?

lower_bound(a+j,a+n,temp)-a;
lower_bound(a+j,a+n,temp)-a中为什么要“-a”?

  • 写回答

2条回答 默认 最新

  • 赵4老师 2022-03-01 15:51
    关注

    lower_bound(...)返回一个和a类型一样的指针比如a1
    a1-a得到a1在a后面第几个

    Pointer Arithmetic
    Additive operations involving a pointer and an integer give meaningful results only if the pointer operand addresses an array member and the integer value produces an offset within the bounds of the same array. When the integer value is converted to an address offset, the compiler assumes that only memory positions of the same size lie between the original address and the address plus the offset.

    This assumption is valid for array members. By definition, an array is a series of values of the same type; its elements reside in contiguous memory locations. However, storage for any types except array elements is not guaranteed to be filled by the same type of identifiers. That is, blanks can appear between memory positions, even positions of the same type. Therefore, the results of adding to or subtracting from the addresses of any values but array elements are undefined.

    Similarly, when two pointer values are subtracted, the conversion assumes that only values of the same type, with no blanks, lie between the addresses given by the operands.
    以下是上面文字用百度自动翻译的结果:(^_^)
    指针算法
    只有当指针操作数寻址数组成员且整数值在同一数组的边界内产生偏移量时,涉及指针和整数的加法运算才会给出有意义的结果。当整数值转换为地址偏移量时,编译器假定只有大小相同的内存位置位于原始地址和地址加上偏移量之间。
    此假设对数组成员有效。根据定义,数组是一系列相同类型的值;它的元素位于连续的内存位置。然而,除了数组元素之外的任何类型的存储都不能保证由相同类型的标识符填充。也就是说,空白可以出现在内存位置之间,甚至是相同类型的位置。因此,除了数组元素之外的任何值的地址加或减的结果都是未定义的。
    类似地,当两个指针值相减时,转换假定只有相同类型的值(不带空格)位于操作数给定的地址之间。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 3月14日
  • 已采纳回答 3月6日
  • 创建了问题 3月1日

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改